From 21b92f248e4dae1b2de24952913cd96ed1a78e8a Mon Sep 17 00:00:00 2001 From: fjrodriguez Date: Wed, 12 Apr 2023 08:07:06 -0600 Subject: [PATCH] Correcciones en nombre sea de dios --- Admin/settings.py | 2 +- .../migrations/0016_alter_device_database.py | 18 ++++++++++++++++++ .../migrations/0017_alter_device_macaddress.py | 18 ++++++++++++++++++ Sistemas/models.py | 9 ++++++--- Sistemas/serializers.py | 4 ++-- Sistemas/views.py | 12 +++--------- 6 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 Sistemas/migrations/0016_alter_device_database.py create mode 100644 Sistemas/migrations/0017_alter_device_macaddress.py diff --git a/Admin/settings.py b/Admin/settings.py index 14d896d..decd797 100644 --- a/Admin/settings.py +++ b/Admin/settings.py @@ -15,7 +15,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent SECRET_KEY = os.getenv("adminAS_KEY") # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = False +DEBUG = True ALLOWED_HOSTS = ['*'] # Application definition diff --git a/Sistemas/migrations/0016_alter_device_database.py b/Sistemas/migrations/0016_alter_device_database.py new file mode 100644 index 0000000..73ebbd6 --- /dev/null +++ b/Sistemas/migrations/0016_alter_device_database.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.3 on 2023-04-12 13:03 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('Sistemas', '0015_device_database'), + ] + + operations = [ + migrations.AlterField( + model_name='device', + name='database', + field=models.CharField(blank=True, max_length=30, null=True), + ), + ] diff --git a/Sistemas/migrations/0017_alter_device_macaddress.py b/Sistemas/migrations/0017_alter_device_macaddress.py new file mode 100644 index 0000000..fdbea1c --- /dev/null +++ b/Sistemas/migrations/0017_alter_device_macaddress.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.3 on 2023-04-12 13:54 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('Sistemas', '0016_alter_device_database'), + ] + + operations = [ + migrations.AlterField( + model_name='device', + name='macAddress', + field=models.CharField(blank=True, max_length=30, null=True), + ), + ] diff --git a/Sistemas/models.py b/Sistemas/models.py index 4d92fcf..a7a7961 100644 --- a/Sistemas/models.py +++ b/Sistemas/models.py @@ -77,14 +77,15 @@ class Device(models.Model): token = models.OneToOneField(Token, on_delete=models.CASCADE, blank=True,null=True) username = models.OneToOneField(User, on_delete=models.CASCADE) sistema = models.ForeignKey(Sistema,on_delete=models.CASCADE, blank=True, null=True) - macAddress = models.CharField(max_length=30, unique=True, blank=True,null=True) - database = models.CharField(max_length=30, unique=True, blank=True,null=True) + macAddress = models.CharField(max_length=30, blank=True,null=True) + database = models.CharField(max_length=30, blank=True,null=True) #objects = DeviceManager() def generate_unique_username(self,client, device_name,ip_address, macAddress, database): username = f"Device_{client.RFC}_{device_name}_{ip_address}_{macAddress}_{database}" username_ = re.sub(r'\W+', '', username) if User.objects.filter(username=username_).exists(): + return User.objects.get(username=username_) raise ValidationError(f"El Usuario ya existe {username_}") obj= User.objects.create_user( username=username_ @@ -95,7 +96,9 @@ class Device(models.Model): if not self.pk: obj = self.generate_unique_username(self.client,self.device_name, self.ip_address, self.macAddress, self.database) self.username= obj - token= Token.objects.create(user=obj) + + token, created = Token.objects.get_or_create(user=obj) + self.token=token super().save(*args, **kwargs) DeviceHistory.objects.create(device=self, ip_address=self.ip_address) diff --git a/Sistemas/serializers.py b/Sistemas/serializers.py index 03e0ba7..2c33142 100644 --- a/Sistemas/serializers.py +++ b/Sistemas/serializers.py @@ -44,13 +44,13 @@ class DeviceSerializer(serializers.ModelSerializer): & Q(macAddress__icontains=mac_address) & Q(database=self.context['request'].data.get('database')) ) - + print('existing_devices', existing_devices) if existing_devices.exists(): # 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 suffix = existing_devices.count() + 1 mac_address += f'_{suffix}' - + print('suffix mac_address',mac_address) validated_data['macAddress']= mac_address return super().create(validated_data) diff --git a/Sistemas/views.py b/Sistemas/views.py index ca413f1..5672118 100644 --- a/Sistemas/views.py +++ b/Sistemas/views.py @@ -233,18 +233,12 @@ class GetDeviceToken(APIView): device_name= data.get('device_name') ip_address = data.get('ip_address') macAddress = data.get('macAddress') - username = f"Device_{cli.RFC}_{device_name}_{ip_address}_{macAddress}" - username = re.sub(r'\W+', '', username) + username_ = f"Device_{cli.RFC}_{device_name}_{ip_address}_{macAddress}" + username_ = re.sub(r'\W+', '', username_) device = Device.objects.filter( - Q(username__username__icontains=username) - # Q(client__id=cli.id) - # & Q(macAddress=data.get('macAddress')) - # & Q(device_name__icontains=data.get('device_name')) - # & Q(ip_address__icontains=data.get('ip_address')) - # & Q(sistema__id= sis.id) - # & Q(database=data.get('database')) + username__username__icontains=username_ ).first() if device is not None: