Skip to content

Commit

Permalink
Add public asset fetch endpoint (#998)
Browse files Browse the repository at this point in the history
* Add public asset fetch endpoint

* Remove filter

* Add caching, remove permissions

* Remove unused import
  • Loading branch information
Ashesh3 authored Sep 5, 2022
1 parent 81823a9 commit 99f3732
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions care/facility/api/serializers/asset.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from re import L
from django.core.cache import cache
from django.db import transaction
from django.shortcuts import get_object_or_404
from rest_framework.exceptions import ValidationError
Expand Down Expand Up @@ -75,6 +76,11 @@ def update(self, instance, validated_data):
updated_instance = super().update(instance, validated_data)
return updated_instance

def save(self, **kwargs):
cache_key = "asset:" + str(self.instance.external_id)
cache.delete(cache_key)
return super().save(**kwargs)


class AssetBareMinimumSerializer(ModelSerializer):
id = UUIDField(source="external_id", read_only=True)
Expand Down
17 changes: 17 additions & 0 deletions care/facility/api/viewsets/asset.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.core.cache import cache
from django.db.models import Q
from django.http import Http404
from django.shortcuts import get_object_or_404
Expand Down Expand Up @@ -98,6 +99,22 @@ class AssetFilter(filters.FilterSet):
qr_code_id = filters.CharFilter(field_name="qr_code_id", lookup_expr="icontains")


class AssetPublicViewSet(GenericViewSet):
queryset = Asset.objects.all()
serializer_class = AssetSerializer
lookup_field = "external_id"

def retrieve(self, request, *args, **kwargs):
key = "asset:" + kwargs["external_id"]
hit = cache.get(key)
if not hit:
instance = self.get_object()
serializer = self.get_serializer(instance)
cache.set(key, serializer.data, 60 * 60 * 24) # Cache the asset details for 24 hours
return Response(serializer.data)
return Response(hit)


class AssetViewSet(
ListModelMixin,
RetrieveModelMixin,
Expand Down
4 changes: 4 additions & 0 deletions config/api_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
AssetLocationViewSet,
AssetTransactionViewSet,
AssetViewSet,
AssetPublicViewSet
)
from care.facility.api.viewsets.bed import (
AssetBedViewSet,
Expand Down Expand Up @@ -187,6 +188,9 @@
consultation_nested_router.register(r"daily_rounds", DailyRoundsViewSet)
consultation_nested_router.register(r"investigation", InvestigationValueViewSet)

# Public endpoints
router.register("public/asset", AssetPublicViewSet)

app_name = "api"
urlpatterns = [
url(r"^", include(router.urls)),
Expand Down

0 comments on commit 99f3732

Please sign in to comment.