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})
|
return Response({'Error':f'check_RFC:{E} RFC:{rfc}','isError':True})
|
||||||
|
|
||||||
class CancelaTimbre(APIView):
|
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]
|
permissions_classes=[IsAuthenticated,ItsAdminToken]
|
||||||
|
|
||||||
def post(self,request,*args, **kwargs):
|
def post(self,request,*args, **kwargs):
|
||||||
UUID = request.data.get('UUID')
|
UUID = request.data.get('UUID')
|
||||||
try:
|
try:
|
||||||
|
|
||||||
timbre = Timbres.objects.get(uuid=UUID)
|
timbre = Timbres.objects.get(uuid=UUID)
|
||||||
obj={'uuid':f'{timbre.uuid}_','rfcc':timbre.rfcc,'fecha':timbre.fecha,'folio':timbre.folio,
|
obj={'uuid':f'{timbre.uuid}_','rfcc':timbre.rfcc,'fecha':timbre.fecha,'folio':timbre.folio,
|
||||||
'serie':timbre.serie,'tipo':'Cancela','rfcp':timbre.rfcp,'modo':'Normal'}
|
'serie':timbre.serie,'tipo':'Cancela','rfcp':timbre.rfcp,'modo':'Normal'}
|
||||||
@@ -419,7 +418,8 @@ class CancelaTimbre(APIView):
|
|||||||
|
|
||||||
return Response({'success':True})
|
return Response({'success':True})
|
||||||
except Exception as ex:
|
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})
|
return Response({'Error':f'{ex}','isError':True})
|
||||||
|
|
||||||
class add_timbre(APIView):
|
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)
|
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, unique=True, blank=True,null=True)
|
||||||
|
database = models.CharField(max_length=30, unique=True, blank=True,null=True)
|
||||||
#objects = DeviceManager()
|
#objects = DeviceManager()
|
||||||
def generate_unique_username(self,client, 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}"
|
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():
|
||||||
@@ -92,7 +93,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)
|
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= Token.objects.create(user=obj)
|
||||||
self.token=token
|
self.token=token
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ class DeviceSerializer(serializers.ModelSerializer):
|
|||||||
macAddress = serializers.CharField(read_only=True)
|
macAddress = serializers.CharField(read_only=True)
|
||||||
class Meta:
|
class Meta:
|
||||||
model =Device
|
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
|
#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
|
||||||
@@ -39,8 +39,12 @@ class DeviceSerializer(serializers.ModelSerializer):
|
|||||||
sistema = validated_data['sistema']
|
sistema = validated_data['sistema']
|
||||||
client = validated_data['client']
|
client = validated_data['client']
|
||||||
existing_devices = Device.objects.filter(
|
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():
|
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
|
||||||
|
|||||||
@@ -230,7 +230,6 @@ class GetDeviceToken(APIView):
|
|||||||
data = request.data
|
data = request.data
|
||||||
sis = Sistema.objects.get(nombre_sistema=data.get('sistema'))
|
sis = Sistema.objects.get(nombre_sistema=data.get('sistema'))
|
||||||
cli = Clientes.objects.get(RFC=data.get('client'))
|
cli = Clientes.objects.get(RFC=data.get('client'))
|
||||||
mac_address = data.get('macAddress')
|
|
||||||
|
|
||||||
device = Device.objects.filter(
|
device = Device.objects.filter(
|
||||||
Q(client__id=cli.id)
|
Q(client__id=cli.id)
|
||||||
@@ -238,9 +237,9 @@ class GetDeviceToken(APIView):
|
|||||||
& Q(device_name__icontains=data.get('device_name'))
|
& Q(device_name__icontains=data.get('device_name'))
|
||||||
& Q(ip_address__icontains=data.get('ip_address'))
|
& Q(ip_address__icontains=data.get('ip_address'))
|
||||||
& Q(sistema__id= sis.id)
|
& Q(sistema__id= sis.id)
|
||||||
|
& Q(database=data.get('database'))
|
||||||
).first()
|
).first()
|
||||||
|
|
||||||
|
|
||||||
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