2015-07-07 10:09:35 +02:00
|
|
|
from collections import OrderedDict
|
2015-02-04 00:08:38 +01:00
|
|
|
|
2015-09-16 00:55:27 +02:00
|
|
|
from rest_framework import status # noqa
|
2015-07-01 23:18:48 +02:00
|
|
|
from rest_framework.decorators import detail_route, list_route # noqa
|
2015-06-17 18:32:05 +02:00
|
|
|
from rest_framework.metadata import SimpleMetadata # noqa
|
2015-10-24 19:02:43 +02:00
|
|
|
from rest_framework.mixins import ( # noqa
|
|
|
|
DestroyModelMixin,
|
|
|
|
ListModelMixin,
|
|
|
|
RetrieveModelMixin,
|
|
|
|
UpdateModelMixin,
|
|
|
|
)
|
2015-06-16 10:37:23 +02:00
|
|
|
from rest_framework.response import Response # noqa
|
|
|
|
from rest_framework.routers import DefaultRouter
|
2015-07-07 10:09:35 +02:00
|
|
|
from rest_framework.serializers import ModelSerializer as _ModelSerializer
|
2015-02-12 18:48:14 +01:00
|
|
|
from rest_framework.serializers import ( # noqa
|
2015-07-07 10:09:35 +02:00
|
|
|
MANY_RELATION_KWARGS,
|
2016-09-12 11:05:34 +02:00
|
|
|
BooleanField,
|
2015-02-12 18:48:14 +01:00
|
|
|
CharField,
|
2015-06-14 23:26:06 +02:00
|
|
|
DictField,
|
2015-02-18 01:45:39 +01:00
|
|
|
Field,
|
2015-09-06 14:12:34 +02:00
|
|
|
FileField,
|
2015-04-30 19:13:28 +02:00
|
|
|
IntegerField,
|
2015-06-14 23:26:06 +02:00
|
|
|
ListField,
|
2015-02-12 18:48:14 +01:00
|
|
|
ListSerializer,
|
2015-07-07 10:09:35 +02:00
|
|
|
ManyRelatedField,
|
2015-02-12 18:48:14 +01:00
|
|
|
PrimaryKeyRelatedField,
|
|
|
|
RelatedField,
|
2015-02-12 20:57:05 +01:00
|
|
|
SerializerMethodField,
|
2015-06-16 10:37:23 +02:00
|
|
|
ValidationError,
|
|
|
|
)
|
2015-07-01 23:18:48 +02:00
|
|
|
from rest_framework.viewsets import GenericViewSet as _GenericViewSet # noqa
|
2015-06-16 10:37:23 +02:00
|
|
|
from rest_framework.viewsets import ModelViewSet as _ModelViewSet # noqa
|
2015-07-01 23:18:48 +02:00
|
|
|
from rest_framework.viewsets import ViewSet as _ViewSet # noqa
|
2014-10-11 14:15:42 +02:00
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
router = DefaultRouter()
|
2015-01-17 14:01:44 +01:00
|
|
|
|
|
|
|
|
2015-07-07 10:09:35 +02:00
|
|
|
class IdManyRelatedField(ManyRelatedField):
|
|
|
|
"""
|
|
|
|
ManyRelatedField that appends an suffix to the sub-fields.
|
|
|
|
|
|
|
|
Only works together with the IdPrimaryKeyRelatedField and our
|
|
|
|
ModelSerializer.
|
|
|
|
"""
|
|
|
|
field_name_suffix = '_id'
|
|
|
|
|
|
|
|
def bind(self, field_name, parent):
|
|
|
|
"""
|
|
|
|
Called when the field is bound to the serializer.
|
|
|
|
|
|
|
|
See IdPrimaryKeyRelatedField for more informations.
|
|
|
|
"""
|
|
|
|
self.source = field_name[:-len(self.field_name_suffix)]
|
|
|
|
super().bind(field_name, parent)
|
|
|
|
|
|
|
|
|
|
|
|
class IdPrimaryKeyRelatedField(PrimaryKeyRelatedField):
|
|
|
|
"""
|
|
|
|
Field, that renames the field name to FIELD_NAME_id.
|
|
|
|
|
|
|
|
Only works together the our ModelSerializer.
|
|
|
|
"""
|
|
|
|
field_name_suffix = '_id'
|
|
|
|
|
|
|
|
def bind(self, field_name, parent):
|
|
|
|
"""
|
|
|
|
Called when the field is bound to the serializer.
|
|
|
|
|
|
|
|
Changes the source so that the original field name is used (removes
|
|
|
|
the _id suffix).
|
|
|
|
"""
|
|
|
|
if field_name:
|
|
|
|
# field_name is an empty string when the field is created with the
|
|
|
|
# attribute many=True. In this case the suffix is added with the
|
|
|
|
# IdManyRelatedField class.
|
|
|
|
self.source = field_name[:-len(self.field_name_suffix)]
|
|
|
|
super().bind(field_name, parent)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def many_init(cls, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Method from rest_framework.relations.RelatedField That uses our
|
|
|
|
IdManyRelatedField class instead of
|
|
|
|
rest_framework.relations.ManyRelatedField class.
|
|
|
|
"""
|
|
|
|
list_kwargs = {'child_relation': cls(*args, **kwargs)}
|
|
|
|
for key in kwargs.keys():
|
|
|
|
if key in MANY_RELATION_KWARGS:
|
|
|
|
list_kwargs[key] = kwargs[key]
|
|
|
|
return IdManyRelatedField(**list_kwargs)
|
|
|
|
|
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
class PermissionMixin:
|
2015-06-12 21:08:57 +02:00
|
|
|
"""
|
2015-07-01 23:18:48 +02:00
|
|
|
Mixin for subclasses of APIView like GenericViewSet and ModelViewSet.
|
|
|
|
|
2016-09-17 22:26:23 +02:00
|
|
|
The method check_view_permissions is evaluated. If it returns False
|
|
|
|
self.permission_denied() is called. Django REST Framework's permission
|
|
|
|
system is disabled.
|
2015-07-01 23:18:48 +02:00
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
Also connects container to handle access permissions for model and
|
|
|
|
viewset.
|
|
|
|
"""
|
|
|
|
access_permissions = None
|
2016-02-11 11:29:19 +01:00
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
def get_permissions(self):
|
2015-06-12 21:08:57 +02:00
|
|
|
"""
|
2016-09-17 22:26:23 +02:00
|
|
|
Overridden method to check view permissions. Returns an empty
|
|
|
|
iterable so Django REST framework won't do any other permission
|
|
|
|
checks by evaluating Django REST framework style permission classes
|
|
|
|
and the request passes.
|
2015-06-12 21:08:57 +02:00
|
|
|
"""
|
2016-09-17 22:26:23 +02:00
|
|
|
if not self.check_view_permissions():
|
2015-07-01 23:18:48 +02:00
|
|
|
self.permission_denied(self.request)
|
|
|
|
return ()
|
2015-06-12 21:08:57 +02:00
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
def check_view_permissions(self):
|
|
|
|
"""
|
|
|
|
Override this and return True if the requesting user should be able to
|
|
|
|
get access to your view.
|
2016-02-11 22:58:32 +01:00
|
|
|
|
2016-09-17 22:26:23 +02:00
|
|
|
Don't forget to use access permissions container for list and retrieve
|
|
|
|
requests.
|
2015-07-01 23:18:48 +02:00
|
|
|
"""
|
|
|
|
return False
|
2015-06-12 21:08:57 +02:00
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
def get_access_permissions(self):
|
|
|
|
"""
|
|
|
|
Returns a container to handle access permissions for this viewset and
|
|
|
|
its corresponding model.
|
|
|
|
"""
|
|
|
|
return self.access_permissions
|
|
|
|
|
|
|
|
def get_serializer_class(self):
|
|
|
|
"""
|
|
|
|
Overridden method to return the serializer class given by the
|
|
|
|
access permissions container.
|
|
|
|
"""
|
|
|
|
if self.get_access_permissions() is not None:
|
|
|
|
serializer_class = self.get_access_permissions().get_serializer_class(self.request.user)
|
|
|
|
else:
|
|
|
|
serializer_class = super().get_serializer_class()
|
|
|
|
return serializer_class
|
|
|
|
|
2015-06-12 21:08:57 +02:00
|
|
|
|
2016-02-11 11:29:19 +01:00
|
|
|
class ModelSerializer(_ModelSerializer):
|
|
|
|
"""
|
|
|
|
ModelSerializer that changes the field names of related fields to
|
|
|
|
FIELD_NAME_id.
|
|
|
|
"""
|
|
|
|
serializer_related_field = IdPrimaryKeyRelatedField
|
|
|
|
|
|
|
|
def get_fields(self):
|
|
|
|
"""
|
|
|
|
Returns all fields of the serializer.
|
|
|
|
"""
|
|
|
|
fields = OrderedDict()
|
|
|
|
|
|
|
|
for field_name, field in super().get_fields().items():
|
|
|
|
try:
|
|
|
|
field_name += field.field_name_suffix
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
fields[field_name] = field
|
|
|
|
return fields
|
|
|
|
|
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
class GenericViewSet(PermissionMixin, _GenericViewSet):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ModelViewSet(PermissionMixin, _ModelViewSet):
|
2016-02-11 22:58:32 +01:00
|
|
|
pass
|
2015-07-01 23:18:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ViewSet(PermissionMixin, _ViewSet):
|
|
|
|
pass
|