Restructure Device Register
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user