Restructure Device Register
This commit is contained in:
17
Sistemas/migrations/0019_alter_device_options.py
Normal file
17
Sistemas/migrations/0019_alter_device_options.py
Normal file
@@ -0,0 +1,17 @@
|
||||
# Generated by Django 4.1.3 on 2023-04-13 13:17
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('Sistemas', '0018_alter_device_token_alter_device_username'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name='device',
|
||||
options={'ordering': ('username',)},
|
||||
),
|
||||
]
|
||||
@@ -73,24 +73,25 @@ class Device(models.Model):
|
||||
client = models.ForeignKey(Clientes,on_delete=models.CASCADE)
|
||||
device_name = models.CharField(max_length=255)
|
||||
device_os = models.CharField(max_length=255)
|
||||
ip_address= models.GenericIPAddressField()
|
||||
#token = models.OneToOneField(Token, on_delete=models.CASCADE, blank=True,null=True)
|
||||
token = models.ForeignKey(Token, on_delete=models.CASCADE, blank=True,null=True)
|
||||
|
||||
#username = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||
ip_address= models.GenericIPAddressField()
|
||||
token = models.ForeignKey(Token, on_delete=models.CASCADE, blank=True,null=True)
|
||||
username = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
|
||||
sistema = models.ForeignKey(Sistema,on_delete=models.CASCADE, 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()
|
||||
#objects = DeviceManager()
|
||||
|
||||
class Meta:
|
||||
ordering = ('username',)
|
||||
|
||||
def generate_unique_username(self,client, device_name,ip_address, macAddress):
|
||||
username = f"Device_{client.RFC}_{device_name}_{ip_address}_{macAddress}"
|
||||
#username = f"Device_{client.RFC}_{device_name}_{ip_address}_{macAddress}"
|
||||
username = f"Device_{client.RFC}_{device_name}_{macAddress}"
|
||||
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_}")
|
||||
#raise ValidationError(f"El Usuario ya existe {username_}")
|
||||
obj= User.objects.create_user(
|
||||
username=username_
|
||||
)
|
||||
@@ -116,12 +117,6 @@ class DeviceHistory(models.Model):
|
||||
last_authentication = models.DateTimeField(auto_now=True)
|
||||
ip_address = models.GenericIPAddressField()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class Maquinas_Conectadas(models.Model):
|
||||
|
||||
UserName = models.CharField(max_length=255)
|
||||
@@ -130,7 +125,6 @@ class Maquinas_Conectadas(models.Model):
|
||||
OSversion= models.CharField(max_length=255)
|
||||
local_ip = models.CharField(max_length=55)
|
||||
public_ip= models.CharField(max_length=55)
|
||||
|
||||
Token = models.CharField(max_length=50)
|
||||
Cliente = models.CharField(max_length=13)
|
||||
class Meta:
|
||||
|
||||
@@ -36,27 +36,28 @@ class DeviceSerializer(serializers.ModelSerializer):
|
||||
#we need passing the post data to the creation instance before commited to the DB
|
||||
def create(self, validated_data):
|
||||
mac_address = self.context['request'].data.get('macAddress')
|
||||
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(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
|
||||
suffix = existing_devices.count() + 1
|
||||
mac_address += f'_{suffix}'
|
||||
print('')
|
||||
# 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(database=self.context['request'].data.get('database'))
|
||||
# )
|
||||
# print('self.context[request].data',self.context['request'].data)
|
||||
# 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}'
|
||||
|
||||
validated_data['macAddress']= mac_address
|
||||
return super().create(validated_data)
|
||||
|
||||
def validate(self, data):
|
||||
sistema = data.get('sistema', None)
|
||||
client = data.get('client', None)
|
||||
|
||||
try:
|
||||
sistemaxCli = sistemas_por_cliente.objects.get(id_sistema=sistema,cliente=client)
|
||||
except sistemas_por_cliente.DoesNotExist:
|
||||
|
||||
@@ -155,9 +155,9 @@ class UsersConnectedList(UserPassesTestMixin,LoginRequiredMixin,ListView):
|
||||
|
||||
class SistemasXCliente_ListView(UserPassesTestMixin,LoginRequiredMixin, ListView):
|
||||
model = sistemas_por_cliente
|
||||
paginate_by = 20
|
||||
paginate_by = 100
|
||||
template_name = 'Sistemas/Xclientes/lista.html'
|
||||
|
||||
|
||||
def test_func(self):
|
||||
res = self.request.user.groups.filter(name= 'admin_soft')
|
||||
if not res:
|
||||
@@ -191,7 +191,7 @@ class SistemasXCliente_DetailView(UserPassesTestMixin,LoginRequiredMixin, Detail
|
||||
sistemas_por_cliente_ = self.object #type: ignore
|
||||
sistema = sistemas_por_cliente_.id_sistema
|
||||
cliente = sistemas_por_cliente_.cliente
|
||||
|
||||
|
||||
context['devices'] = Device.objects.filter(sistema=sistema, client=cliente.id)
|
||||
return context
|
||||
|
||||
@@ -228,16 +228,17 @@ class GetDeviceToken(APIView):
|
||||
def post(self,request):
|
||||
try:
|
||||
data = request.data
|
||||
sis = Sistema.objects.get(nombre_sistema=data.get('sistema'))
|
||||
#sis = Sistema.objects.get(nombre_sistema=data.get('sistema'))
|
||||
#ip_address = data.get('ip_address')
|
||||
cli = Clientes.objects.get(RFC=data.get('client'))
|
||||
device_name= data.get('device_name')
|
||||
ip_address = data.get('ip_address')
|
||||
macAddress = data.get('macAddress')
|
||||
database = data.get('database')
|
||||
|
||||
username_ = f"Device_{cli.RFC}_{device_name}_{ip_address}_{macAddress}"
|
||||
username_ = re.sub(r'\W+', '', username_)
|
||||
print(username_)
|
||||
#username_ = f"Device_{cli.RFC}_{device_name}_{ip_address}_{macAddress}"
|
||||
username_ = f"Device_{cli.RFC}_{device_name}_{macAddress}"
|
||||
username_ = re.sub(r'\W+', '', username_)
|
||||
|
||||
device = Device.objects.filter(
|
||||
username__username__icontains=username_,
|
||||
database=database
|
||||
@@ -317,9 +318,9 @@ class CheckVersionView(APIView):
|
||||
def post(self, request,*args, **kwargs):
|
||||
try:
|
||||
version = request.data.get('version')
|
||||
|
||||
|
||||
client_version = [int(x) for x in version.split(".")]
|
||||
|
||||
|
||||
try:
|
||||
ver = Sistema.objects.get(nombre_sistema="CFDI")
|
||||
server_version = [int(x) for x in ver.version.split(".")]
|
||||
@@ -327,8 +328,6 @@ class CheckVersionView(APIView):
|
||||
BitacoraErrores.objects.create(level=2, message=str(ex), traceback=traceback.format_exc(), view='Sistemas.CheckVersionView')
|
||||
return Response({'Error':f'{ex}','isError':True})
|
||||
|
||||
print('client_version: ',client_version)
|
||||
print('server_version', server_version)
|
||||
result=False
|
||||
for cont, ele in enumerate(client_version):
|
||||
if client_version[cont] != server_version[cont]:
|
||||
|
||||
Reference in New Issue
Block a user