in name of god 2.0
This commit is contained in:
@@ -20,7 +20,7 @@ class DeviceHistoryAdmin(admin.ModelAdmin):
|
|||||||
list_display = ['device','first_authentication', 'last_authentication']
|
list_display = ['device','first_authentication', 'last_authentication']
|
||||||
|
|
||||||
class DeviceAdmin(admin.ModelAdmin):
|
class DeviceAdmin(admin.ModelAdmin):
|
||||||
list_display = ['client', 'device_name', 'ip_address', 'sistema', 'macAddress']
|
list_display = ['client', 'device_name', 'ip_address', 'sistema', 'macAddress','database']
|
||||||
|
|
||||||
admin.site.register(BitacoraErrores,BitacoraErroresAdmin)
|
admin.site.register(BitacoraErrores,BitacoraErroresAdmin)
|
||||||
admin.site.register(Sistema,Sistema_Admin)
|
admin.site.register(Sistema,Sistema_Admin)
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
# Generated by Django 4.1.3 on 2023-04-12 15:12
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||||
|
('authtoken', '0003_tokenproxy'),
|
||||||
|
('Sistemas', '0017_alter_device_macaddress'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='device',
|
||||||
|
name='token',
|
||||||
|
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='authtoken.token'),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='device',
|
||||||
|
name='username',
|
||||||
|
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -74,14 +74,18 @@ class Device(models.Model):
|
|||||||
device_name = models.CharField(max_length=255)
|
device_name = models.CharField(max_length=255)
|
||||||
device_os = models.CharField(max_length=255)
|
device_os = models.CharField(max_length=255)
|
||||||
ip_address= models.GenericIPAddressField()
|
ip_address= models.GenericIPAddressField()
|
||||||
token = models.OneToOneField(Token, on_delete=models.CASCADE, blank=True,null=True)
|
#token = models.OneToOneField(Token, on_delete=models.CASCADE, blank=True,null=True)
|
||||||
username = models.OneToOneField(User, on_delete=models.CASCADE)
|
token = models.ForeignKey(Token, on_delete=models.CASCADE, blank=True,null=True)
|
||||||
|
|
||||||
|
#username = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||||
|
username = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
|
|
||||||
sistema = models.ForeignKey(Sistema,on_delete=models.CASCADE, blank=True, null=True)
|
sistema = models.ForeignKey(Sistema,on_delete=models.CASCADE, blank=True, null=True)
|
||||||
macAddress = models.CharField(max_length=30, blank=True,null=True)
|
macAddress = models.CharField(max_length=30, blank=True,null=True)
|
||||||
database = models.CharField(max_length=30, blank=True,null=True)
|
database = models.CharField(max_length=30, blank=True,null=True)
|
||||||
#objects = DeviceManager()
|
#objects = DeviceManager()
|
||||||
def generate_unique_username(self,client, device_name,ip_address, macAddress, database):
|
def generate_unique_username(self,client, device_name,ip_address, macAddress):
|
||||||
username = f"Device_{client.RFC}_{device_name}_{ip_address}_{macAddress}_{database}"
|
username = f"Device_{client.RFC}_{device_name}_{ip_address}_{macAddress}"
|
||||||
username_ = re.sub(r'\W+', '', username)
|
username_ = re.sub(r'\W+', '', username)
|
||||||
|
|
||||||
if User.objects.filter(username=username_).exists():
|
if User.objects.filter(username=username_).exists():
|
||||||
@@ -94,7 +98,7 @@ class Device(models.Model):
|
|||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
if not self.pk:
|
if not self.pk:
|
||||||
obj = self.generate_unique_username(self.client,self.device_name, self.ip_address, self.macAddress, self.database)
|
obj = self.generate_unique_username(self.client,self.device_name, self.ip_address, self.macAddress)
|
||||||
self.username= obj
|
self.username= obj
|
||||||
|
|
||||||
token, created = Token.objects.get_or_create(user=obj)
|
token, created = Token.objects.get_or_create(user=obj)
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ class DeviceSerializer(serializers.ModelSerializer):
|
|||||||
model =Device
|
model =Device
|
||||||
fields = ('client','sistema','device_name','device_os', 'ip_address','token', 'macAddress', 'database',)
|
fields = ('client','sistema','device_name','device_os', 'ip_address','token', 'macAddress', 'database',)
|
||||||
|
|
||||||
#this assign the masAddress value from
|
#this assign the masAddress value from
|
||||||
#the request context passed throught argument in the view to the serializer
|
#the request context passed throught argument in the view to the serializer
|
||||||
#given that the macAddres field are read_Only in the beggining of this serialazer
|
#given that the macAddres field are read_Only in the beggining of this serialazer
|
||||||
#we need passing the post data to the creation instance before commited to the DB
|
#we need passing the post data to the creation instance before commited to the DB
|
||||||
@@ -44,13 +44,13 @@ class DeviceSerializer(serializers.ModelSerializer):
|
|||||||
& Q(macAddress__icontains=mac_address)
|
& Q(macAddress__icontains=mac_address)
|
||||||
& Q(database=self.context['request'].data.get('database'))
|
& Q(database=self.context['request'].data.get('database'))
|
||||||
)
|
)
|
||||||
print('existing_devices', existing_devices)
|
|
||||||
if existing_devices.exists():
|
if existing_devices.exists():
|
||||||
# A device with the same macAddress already exists for the given sistema and client
|
# A device with the same macAddress already exists for the given sistema and client
|
||||||
# Get the number of existing devices and add 1 to create a new suffix
|
# Get the number of existing devices and add 1 to create a new suffix
|
||||||
suffix = existing_devices.count() + 1
|
suffix = existing_devices.count() + 1
|
||||||
mac_address += f'_{suffix}'
|
mac_address += f'_{suffix}'
|
||||||
print('suffix mac_address',mac_address)
|
print('')
|
||||||
validated_data['macAddress']= mac_address
|
validated_data['macAddress']= mac_address
|
||||||
return super().create(validated_data)
|
return super().create(validated_data)
|
||||||
|
|
||||||
|
|||||||
@@ -233,14 +233,16 @@ class GetDeviceToken(APIView):
|
|||||||
device_name= data.get('device_name')
|
device_name= data.get('device_name')
|
||||||
ip_address = data.get('ip_address')
|
ip_address = data.get('ip_address')
|
||||||
macAddress = data.get('macAddress')
|
macAddress = data.get('macAddress')
|
||||||
|
database = data.get('database')
|
||||||
|
|
||||||
username_ = f"Device_{cli.RFC}_{device_name}_{ip_address}_{macAddress}"
|
username_ = f"Device_{cli.RFC}_{device_name}_{ip_address}_{macAddress}"
|
||||||
username_ = re.sub(r'\W+', '', username_)
|
username_ = re.sub(r'\W+', '', username_)
|
||||||
|
print(username_)
|
||||||
device = Device.objects.filter(
|
device = Device.objects.filter(
|
||||||
username__username__icontains=username_
|
username__username__icontains=username_,
|
||||||
|
database=database
|
||||||
).first()
|
).first()
|
||||||
|
print('device',device)
|
||||||
if device is not None:
|
if device is not None:
|
||||||
token = {"token":str(device.token)}
|
token = {"token":str(device.token)}
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user