| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- from __future__ import unicode_literals
- from django.conf import settings
- from django.contrib.contenttypes.models import ContentType
- from rest_framework.compat import is_authenticated
- from rest_framework.exceptions import APIException
- from rest_framework.permissions import BasePermission
- from rest_framework.serializers import Field, ModelSerializer, ValidationError
- WRITE_OPERATIONS = ['create', 'update', 'partial_update', 'delete']
- class ServiceUnavailable(APIException):
- status_code = 503
- default_detail = "Service temporarily unavailable, please try again later."
- #
- # Authentication
- #
- class IsAuthenticatedOrLoginNotRequired(BasePermission):
- """
- Returns True if the user is authenticated or LOGIN_REQUIRED is False.
- """
- def has_permission(self, request, view):
- if not settings.LOGIN_REQUIRED:
- return True
- return request.user and is_authenticated(request.user)
- #
- # Serializers
- #
- class ValidatedModelSerializer(ModelSerializer):
- """
- Extends the built-in ModelSerializer to enforce calling clean() on the associated model during validation.
- """
- def validate(self, data):
- # Remove custom field data (if any) prior to model validation
- attrs = data.copy()
- attrs.pop('custom_fields', None)
- # Run clean() on an instance of the model
- if self.instance is None:
- instance = self.Meta.model(**attrs)
- else:
- instance = self.instance
- for k, v in attrs.items():
- setattr(instance, k, v)
- instance.clean()
- return data
- class ChoiceFieldSerializer(Field):
- """
- Represent a ChoiceField as {'value': <DB value>, 'label': <string>}.
- """
- def __init__(self, choices, **kwargs):
- self._choices = dict()
- for k, v in choices:
- # Unpack grouped choices
- if type(v) in [list, tuple]:
- for k2, v2 in v:
- self._choices[k2] = v2
- else:
- self._choices[k] = v
- super(ChoiceFieldSerializer, self).__init__(**kwargs)
- def to_representation(self, obj):
- return {'value': obj, 'label': self._choices[obj]}
- def to_internal_value(self, data):
- return self._choices.get(data)
- class ContentTypeFieldSerializer(Field):
- """
- Represent a ContentType as '<app_label>.<model>'
- """
- def to_representation(self, obj):
- return "{}.{}".format(obj.app_label, obj.model)
- def to_internal_value(self, data):
- app_label, model = data.split('.')
- try:
- return ContentType.objects.get_by_natural_key(app_label=app_label, model=model)
- except ContentType.DoesNotExist:
- raise ValidationError("Invalid content type")
- #
- # Mixins
- #
- class WritableSerializerMixin(object):
- """
- Allow for the use of an alternate, writable serializer class for write operations (e.g. POST, PUT).
- """
- def get_serializer_class(self):
- if self.action in WRITE_OPERATIONS and hasattr(self, 'write_serializer_class'):
- return self.write_serializer_class
- return self.serializer_class
|