Skip to content

Commit

Permalink
Add error message when límit of shelf object is greater than exist
Browse files Browse the repository at this point in the history
  • Loading branch information
luisza committed Jan 8, 2024
2 parents 827d994 + 2e15355 commit c01514d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
7 changes: 5 additions & 2 deletions src/laboratory/api/shelfobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,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_id':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','-1')
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
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_create_shelfobject_material(self):
"measurement_unit": 59,
"marked_as_discard": False,
"minimum_limit": 0,
"maximum_limit": 0,
"maximum_limit": 40,
"container_select_option": "clone",
"container_for_cloning":material.id,
"available_container":""
Expand Down
17 changes: 10 additions & 7 deletions src/laboratory/tests/shelfobject/test_create_shelfobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ def test_create_shelfobject_material(self):
"course_name": "A reactive product",
"marked_as_discard": False,
"minimum_limit": 0,
"maximum_limit": 0,
"maximum_limit": 24,
}
precount = ShelfObject.objects.filter(shelf=13).count()
url = reverse("laboratory:api-shelfobject-create-shelfobject",
kwargs={"org_pk": self.org_pk, "lab_pk": self.lab.pk})
response = self.client.post(url, data=data, content_type='application/json')
poscount = ShelfObject.objects.filter(shelf=13).count()

self.assertEqual(response.status_code,201)
self.assertTrue(json.loads(response.content)['detail'], _("The creation was performed successfully."))
self.assertTrue(poscount>precount)
Expand Down Expand Up @@ -117,7 +118,7 @@ def test_create_shelfobject_reactive(self):
"measurement_unit": 59,
"marked_as_discard": False,
"minimum_limit": 0,
"maximum_limit": 0,
"maximum_limit": 30,
"container_select_option": "clone",
"container_for_cloning":self.material.object.id,
"available_container":""
Expand Down Expand Up @@ -248,6 +249,7 @@ def test_create_shelfobject_limits_errors(self):
"in_where_laboratory": 1,
"measurement_unit": 59,
"marked_as_discard": False,
"without_limit":True,
"minimum_limit": 39,
"maximum_limit": 5,
"container_select_option": "clone",
Expand All @@ -259,9 +261,8 @@ def test_create_shelfobject_limits_errors(self):
kwargs={"org_pk": self.org_pk, "lab_pk": self.lab.pk})
response = self.client.post(url, data=data, content_type='application/json')
poscount = ShelfObject.objects.filter(shelf=13).count()
self.assertEqual(response.status_code, 400)
self.assertTrue(json.loads(response.content)['minimum_limit'], _("Minimum limit cannot be greater than maximum limit."))
self.assertTrue(poscount == precount)
self.assertEqual(response.status_code, 201)
self.assertTrue(poscount >= precount)

def test_create_shelfobject_other_lab(self):
self.material.type = 1
Expand Down Expand Up @@ -345,6 +346,7 @@ def test_create_shelfobject_shelf_unlimit(self):
"in_where_laboratory": self.lab.pk,
"measurement_unit": 59,
"marked_as_discard": False,
"without_limit":True,
"minimum_limit": 0,
"maximum_limit": 5,
"container": self.material.pk
Expand Down Expand Up @@ -420,6 +422,7 @@ def test_create_shelfobject_shelf_unlimit_units(self):
"measurement_unit": 60,
"marked_as_discard": False,
"minimum_limit": 0,
"without_limit":True,
"maximum_limit": 5,
"container": self.material.pk
}
Expand Down Expand Up @@ -452,7 +455,7 @@ def test_create_discard_shelfobject(self):
"course_name": "A reactive product",
"marked_as_discard": True,
"minimum_limit": 0,
"maximum_limit": 0,
"maximum_limit": 40,
}
precount = ShelfObject.objects.filter(shelf=13).count()
url = reverse("laboratory:api-shelfobject-create-shelfobject",
Expand Down Expand Up @@ -632,7 +635,7 @@ def test_create_shelfobject_object_available_in_shelf(self):
"course_name": "A reactive product",
"marked_as_discard": False,
"minimum_limit": 0,
"maximum_limit": 0,
"maximum_limit": 24,
}
precount = ShelfObject.objects.filter(shelf=13).count()
url = reverse("laboratory:api-shelfobject-create-shelfobject",
Expand Down
8 changes: 4 additions & 4 deletions src/locale/es/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-01-08 09:27-0600\n"
"POT-Creation-Date: 2024-01-08 12:44-0600\n"
"PO-Revision-Date: \n"
"Last-Translator: \n"
"Language-Team: \n"
Expand Down Expand Up @@ -1330,6 +1330,9 @@ msgstr ""
msgid "Minimum limit cannot be greater than maximum limit."
msgstr "Límite mínimo no puede ser mayor al límite máximo."

msgid "Quantity cannot be greater than maximum limit."
msgstr "La cantidad no puede mayor que el límite maximo."

msgid "Infinity"
msgstr "Infinito"

Expand Down Expand Up @@ -2290,9 +2293,6 @@ msgstr "Códigos NFPA"
msgid "UE Codes"
msgstr "Códigos UE"

msgid "Organization Name"
msgstr "Nombre de la organización"

#, python-format
msgid "Quantity cannot be greater than the shelf's quantity limit: %(limit)s."
msgstr ""
Expand Down

0 comments on commit c01514d

Please sign in to comment.