Respaldo anes de irme

This commit is contained in:
fjrodriguez
2023-01-25 15:59:58 -06:00
parent 74ade4ea9b
commit b69f6dbb98
5 changed files with 47 additions and 42 deletions

View File

@@ -5,8 +5,7 @@ from Clientes.models import Clientes
class SistemaPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):
def to_internal_value(self,data):
try:
print(Sistema.objects.get(nombre_sistema=data))
try:
return Sistema.objects.get(nombre_sistema=data)
except Sistema.DoesNotExist:
raise serializers.ValidationError("Sistema no existe")
@@ -21,18 +20,28 @@ class ClientPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):
class DeviceSerializer(serializers.ModelSerializer):
client = ClientPrimaryKeyRelatedField(queryset=Clientes.objects.all())
sistema = SistemaPrimaryKeyRelatedField(queryset=Sistema.objects.all())
#this read_only fields not are required to be updated in the serializer.
#however in the model are required in other instantiation like queries, forms, are required to add it
token = serializers.CharField(read_only=True)
macAddress = serializers.CharField(read_only=True)
class Meta:
model =Device
fields = ('client','sistema','device_name','device_os', 'ip_address','token')
fields = ('client','sistema','device_name','device_os', 'ip_address','token', 'macAddress')
#this assign the masAddress value from
#the request context passed throught argument in the view to the serializer
#given that the macAddres field are read_Only in the beggining of this serialazer
#we need passing the post data to the creation instance before commited to the DB
def create(self, validated_data):
validated_data['macAddress']= self.context['request'].data.get('macAddress')
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)
print('validate spc', sistemaxCli)
sistemaxCli = sistemas_por_cliente.objects.get(id_sistema=sistema,cliente=client)
except sistemas_por_cliente.DoesNotExist:
raise serializers.ValidationError('No existe licencia para este sistmea y/o cliente')
if sistemaxCli.num_licencias <= Device.objects.filter(sistema=sistemaxCli.id_sistema).count():