Performed some general linting. More is needed along with cleanup
Some checks failed
Lint / Lint (push) Failing after 22s
Some checks failed
Lint / Lint (push) Failing after 22s
This commit is contained in:
@@ -8,56 +8,70 @@ from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.settings import api_settings
|
||||
from rest_framework import mixins
|
||||
from starfields_drf_generics.cache_mixins import CacheGetMixin, CacheSetMixin, CacheDeleteMixin
|
||||
from starfields_drf_generics.cache_mixins import (
|
||||
CacheGetMixin, CacheSetMixin, CacheDeleteMixin)
|
||||
|
||||
|
||||
# Mixin classes to be included in the generic classes
|
||||
class CachedCreateModelMixin(CacheDeleteMixin, mixins.CreateModelMixin):
|
||||
"""
|
||||
A slightly modified version of rest_framework.mixins.CreateModelMixin that handles cache deletions.
|
||||
A slightly modified version of rest_framework.mixins.CreateModelMixin
|
||||
that handles cache deletions.
|
||||
"""
|
||||
|
||||
def create(self, request, *args, **kwargs):
|
||||
" Creates the entry in the request "
|
||||
# Go on with the creation as normal
|
||||
serializer = self.get_serializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
self.perform_create(serializer)
|
||||
headers = self.get_success_headers(serializer.data)
|
||||
|
||||
|
||||
# Delete the cache
|
||||
self.delete_cache(request)
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED,
|
||||
headers=headers)
|
||||
|
||||
|
||||
class CachedRetrieveModelMixin(CacheGetMixin, CacheSetMixin):
|
||||
"""
|
||||
A slightly modified version of rest_framework.mixins.RetrieveModelMixin that handles cache attempts.
|
||||
mixins.RetrieveModelMixin only has the retrieve method so it doesn't stand to inherit anything from it.
|
||||
A slightly modified version of rest_framework.mixins.RetrieveModelMixin
|
||||
that handles cache attempts.
|
||||
mixins.RetrieveModelMixin only has the retrieve method so it doesn't stand
|
||||
to inherit anything from it.
|
||||
"""
|
||||
def retrieve(self, request, *args, **kwargs):
|
||||
|
||||
def retrieve(self, request):
|
||||
" Retrieves the entry in the request "
|
||||
# Attempt to get the request from the cache
|
||||
cache_attempt = self.get_cache(request)
|
||||
|
||||
|
||||
if cache_attempt:
|
||||
return Response(cache_attempt)
|
||||
else:
|
||||
# The cache get attempt failed so we have to get the results from the database
|
||||
instance = self.get_object()
|
||||
|
||||
serializer = self.get_serializer(instance)
|
||||
response = Response(serializer.data)
|
||||
|
||||
self.set_cache(request, response)
|
||||
return response
|
||||
|
||||
# The cache get attempt failed so we have to get the results from
|
||||
# the database
|
||||
instance = self.get_object()
|
||||
|
||||
serializer = self.get_serializer(instance)
|
||||
response = Response(serializer.data)
|
||||
|
||||
self.set_cache(request, response)
|
||||
return response
|
||||
|
||||
|
||||
class CachedUpdateModelMixin(CacheDeleteMixin, mixins.UpdateModelMixin):
|
||||
"""
|
||||
A slightly modified version of rest_framework.mixins.UpdateModelMixin that handles cache deletes.
|
||||
A slightly modified version of rest_framework.mixins.UpdateModelMixin that
|
||||
handles cache deletes.
|
||||
"""
|
||||
|
||||
def update(self, request, *args, **kwargs):
|
||||
" Updates the entry in the request "
|
||||
partial = kwargs.pop('partial', False)
|
||||
instance = self.get_object()
|
||||
serializer = self.get_serializer(instance, data=request.data, partial=partial)
|
||||
serializer = self.get_serializer(instance, data=request.data,
|
||||
partial=partial)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
self.perform_update(serializer)
|
||||
|
||||
@@ -65,7 +79,7 @@ class CachedUpdateModelMixin(CacheDeleteMixin, mixins.UpdateModelMixin):
|
||||
# If 'prefetch_related' has been applied to a queryset, we need to
|
||||
# forcibly invalidate the prefetch cache on the instance.
|
||||
instance._prefetched_objects_cache = {}
|
||||
|
||||
|
||||
# Delete the related caches
|
||||
self.delete_cache(request)
|
||||
|
||||
@@ -74,15 +88,18 @@ class CachedUpdateModelMixin(CacheDeleteMixin, mixins.UpdateModelMixin):
|
||||
|
||||
class CachedDestroyModelMixin(CacheDeleteMixin, mixins.DestroyModelMixin):
|
||||
"""
|
||||
A slightly modified version of rest_framework.mixins.DestroyModelMixin that handles cache deletes.
|
||||
A slightly modified version of rest_framework.mixins.DestroyModelMixin
|
||||
that handles cache deletes.
|
||||
"""
|
||||
|
||||
def destroy(self, request, *args, **kwargs):
|
||||
" Deletes the entry in the request "
|
||||
instance = self.get_object()
|
||||
self.perform_destroy(instance)
|
||||
|
||||
|
||||
# Delete the related caches
|
||||
self.delete_cache(request)
|
||||
|
||||
|
||||
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
|
||||
@@ -91,21 +108,26 @@ class CachedListCreateModelMixin(CacheDeleteMixin):
|
||||
"""
|
||||
A fully custom mixin that handles mutiple instance cration.
|
||||
"""
|
||||
def list_create(self, request, *args, **kwargs):
|
||||
|
||||
def list_create(self, request):
|
||||
" Creates the list of entries in the request "
|
||||
# Go on with the creation as normal
|
||||
serializer = self.get_serializer(data=request.data, many=True)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
self.perform_create(serializer)
|
||||
headers = self.get_success_headers(serializer.data)
|
||||
|
||||
|
||||
# Delete the cache
|
||||
self.delete_cache(request)
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED,
|
||||
headers=headers)
|
||||
|
||||
def perform_create(self, serializer):
|
||||
" Generic save hook "
|
||||
serializer.save()
|
||||
|
||||
def get_success_headers(self, data):
|
||||
" Returns extra success headers "
|
||||
try:
|
||||
return {'Location': str(data[api_settings.URL_FIELD_NAME])}
|
||||
except (TypeError, KeyError):
|
||||
@@ -114,31 +136,36 @@ class CachedListCreateModelMixin(CacheDeleteMixin):
|
||||
|
||||
class CachedListRetrieveModelMixin(CacheGetMixin, CacheSetMixin):
|
||||
"""
|
||||
A slightly modified version of rest_framework.mixins.ListModelMixin that handles cache saves.
|
||||
mixins.ListModelMixin only has the list method so it doesn't stand to inherit anything from it.
|
||||
A slightly modified version of rest_framework.mixins.ListModelMixin that
|
||||
handles cache saves.
|
||||
mixins.ListModelMixin only has the list method so it doesn't stand to
|
||||
inherit anything from it.
|
||||
"""
|
||||
def list(self, request, *args, **kwargs):
|
||||
|
||||
def list(self, request):
|
||||
" Retrieves the listing of entries "
|
||||
# Attempt to get the request from the cache
|
||||
cache_attempt = self.get_cache(request)
|
||||
|
||||
|
||||
if cache_attempt:
|
||||
return Response(cache_attempt)
|
||||
else:
|
||||
# The cache get attempt failed so we have to get the results from the database
|
||||
queryset = self.filter_queryset(self.get_queryset())
|
||||
|
||||
if self.paged:
|
||||
page = self.paginate_queryset(queryset)
|
||||
if page is not None:
|
||||
serializer = self.get_serializer(page, many=True)
|
||||
response = self.get_paginated_response(serializer.data)
|
||||
else:
|
||||
serializer = self.get_serializer(queryset, many=True)
|
||||
response = Response(serializer.data)
|
||||
|
||||
# The cache get attempt failed so we have to get the results from
|
||||
# the database
|
||||
queryset = self.filter_queryset(self.get_queryset())
|
||||
|
||||
if self.paged:
|
||||
page = self.paginate_queryset(queryset)
|
||||
if page is not None:
|
||||
serializer = self.get_serializer(page, many=True)
|
||||
response = self.get_paginated_response(serializer.data)
|
||||
else:
|
||||
serializer = self.get_serializer(queryset, many=True)
|
||||
response = Response(serializer.data)
|
||||
|
||||
else:
|
||||
serializer = self.get_serializer(queryset, many=True)
|
||||
response = Response(serializer.data)
|
||||
|
||||
self.set_cache(request, response)
|
||||
return response
|
||||
|
||||
@@ -147,12 +174,15 @@ class CachedListUpdateModelMixin(CacheDeleteMixin):
|
||||
"""
|
||||
A fully custom mixin that handles mutiple instance updates.
|
||||
"""
|
||||
def list_update(self, request, *args, **kwargs):
|
||||
|
||||
def list_update(self, request, **kwargs):
|
||||
" Updates the list of entries in the request "
|
||||
partial = kwargs.pop('partial', False)
|
||||
|
||||
|
||||
queryset = self.filter_queryset(self.get_queryset())
|
||||
|
||||
serializer = self.get_serializer(queryset, data=request.data, partial=partial, many=True)
|
||||
|
||||
serializer = self.get_serializer(queryset, data=request.data,
|
||||
partial=partial, many=True)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
self.perform_update(serializer)
|
||||
|
||||
@@ -162,9 +192,11 @@ class CachedListUpdateModelMixin(CacheDeleteMixin):
|
||||
return Response(serializer.data)
|
||||
|
||||
def perform_update(self, serializer):
|
||||
" Generic save hook "
|
||||
serializer.save()
|
||||
|
||||
def list_partial_update(self, request, *args, **kwargs):
|
||||
" Needs to be called on partial updates "
|
||||
kwargs['partial'] = True
|
||||
return self.list_update(request, *args, **kwargs)
|
||||
|
||||
@@ -173,39 +205,22 @@ class CachedListDestroyModelMixin(CacheDeleteMixin):
|
||||
"""
|
||||
A fully custom mixin that handles mutiple instance deletions.
|
||||
"""
|
||||
def list_destroy(self, request, *args, **kwargs):
|
||||
|
||||
def list_destroy(self, request):
|
||||
" Deletes the list of entries in the request "
|
||||
# Go on with the validation as normal
|
||||
serializer = self.get_serializer(data=request.data, many=True)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
validated_data = serializer.validated_data
|
||||
|
||||
|
||||
# TODO does this new stuff work even? need to check on the frontend
|
||||
serializer.delete(validated_data)
|
||||
|
||||
|
||||
# for instance in self.get_objects():
|
||||
# if instance is not None:
|
||||
# self.perform_destroy(instance)
|
||||
|
||||
# Delete the related caches
|
||||
self.delete_cache(request)
|
||||
|
||||
|
||||
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
#def perform_destroy(self, instance):
|
||||
# instance.delete()
|
||||
|
||||
#def get_objects(self):
|
||||
# """
|
||||
# The custom list version of get_object that retrieves one instance from the #database. It yields model instances with each call.
|
||||
# """
|
||||
# queryset = self.filter_queryset(self.get_queryset())
|
||||
#
|
||||
# if len(queryset):
|
||||
# for obj in queryset.all():
|
||||
|
||||
# # May raise a permission denied
|
||||
# self.check_object_permissions(self.request, obj)
|
||||
|
||||
# yield obj
|
||||
|
||||
#yield None
|
||||
|
||||
Reference in New Issue
Block a user