api.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from __future__ import unicode_literals
  2. from django.conf import settings
  3. from django.contrib.contenttypes.models import ContentType
  4. from rest_framework.compat import is_authenticated
  5. from rest_framework.exceptions import APIException
  6. from rest_framework.permissions import BasePermission
  7. from rest_framework.serializers import Field, ModelSerializer, ValidationError
  8. WRITE_OPERATIONS = ['create', 'update', 'partial_update', 'delete']
  9. class ServiceUnavailable(APIException):
  10. status_code = 503
  11. default_detail = "Service temporarily unavailable, please try again later."
  12. #
  13. # Authentication
  14. #
  15. class IsAuthenticatedOrLoginNotRequired(BasePermission):
  16. """
  17. Returns True if the user is authenticated or LOGIN_REQUIRED is False.
  18. """
  19. def has_permission(self, request, view):
  20. if not settings.LOGIN_REQUIRED:
  21. return True
  22. return request.user and is_authenticated(request.user)
  23. #
  24. # Serializers
  25. #
  26. class ValidatedModelSerializer(ModelSerializer):
  27. """
  28. Extends the built-in ModelSerializer to enforce calling clean() on the associated model during validation.
  29. """
  30. def validate(self, data):
  31. # Remove custom field data (if any) prior to model validation
  32. attrs = data.copy()
  33. attrs.pop('custom_fields', None)
  34. # Run clean() on an instance of the model
  35. if self.instance is None:
  36. instance = self.Meta.model(**attrs)
  37. else:
  38. instance = self.instance
  39. for k, v in attrs.items():
  40. setattr(instance, k, v)
  41. instance.clean()
  42. return data
  43. class ChoiceFieldSerializer(Field):
  44. """
  45. Represent a ChoiceField as {'value': <DB value>, 'label': <string>}.
  46. """
  47. def __init__(self, choices, **kwargs):
  48. self._choices = dict()
  49. for k, v in choices:
  50. # Unpack grouped choices
  51. if type(v) in [list, tuple]:
  52. for k2, v2 in v:
  53. self._choices[k2] = v2
  54. else:
  55. self._choices[k] = v
  56. super(ChoiceFieldSerializer, self).__init__(**kwargs)
  57. def to_representation(self, obj):
  58. return {'value': obj, 'label': self._choices[obj]}
  59. def to_internal_value(self, data):
  60. return self._choices.get(data)
  61. class ContentTypeFieldSerializer(Field):
  62. """
  63. Represent a ContentType as '<app_label>.<model>'
  64. """
  65. def to_representation(self, obj):
  66. return "{}.{}".format(obj.app_label, obj.model)
  67. def to_internal_value(self, data):
  68. app_label, model = data.split('.')
  69. try:
  70. return ContentType.objects.get_by_natural_key(app_label=app_label, model=model)
  71. except ContentType.DoesNotExist:
  72. raise ValidationError("Invalid content type")
  73. #
  74. # Mixins
  75. #
  76. class WritableSerializerMixin(object):
  77. """
  78. Allow for the use of an alternate, writable serializer class for write operations (e.g. POST, PUT).
  79. """
  80. def get_serializer_class(self):
  81. if self.action in WRITE_OPERATIONS and hasattr(self, 'write_serializer_class'):
  82. return self.write_serializer_class
  83. return self.serializer_class