Commit 1ff6c45b authored by Florent Chehab's avatar Florent Chehab

Backend updated for #48

parent 364be4d4
from rest_framework import viewsets
class DictModeViewSet(viewsets.ModelViewSet):
"""
ViewSet that renders data as dict with keys corresponding to the model
primary key. Instead of list.
"""
BYPASS_DICT_MODE = False
LIST_SHOULD_BE_DETAIL = False
def list(self, request, *args, **kwargs):
response = super(viewsets.ModelViewSet, self).list( # pylint: disable=E1003
request, *args, **kwargs
) # call the original 'list'
if self.LIST_SHOULD_BE_DETAIL:
if len(response.data) == 0:
response.data = dict()
elif len(response.data) == 1:
response.data = response.data[0]
else:
raise Exception(
"There should be no more than one element here check your queryset !"
)
elif not self.BYPASS_DICT_MODE:
pk_attr_name = self.serializer_class.Meta.model._meta.pk.name
response.data = {d[pk_attr_name]: d for d in response.data}
return response
from .DictModeViewSet import DictModeViewSet
from .mySerializerWithJSON import MySerializerWithJSON
__all__ = ["DictModeViewSet", "MySerializerWithJSON"]
__all__ = ["MySerializerWithJSON"]
from .myModelSerializer import MyModelSerializer
from backend_app.permissions import DEFAULT_VIEWSET_PERMISSIONS
from backend_app.custom import DictModeViewSet
from rest_framework import viewsets
class MyModelViewSet(DictModeViewSet):
class MyModelViewSet(viewsets.ModelViewSet):
serializer_class = MyModelSerializer
permission_classes = DEFAULT_VIEWSET_PERMISSIONS
LIST_SHOULD_BE_DETAIL = False
def list(self, request, *args, **kwargs):
response = super(viewsets.ModelViewSet, self).list( # pylint: disable=E1003
request, *args, **kwargs
) # call the original 'list'
if self.LIST_SHOULD_BE_DETAIL:
if len(response.data) == 0:
response.data = dict()
elif len(response.data) == 1:
response.data = response.data[0]
else:
raise Exception(
"There should be no more than one element in the queryset, check it !"
)
return response
def get_queryset(self):
"""
Extended default rest framework behavior
......
......@@ -33,8 +33,6 @@ class CampusTaggedItemViewSet(TaggedItemViewSet):
queryset = CampusTaggedItem.objects.all() # pylint: disable=E1101
serializer_class = CampusTaggedItemSerializer
BYPASS_DICT_MODE = True
def extend_queryset(self):
campus_id = self.kwargs["campus_id"]
return self.my_model_queryset.filter(campus=campus_id).distinct()
......@@ -34,8 +34,6 @@ class CityTaggedItemViewSet(TaggedItemViewSet):
queryset = CityTaggedItem.objects.all() # pylint: disable=E1101
serializer_class = CityTaggedItemSerializer
BYPASS_DICT_MODE = True
def extend_queryset(self):
city_id = self.kwargs["city_id"]
return self.my_model_queryset.filter(city=city_id).distinct()
......@@ -30,8 +30,6 @@ class CountryDriViewSet(BasicModuleViewSet):
queryset = CountryDri.objects.all() # pylint: disable=E1101
serializer_class = CountryDriSerializer
BYPASS_DICT_MODE = True
def extend_queryset(self):
country_id = self.kwargs["country_id"]
return self.my_model_queryset.filter(countries__pk=country_id).distinct()
......@@ -28,8 +28,6 @@ class CountryScholarshipViewSet(ScholarshipViewSet):
queryset = CountryScholarship.objects.all() # pylint: disable=E1101
serializer_class = CountryScholarshipSerializer
BYPASS_DICT_MODE = True
def extend_queryset(self):
country_id = self.kwargs["country_id"]
return self.my_model_queryset.filter(countries__pk=country_id).distinct()
......@@ -33,8 +33,6 @@ class CountryTaggedItemViewSet(TaggedItemViewSet):
queryset = CountryTaggedItem.objects.all() # pylint: disable=E1101
serializer_class = CountryTaggedItemSerializer
BYPASS_DICT_MODE = True
def extend_queryset(self):
country_id = self.kwargs["country_id"]
return self.my_model_queryset.filter(country=country_id).distinct()
......@@ -30,8 +30,6 @@ class UniversityDriViewSet(BasicModuleViewSet):
queryset = UniversityDri.objects.all() # pylint: disable=E1101
serializer_class = UniversityDriSerializer
BYPASS_DICT_MODE = True
def extend_queryset(self):
univ_id = self.kwargs["univ_id"]
return self.my_model_queryset.filter(universities__pk=univ_id).distinct()
......@@ -32,8 +32,6 @@ class UniversityScholarshipViewSet(ScholarshipViewSet):
queryset = UniversityScholarship.objects.all() # pylint: disable=E1101
serializer_class = UniversityScholarshipSerializer
BYPASS_DICT_MODE = True
def extend_queryset(self):
univ_id = self.kwargs["univ_id"]
return self.my_model_queryset.filter(universities__pk=univ_id).distinct()
......@@ -33,8 +33,6 @@ class UniversityTaggedItemViewSet(TaggedItemViewSet):
queryset = UniversityTaggedItem.objects.all() # pylint: disable=E1101
serializer_class = UniversityTaggedItemSerializer
BYPASS_DICT_MODE = True
def extend_queryset(self):
univ_id = self.kwargs["univ_id"]
return self.my_model_queryset.filter(university__pk=univ_id).distinct()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment