Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Feat: Implemented Caching for Families and Confirmed Models
Browse files Browse the repository at this point in the history
  • Loading branch information
MeisterSeSe committed Oct 16, 2023
1 parent 526ef9e commit 527af63
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions backend/core/fileupload/viewsets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.core.cache import cache
from django.db.models import Q
from django.utils.decorators import method_decorator

from core.fileupload.models import Family, Tag, License, File, Analysis, AnalysisResult
from core.fileupload.utils import generate_random_string
Expand Down Expand Up @@ -39,6 +41,7 @@
from multiprocessing import Process
from rest_framework.views import APIView
from django.http.request import QueryDict
from django.views.decorators.cache import cache_page

import json
import os
Expand Down Expand Up @@ -204,16 +207,16 @@ class BulkUploadApiView(UploadApiView):
def post(self, request, format=None):
if not request.data["files"]:
return Response(
{"files": "This field may not be blank."},
status.HTTP_400_BAD_REQUEST,
)
{"files": "This field may not be blank."},
status.HTTP_400_BAD_REQUEST,
)
try:
files = json.loads(request.data["files"])
except:
return Response(
{"files": "This field must contain JSON."},
status.HTTP_400_BAD_REQUEST,
)
{"files": "This field must contain JSON."},
status.HTTP_400_BAD_REQUEST,
)

serializers = []
confirmation_token = generate_random_string(30)
Expand Down Expand Up @@ -266,16 +269,16 @@ class ZipUploadApiView(UploadApiView):
def post(self, request, format=None):
if not request.data["files"]:
return Response(
{"files": "This field may not be blank."},
status.HTTP_400_BAD_REQUEST,
)
{"files": "This field may not be blank."},
status.HTTP_400_BAD_REQUEST,
)
try:
file_data = json.loads(request.data["files"])
except:
return Response(
{"files": "This field must contain JSON."},
status.HTTP_400_BAD_REQUEST,
)
{"files": "This field must contain JSON."},
status.HTTP_400_BAD_REQUEST,
)

label = file_data["label"]
description = file_data["description"]
Expand All @@ -301,7 +304,7 @@ def post(self, request, format=None):
validated_data["label"] = label
validated_data["description"] = description
validated_data["license"] = license
validated_data["version"] = f"{i+1}.0.0"
validated_data["version"] = f"{i + 1}.0.0"
validated_data["family"] = family
validated_data.setlist("tags", tags)
validated_data["local_file"] = local_file
Expand Down Expand Up @@ -384,10 +387,22 @@ def list(self, request, **kwargs):
Replace email address of file owner with True or False,
indicating if the user which has sent the request is the owner.
"""
queryset = File.objects.filter(is_confirmed=True)

family_id = self.request.query_params.get("family")
owner = self.request.query_params.get("owner")
if family_id is not None and owner is not None:
cache_key = f"confirmed_files_{family_id}_{owner}"
elif family_id is not None:
cache_key = f"confirmed_files_{family_id}_all"
elif owner is not None:
cache_key = f"confirmed_files_all_{owner}"
else:
cache_key = "confirmed_files_all"

cached_data = cache.get(cache_key)
if cached_data is not None:
return Response(cached_data)
queryset = File.objects.filter(is_confirmed=True)
filter_conditions = Q(is_confirmed=True)
if family_id is not None:
filter_conditions &= Q(family__id=family_id)
Expand All @@ -397,6 +412,7 @@ def list(self, request, **kwargs):
prefetch_related('tags').order_by("version"))

anonymized_files = [anonymize_file(FilesSerializer(file).data, request) for file in queryset]
cache.set(cache_key, anonymized_files, 60 * 15)
return Response(anonymized_files)

def retrieve(self, request, *args, **kwargs):
Expand Down Expand Up @@ -434,11 +450,15 @@ def anonymize_family(self, family, request):
return anonymized_family

def list(self, request, **kwargs):
cached_families = cache.get("families_cache_key")
if cached_families is not None:
return Response(cached_families)
queryset = Family.objects.all()
families = FamiliesSerializer(queryset, many=True).data
anonymized_families = []
for family in families:
anonymized_families.append(self.anonymize_family(family, request))
cache.set("families_cache_key", anonymized_families, 60 * 15)
return Response(anonymized_families)

def retrieve(self, request, *args, **kwargs):
Expand All @@ -449,6 +469,7 @@ def retrieve(self, request, *args, **kwargs):

def perform_create(self, serializer):
serializer.save(owner=self.request.user)
cache.delete("families_cache_key")


class LicensesViewSet(
Expand Down Expand Up @@ -524,14 +545,14 @@ def retrieve(self, request, *args, **kwargs):
def perform_create(self, serializer):
serializer.save(owner=self.request.user)


class AnalysesViewSet(
viewsets.GenericViewSet,
mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.DestroyModelMixin,
mixins.ListModelMixin,
):

queryset = Analysis.objects.all()
serializer_class = AnalysesSerializer
permission_classes = [
Expand All @@ -543,12 +564,12 @@ def list(self, request, **kwargs):
analyses = AnalysesSerializer(queryset, many=True).data
return Response(analyses)


class AnalysisResultsViewSet(
viewsets.GenericViewSet,
mixins.RetrieveModelMixin,
mixins.ListModelMixin,
):

queryset = AnalysisResult.objects.all()
serializer_class = AnalysisResultsSerializer

Expand Down

0 comments on commit 527af63

Please sign in to comment.