Skip to content

Commit

Permalink
Fixed limits quantity validation of shelfobject
Browse files Browse the repository at this point in the history
  • Loading branch information
Kejebo committed Dec 6, 2023
1 parent e32ba8f commit 3201c04
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/laboratory/api/shelfobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 23 additions & 6 deletions src/laboratory/shelfobject/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
3 changes: 3 additions & 0 deletions src/locale/es/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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."

0 comments on commit 3201c04

Please sign in to comment.