Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Pragati1610/care
Browse files Browse the repository at this point in the history
  • Loading branch information
Pragati1610 committed Sep 8, 2022
2 parents 24b246c + e9b5f76 commit 08db30b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
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
18 changes: 18 additions & 0 deletions care/facility/api/viewsets/daily_round.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django_filters import rest_framework as filters
from dry_rest_permissions.generics import DRYPermissions
from rest_framework import mixins
from rest_framework.decorators import action
Expand All @@ -16,6 +17,20 @@
DailyRoundAttributes = [f.name for f in DailyRound._meta.get_fields()]


class DailyRoundFilterSet(filters.FilterSet):
rounds_type = filters.CharFilter(method="filter_rounds_type")

def filter_rounds_type(self, queryset, name, value):
rounds_type = set()
values = value.split(",")
for v in values:
try:
rounds_type.add(DailyRound.RoundsTypeDict[v])
except KeyError:
pass
return queryset.filter(rounds_type__in=list(rounds_type))


class DailyRoundsViewSet(
AssetUserAccessMixin,
mixins.CreateModelMixin,
Expand All @@ -31,6 +46,9 @@ class DailyRoundsViewSet(
)
queryset = DailyRound.objects.all().order_by("-id")
lookup_field = "external_id"
filterset_class = DailyRoundFilterSet

filter_backends = (filters.DjangoFilterBackend,)

FIELDS_KEY = "fields"
MAX_FIELDS = 20
Expand Down
2 changes: 1 addition & 1 deletion care/facility/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Status(enum.Enum):
is_working = models.BooleanField(default=None, null=True, blank=True)
not_working_reason = models.CharField(max_length=1024, blank=True, null=True)
serial_number = models.CharField(max_length=1024, blank=True, null=True)
warranty_details = models.TextField(null=True, blank=True, default="")
warranty_details = models.TextField(null=True, blank=True, default="") # Deprecated
meta = JSONField(default=dict, blank=True, validators=[JSONFieldSchemaValidator(ASSET_META)])
# Vendor Details
vendor_name = models.CharField(max_length=1024, blank=True, null=True)
Expand Down
2 changes: 2 additions & 0 deletions care/facility/models/daily_round.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from multiselectfield import MultiSelectField

from care.facility.models import CATEGORY_CHOICES, PatientBaseModel
from care.facility.models.base import covert_choice_dict
from care.facility.models.bed import AssetBed
from care.facility.models.json_schema.daily_round import (
BLOOD_PRESSURE,
Expand All @@ -31,6 +32,7 @@ class RoundsType(enum.Enum):
AUTOMATED = 300

RoundsTypeChoice = [(e.value, e.name) for e in RoundsType]
RoundsTypeDict = covert_choice_dict(RoundsTypeChoice)

class ConsciousnessType(enum.Enum):
UNKNOWN = 0
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 08db30b

Please sign in to comment.