api.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. from __future__ import unicode_literals
  2. from django.conf import settings
  3. from django.contrib.contenttypes.models import ContentType
  4. from rest_framework import authentication, exceptions
  5. from rest_framework.compat import is_authenticated
  6. from rest_framework.exceptions import APIException
  7. from rest_framework.pagination import LimitOffsetPagination
  8. from rest_framework.permissions import BasePermission, DjangoModelPermissions, SAFE_METHODS
  9. from rest_framework.serializers import Field, ModelSerializer, ValidationError
  10. from rest_framework.views import get_view_name as drf_get_view_name
  11. from users.models import Token
  12. WRITE_OPERATIONS = ['create', 'update', 'partial_update', 'delete']
  13. class ServiceUnavailable(APIException):
  14. status_code = 503
  15. default_detail = "Service temporarily unavailable, please try again later."
  16. #
  17. # Authentication
  18. #
  19. class TokenAuthentication(authentication.TokenAuthentication):
  20. """
  21. A custom authentication scheme which enforces Token expiration times.
  22. """
  23. model = Token
  24. def authenticate_credentials(self, key):
  25. model = self.get_model()
  26. try:
  27. token = model.objects.select_related('user').get(key=key)
  28. except model.DoesNotExist:
  29. raise exceptions.AuthenticationFailed("Invalid token")
  30. # Enforce the Token's expiration time, if one has been set.
  31. if token.is_expired:
  32. raise exceptions.AuthenticationFailed("Token expired")
  33. if not token.user.is_active:
  34. raise exceptions.AuthenticationFailed("User inactive")
  35. return token.user, token
  36. class TokenPermissions(DjangoModelPermissions):
  37. """
  38. Custom permissions handler which extends the built-in DjangoModelPermissions to validate a Token's write ability
  39. for unsafe requests (POST/PUT/PATCH/DELETE).
  40. """
  41. def __init__(self):
  42. # LOGIN_REQUIRED determines whether read-only access is provided to anonymous users.
  43. self.authenticated_users_only = settings.LOGIN_REQUIRED
  44. super(TokenPermissions, self).__init__()
  45. def has_permission(self, request, view):
  46. # If token authentication is in use, verify that the token allows write operations (for unsafe methods).
  47. if request.method not in SAFE_METHODS and isinstance(request.auth, Token):
  48. if not request.auth.write_enabled:
  49. return False
  50. return super(TokenPermissions, self).has_permission(request, view)
  51. class IsAuthenticatedOrLoginNotRequired(BasePermission):
  52. """
  53. Returns True if the user is authenticated or LOGIN_REQUIRED is False.
  54. """
  55. def has_permission(self, request, view):
  56. if not settings.LOGIN_REQUIRED:
  57. return True
  58. return request.user and is_authenticated(request.user)
  59. #
  60. # Serializers
  61. #
  62. class ValidatedModelSerializer(ModelSerializer):
  63. """
  64. Extends the built-in ModelSerializer to enforce calling clean() on the associated model during validation.
  65. """
  66. def validate(self, data):
  67. # Remove custom field data (if any) prior to model validation
  68. attrs = data.copy()
  69. attrs.pop('custom_fields', None)
  70. # Run clean() on an instance of the model
  71. if self.instance is None:
  72. instance = self.Meta.model(**attrs)
  73. else:
  74. instance = self.instance
  75. for k, v in attrs.items():
  76. setattr(instance, k, v)
  77. instance.clean()
  78. return data
  79. class ChoiceFieldSerializer(Field):
  80. """
  81. Represent a ChoiceField as {'value': <DB value>, 'label': <string>}.
  82. """
  83. def __init__(self, choices, **kwargs):
  84. self._choices = dict()
  85. for k, v in choices:
  86. # Unpack grouped choices
  87. if type(v) in [list, tuple]:
  88. for k2, v2 in v:
  89. self._choices[k2] = v2
  90. else:
  91. self._choices[k] = v
  92. super(ChoiceFieldSerializer, self).__init__(**kwargs)
  93. def to_representation(self, obj):
  94. return {'value': obj, 'label': self._choices[obj]}
  95. def to_internal_value(self, data):
  96. return self._choices.get(data)
  97. class ContentTypeFieldSerializer(Field):
  98. """
  99. Represent a ContentType as '<app_label>.<model>'
  100. """
  101. def to_representation(self, obj):
  102. return "{}.{}".format(obj.app_label, obj.model)
  103. def to_internal_value(self, data):
  104. app_label, model = data.split('.')
  105. try:
  106. return ContentType.objects.get_by_natural_key(app_label=app_label, model=model)
  107. except ContentType.DoesNotExist:
  108. raise ValidationError("Invalid content type")
  109. #
  110. # Mixins
  111. #
  112. class WritableSerializerMixin(object):
  113. """
  114. Allow for the use of an alternate, writable serializer class for write operations (e.g. POST, PUT).
  115. """
  116. def get_serializer_class(self):
  117. if self.action in WRITE_OPERATIONS and hasattr(self, 'write_serializer_class'):
  118. return self.write_serializer_class
  119. return self.serializer_class
  120. #
  121. # Pagination
  122. #
  123. class OptionalLimitOffsetPagination(LimitOffsetPagination):
  124. """
  125. Override the stock paginator to allow setting limit=0 to disable pagination for a request. This returns all objects
  126. matching a query, but retains the same format as a paginated request. The limit can only be disabled if
  127. MAX_PAGE_SIZE has been set to 0 or None.
  128. """
  129. def paginate_queryset(self, queryset, request, view=None):
  130. try:
  131. self.count = queryset.count()
  132. except (AttributeError, TypeError):
  133. self.count = len(queryset)
  134. self.limit = self.get_limit(request)
  135. self.offset = self.get_offset(request)
  136. self.request = request
  137. if self.limit and self.count > self.limit and self.template is not None:
  138. self.display_page_controls = True
  139. if self.count == 0 or self.offset > self.count:
  140. return list()
  141. if self.limit:
  142. return list(queryset[self.offset:self.offset + self.limit])
  143. else:
  144. return list(queryset[self.offset:])
  145. def get_limit(self, request):
  146. if self.limit_query_param:
  147. try:
  148. limit = int(request.query_params[self.limit_query_param])
  149. if limit < 0:
  150. raise ValueError()
  151. # Enforce maximum page size, if defined
  152. if settings.MAX_PAGE_SIZE:
  153. if limit == 0:
  154. return settings.MAX_PAGE_SIZE
  155. else:
  156. return min(limit, settings.MAX_PAGE_SIZE)
  157. return limit
  158. except (KeyError, ValueError):
  159. pass
  160. return self.default_limit
  161. #
  162. # Miscellaneous
  163. #
  164. def get_view_name(view_cls, suffix=None):
  165. """
  166. Derive the view name from its associated model, if it has one. Fall back to DRF's built-in `get_view_name`.
  167. """
  168. if hasattr(view_cls, 'queryset'):
  169. name = view_cls.queryset.model._meta.verbose_name
  170. name = ' '.join([w[0].upper() + w[1:] for w in name.split()]) # Capitalize each word
  171. if suffix:
  172. name = "{} {}".format(name, suffix)
  173. return name
  174. return drf_get_view_name(view_cls, suffix)