Refactoring base api classes.
parent
68f0dd7928
commit
3a08114eea
|
@ -1,10 +1,49 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from django.db import transaction
|
||||||
|
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets
|
||||||
|
from rest_framework import status
|
||||||
|
from rest_framework import mixins
|
||||||
|
from rest_framework.response import Response
|
||||||
|
|
||||||
from .pagination import HeadersPaginationMixin, ConditionalPaginationMixin
|
from .pagination import HeadersPaginationMixin, ConditionalPaginationMixin
|
||||||
|
|
||||||
|
|
||||||
|
class AtomicMixin(object):
|
||||||
|
@transaction.atomic
|
||||||
|
def post(self, *args, **kwargs):
|
||||||
|
return super().post(*args, **kwargs)
|
||||||
|
|
||||||
|
@transaction.atomic
|
||||||
|
def delete(self, *args, **kwargs):
|
||||||
|
return super().delete(*args, **kwargs)
|
||||||
|
|
||||||
|
@transaction.atomic
|
||||||
|
def put(self, *args, **kwargs):
|
||||||
|
return super().put(*args, **kwargs)
|
||||||
|
|
||||||
|
@transaction.atomic
|
||||||
|
def patch(self, *args, **kwargs):
|
||||||
|
return super().patch(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class DestroyModelMixin(object):
|
||||||
|
"""
|
||||||
|
Self version of DestroyModelMixin with
|
||||||
|
pre_delete hook method.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def pre_delete(self, obj):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def destroy(self, request, *args, **kwargs):
|
||||||
|
obj = self.get_object()
|
||||||
|
self.pre_delete(obj)
|
||||||
|
obj.delete()
|
||||||
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||||
|
|
||||||
|
|
||||||
class PreconditionMixin(object):
|
class PreconditionMixin(object):
|
||||||
def pre_conditions_on_save(self, obj):
|
def pre_conditions_on_save(self, obj):
|
||||||
pass
|
pass
|
||||||
|
@ -21,14 +60,21 @@ class PreconditionMixin(object):
|
||||||
self.pre_conditions_on_delete(obj)
|
self.pre_conditions_on_delete(obj)
|
||||||
|
|
||||||
|
|
||||||
class ModelCrudViewSet(PreconditionMixin,
|
class ModelCrudViewSet(AtomicMixin,
|
||||||
|
PreconditionMixin,
|
||||||
HeadersPaginationMixin,
|
HeadersPaginationMixin,
|
||||||
ConditionalPaginationMixin,
|
ConditionalPaginationMixin,
|
||||||
viewsets.ModelViewSet):
|
mixins.CreateModelMixin,
|
||||||
|
mixins.RetrieveModelMixin,
|
||||||
|
mixins.UpdateModelMixin,
|
||||||
|
DestroyModelMixin,
|
||||||
|
mixins.ListModelMixin,
|
||||||
|
viewsets.GenericViewSet):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ModelListViewSet(PreconditionMixin,
|
class ModelListViewSet(AtomicMixin,
|
||||||
|
PreconditionMixin,
|
||||||
HeadersPaginationMixin,
|
HeadersPaginationMixin,
|
||||||
ConditionalPaginationMixin,
|
ConditionalPaginationMixin,
|
||||||
viewsets.ReadOnlyModelViewSet):
|
viewsets.ReadOnlyModelViewSet):
|
||||||
|
|
Loading…
Reference in New Issue