add database to Device model
This commit is contained in:
@@ -405,13 +405,12 @@ class check_RFC(APIView):
|
||||
return Response({'Error':f'check_RFC:{E} RFC:{rfc}','isError':True})
|
||||
|
||||
class CancelaTimbre(APIView):
|
||||
"""API CLASS for upload the CFDI Version into the server"""
|
||||
"""API CLASS for upload the CFDI Version onto the server"""
|
||||
permissions_classes=[IsAuthenticated,ItsAdminToken]
|
||||
|
||||
def post(self,request,*args, **kwargs):
|
||||
UUID = request.data.get('UUID')
|
||||
try:
|
||||
|
||||
timbre = Timbres.objects.get(uuid=UUID)
|
||||
obj={'uuid':f'{timbre.uuid}_','rfcc':timbre.rfcc,'fecha':timbre.fecha,'folio':timbre.folio,
|
||||
'serie':timbre.serie,'tipo':'Cancela','rfcp':timbre.rfcp,'modo':'Normal'}
|
||||
@@ -419,7 +418,8 @@ class CancelaTimbre(APIView):
|
||||
|
||||
return Response({'success':True})
|
||||
except Exception as ex:
|
||||
BitacoraErrores.objects.create(level=2, message=str(ex), traceback=traceback.format_exc(), view='Sistemas.CancelaTimbre')
|
||||
msn = f'UUID:{UUID} \n {str(ex)}'
|
||||
BitacoraErrores.objects.create(level=2, message=msn, traceback=traceback.format_exc(), view='Sistemas.CancelaTimbre')
|
||||
return Response({'Error':f'{ex}','isError':True})
|
||||
|
||||
class add_timbre(APIView):
|
||||
|
||||
18
Sistemas/migrations/0015_device_database.py
Normal file
18
Sistemas/migrations/0015_device_database.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.1.3 on 2023-04-06 16:47
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('Sistemas', '0014_alter_sistemas_por_cliente_unique_together'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='device',
|
||||
name='database',
|
||||
field=models.CharField(blank=True, max_length=30, null=True, unique=True),
|
||||
),
|
||||
]
|
||||
@@ -78,9 +78,10 @@ class Device(models.Model):
|
||||
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)
|
||||
#objects = DeviceManager()
|
||||
def generate_unique_username(self,client, device_name,ip_address, macAddress):
|
||||
username = f"Device_{client.RFC}_{device_name}_{ip_address}_{macAddress}"
|
||||
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():
|
||||
@@ -92,12 +93,12 @@ class Device(models.Model):
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.pk:
|
||||
obj = self.generate_unique_username(self.client,self.device_name, self.ip_address, self.macAddress)
|
||||
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)
|
||||
self.token=token
|
||||
super().save(*args, **kwargs)
|
||||
DeviceHistory.objects.create(device=self,ip_address=self.ip_address)
|
||||
DeviceHistory.objects.create(device=self, ip_address=self.ip_address)
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.username}'
|
||||
|
||||
@@ -28,7 +28,7 @@ class DeviceSerializer(serializers.ModelSerializer):
|
||||
macAddress = serializers.CharField(read_only=True)
|
||||
class Meta:
|
||||
model =Device
|
||||
fields = ('client','sistema','device_name','device_os', 'ip_address','token', 'macAddress')
|
||||
fields = ('client','sistema','device_name','device_os', 'ip_address','token', 'macAddress', 'database',)
|
||||
|
||||
#this assign the masAddress value from
|
||||
#the request context passed throught argument in the view to the serializer
|
||||
@@ -39,8 +39,12 @@ class DeviceSerializer(serializers.ModelSerializer):
|
||||
sistema = validated_data['sistema']
|
||||
client = validated_data['client']
|
||||
existing_devices = Device.objects.filter(
|
||||
Q(sistema=sistema)& Q(client=client)& Q(macAddress__icontains=mac_address)
|
||||
Q(sistema=sistema)
|
||||
& Q(client=client)
|
||||
& Q(macAddress__icontains=mac_address)
|
||||
& Q(database=self.context['request'].data.get('database'))
|
||||
)
|
||||
|
||||
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
|
||||
|
||||
@@ -230,16 +230,15 @@ class GetDeviceToken(APIView):
|
||||
data = request.data
|
||||
sis = Sistema.objects.get(nombre_sistema=data.get('sistema'))
|
||||
cli = Clientes.objects.get(RFC=data.get('client'))
|
||||
mac_address = data.get('macAddress')
|
||||
|
||||
|
||||
device = Device.objects.filter(
|
||||
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(sistema__id= sis.id)
|
||||
& Q(database=data.get('database'))
|
||||
).first()
|
||||
|
||||
|
||||
if device is not None:
|
||||
token = {"token":str(device.token)}
|
||||
|
||||
Reference in New Issue
Block a user