Correcciones en nombre sea de dios

This commit is contained in:
fjrodriguez
2023-04-12 08:07:06 -06:00
parent 4fe84dbfb6
commit 21b92f248e
6 changed files with 48 additions and 15 deletions

View File

@@ -15,7 +15,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = os.getenv("adminAS_KEY") SECRET_KEY = os.getenv("adminAS_KEY")
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False DEBUG = True
ALLOWED_HOSTS = ['*'] ALLOWED_HOSTS = ['*']
# Application definition # Application definition

View File

@@ -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),
),
]

View File

@@ -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),
),
]

View File

@@ -77,14 +77,15 @@ class Device(models.Model):
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) username = models.OneToOneField(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, unique=True, blank=True,null=True) macAddress = models.CharField(max_length=30, blank=True,null=True)
database = models.CharField(max_length=30, unique=True, 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, database):
username = f"Device_{client.RFC}_{device_name}_{ip_address}_{macAddress}_{database}" username = f"Device_{client.RFC}_{device_name}_{ip_address}_{macAddress}_{database}"
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():
return User.objects.get(username=username_)
raise ValidationError(f"El Usuario ya existe {username_}") raise ValidationError(f"El Usuario ya existe {username_}")
obj= User.objects.create_user( obj= User.objects.create_user(
username=username_ username=username_
@@ -95,7 +96,9 @@ class Device(models.Model):
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.database)
self.username= obj self.username= obj
token= Token.objects.create(user=obj)
token, created = Token.objects.get_or_create(user=obj)
self.token=token self.token=token
super().save(*args, **kwargs) super().save(*args, **kwargs)
DeviceHistory.objects.create(device=self, ip_address=self.ip_address) DeviceHistory.objects.create(device=self, ip_address=self.ip_address)

View File

@@ -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)
validated_data['macAddress']= mac_address validated_data['macAddress']= mac_address
return super().create(validated_data) return super().create(validated_data)

View File

@@ -233,18 +233,12 @@ 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')
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( device = Device.objects.filter(
Q(username__username__icontains=username) 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'))
).first() ).first()
if device is not None: if device is not None: