diff --git a/care/facility/api/viewsets/asset.py b/care/facility/api/viewsets/asset.py index d1b7446bc4..5f6cea0f24 100644 --- a/care/facility/api/viewsets/asset.py +++ b/care/facility/api/viewsets/asset.py @@ -1,5 +1,3 @@ -import uuid - from django.conf import settings from django.core.cache import cache from django.db.models import Exists, OuterRef, Q @@ -192,24 +190,15 @@ class AssetPublicQRViewSet(GenericViewSet): lookup_field = "qr_code_id" def retrieve(self, request, *args, **kwargs): - is_uuid = True - try: - uuid.UUID(kwargs["qr_code_id"]) - except ValueError: - # If the qr_code_id is not a UUID, then it is the pk of the asset - is_uuid = False - if not kwargs["qr_code_id"].isnumeric(): - return Response(status=status.HTTP_404_NOT_FOUND) - - key = "asset:qr:" + kwargs["qr_code_id"] + qr_code_id = kwargs["qr_code_id"] + key = "asset:qr:" + qr_code_id hit = cache.get(key) if not hit: - if is_uuid: - instance = self.get_object() - else: - instance = get_object_or_404( - self.get_queryset(), pk=kwargs["qr_code_id"] - ) + instance = self.get_queryset().filter(qr_code_id=qr_code_id).first() + if not instance: # If the asset is not found, try to find it by pk + if not qr_code_id.isnumeric(): + return Response(status=status.HTTP_404_NOT_FOUND) + instance = get_object_or_404(self.get_queryset(), pk=qr_code_id) serializer = self.get_serializer(instance) cache.set(key, serializer.data, 60 * 60 * 24) return Response(serializer.data)