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

Commit

Permalink
Revert "Fix: Removed unnecessary user_core.core DB Accesses"
Browse files Browse the repository at this point in the history
This reverts commit 800a312.
  • Loading branch information
MeisterSeSe committed Nov 2, 2023
1 parent 800a312 commit 374d347
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 40 deletions.
42 changes: 4 additions & 38 deletions backend/core/fileupload/serializers.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
import json

from django.core.cache.utils import make_template_fragment_key
from django.core.exceptions import ObjectDoesNotExist
from django.core.files.base import ContentFile
from django.utils.functional import cached_property

from core.fileupload.models import Family, Tag, License, File, Analysis, AnalysisResult
from rest_framework import serializers
from django.http import QueryDict

from core.user.models import User
from transpiler.g6_transpiler import xml_to_g6
from core.fileupload.utils import generate_random_string
from django.core.cache import cache

class FamiliesSerializer(serializers.ModelSerializer):
"""
A serializer for defining which Feature Model Family attributes should be converted to JSON
"""
def get_owner_email(self, instance):
cache_key = make_template_fragment_key('owner_email', [instance.pk])
owner_email = cache.get(cache_key)
if owner_email is None:
owner_email = instance.owner.email if instance.owner else ''
cache.set(cache_key, owner_email, timeout=None)
return owner_email

owner = serializers.SerializerMethodField(method_name='get_owner_email')
owner = serializers.ReadOnlyField(source='owner.email')

class Meta:
model = Family
Expand All @@ -47,15 +33,7 @@ class TagsSerializer(serializers.ModelSerializer):
"""
A serializer for defining which Tag attributes should be converted to JSON
"""
def get_owner_email(self, instance):
cache_key = make_template_fragment_key('owner_email', [instance.pk])
owner_email = cache.get(cache_key)
if owner_email is None:
owner_email = instance.owner.email if instance.owner else ''
cache.set(cache_key, owner_email, timeout=None) # Timeout None bedeutet, dass der Cache nicht abläuft
return owner_email

owner = serializers.SerializerMethodField(method_name='get_owner_email')
owner = serializers.ReadOnlyField(source='owner.email')

class Meta:
model = Tag
Expand All @@ -66,26 +44,14 @@ class FilesSerializer(serializers.ModelSerializer):
"""
A serializer for defining which file attributes should be converted to JSON
"""

def get_owner_email(self, instance):
cache_key = make_template_fragment_key('owner_email', [instance.pk])
owner_email = cache.get(cache_key)
if owner_email is None:
owner_email = instance.owner.email if instance.owner else ''
cache.set(cache_key, owner_email, timeout=None) # Timeout None bedeutet, dass der Cache nicht abläuft
return owner_email

owner = serializers.SerializerMethodField(method_name='get_owner_email')
owner = serializers.ReadOnlyField(source='owner.email')
# For further relations on serializers:
# https://www.django-rest-framework.org/api-guide/relations
tags = TagsSerializer(many=True)

license = LicensesSerializer()

family = FamiliesSerializer()
license = LicensesSerializer()
# version = 'self'


class Meta:
model = File
fields = ['id', 'label', 'description', 'local_file', 'family', 'license', 'tags', 'owner', 'uploaded_at',
Expand Down
3 changes: 1 addition & 2 deletions backend/core/fileupload/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,6 @@ class ConfirmedFileViewSet(
serializer_class = FilesSerializer
permission_classes = [IsAuthenticatedOrReadOnly, IsOwnerOrIsAdminOrReadOnly]


def list(self, request, **kwargs):
"""
Replace email address of file owner with True or False,
Expand All @@ -431,7 +430,7 @@ def list(self, request, **kwargs):
filter_conditions &= Q(family__id=family_id)
if owner is not None:
filter_conditions &= Q(owner=owner)
queryset = (queryset.filter(filter_conditions).select_related('family', 'license', 'owner').
queryset = (queryset.filter(filter_conditions).select_related('family', 'license').
prefetch_related('tags').order_by("version"))

anonymized_files = [anonymize_file(FilesSerializer(file).data, request) for file in queryset]
Expand Down

0 comments on commit 374d347

Please sign in to comment.