diff --git a/src/laboratory/api/shelfobject.py b/src/laboratory/api/shelfobject.py index ac0f76d2f..b83902062 100644 --- a/src/laboratory/api/shelfobject.py +++ b/src/laboratory/api/shelfobject.py @@ -468,10 +468,13 @@ def create_shelfobject(self, request, org_pk, lab_pk, **kwargs): serializer = self.serializer_class['serializer'](data=request.data, context={"organization_id": org_pk, "laboratory_id": lab_pk}) - limit_serializer = ShelfObjectLimitsSerializer(data=request.data) + limit_serializer = ShelfObjectLimitsSerializer(data=request.data, + context={'type':request.data.get('objecttype',-1), + 'quantity': request.data.get('quantity',0), + 'without_limit':request.data.get('without_limit',False)}) errors = {} if serializer.is_valid(): - if limit_serializer.is_valid(raise_exception=True): + if limit_serializer.is_valid(): shelfobject = self.serializer_class['method'](serializer, limit_serializer) create_shelfobject_observation(shelfobject, shelfobject.course_name, diff --git a/src/laboratory/shelfobject/serializers.py b/src/laboratory/shelfobject/serializers.py index a6f7af777..def9a53d4 100644 --- a/src/laboratory/shelfobject/serializers.py +++ b/src/laboratory/shelfobject/serializers.py @@ -265,12 +265,29 @@ class Meta: fields = '__all__' def validate(self, data): - if data["minimum_limit"] > data["maximum_limit"]: - logger.debug( - f'ShelfObjectLimitsSerializer --> data["minimum_limit"] ({data["minimum_limit"]}) > ' - f'data["maximum_limit"] ({data["maximum_limit"]})') - raise serializers.ValidationError({"minimum_limit": _( - "Minimum limit cannot be greater than maximum limit.")}) + quantity= self.context.get('quantity',0) + type_id = self.context.get('type_id','0') + without_limit = self.context.get('without_limit',False) + errors = {} + + if type_id==Object.REACTIVE and not without_limit: + if data["minimum_limit"] > data["maximum_limit"]: + logger.debug( + f'ShelfObjectLimitsSerializer --> data["minimum_limit"] ({data["minimum_limit"]}) > ' + f'data["maximum_limit"] ({data["maximum_limit"]})') + errors.update({"minimum_limit": + _("Minimum limit cannot be greater than maximum limit.")}) + + if float(quantity) > data["maximum_limit"]: + logger.debug( + f'ShelfObjectLimitsSerializer --> shelfobject.quantity ({quantity}) > ' + f'({data["maximum_limit"]})') + errors.update({"quantity": _( + "Quantity cannot be greater than maximum limit.")}) + + if errors: + raise serializers.ValidationError(errors) + return data diff --git a/src/locale/es/LC_MESSAGES/django.po b/src/locale/es/LC_MESSAGES/django.po index 98a0aede7..cca76b2ea 100644 --- a/src/locale/es/LC_MESSAGES/django.po +++ b/src/locale/es/LC_MESSAGES/django.po @@ -4173,3 +4173,6 @@ msgstr "Eliminar organización" msgid "This action will delete its descendants organizations too." msgstr "Esta acción también eliminará sus organizaciones descendientes." + +msgid "Quantity cannot be greater than maximum limit." +msgstr "La cantidad no puede mayor que el límite maximo."