api.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from django.conf import settings
  2. from django.contrib.contenttypes.models import ContentType
  3. from rest_framework import authentication, exceptions
  4. from rest_framework.exceptions import APIException
  5. from rest_framework.permissions import DjangoModelPermissions, SAFE_METHODS
  6. from rest_framework.serializers import Field, ValidationError
  7. from users.models import Token
  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. class TokenAuthentication(authentication.TokenAuthentication):
  13. """
  14. A custom authentication scheme which enforces Token expiration times.
  15. """
  16. model = Token
  17. def authenticate_credentials(self, key):
  18. model = self.get_model()
  19. try:
  20. token = model.objects.select_related('user').get(key=key)
  21. except model.DoesNotExist:
  22. raise exceptions.AuthenticationFailed("Invalid token")
  23. # Enforce the Token's expiration time, if one has been set.
  24. if token.expires and not token.is_expired:
  25. raise exceptions.AuthenticationFailed("Token expired")
  26. if not token.user.is_active:
  27. raise exceptions.AuthenticationFailed("User inactive")
  28. return token.user, token
  29. class TokenPermissions(DjangoModelPermissions):
  30. """
  31. Custom permissions handler which extends the built-in DjangoModelPermissions to validate a Token's write ability
  32. for unsafe requests (POST/PUT/PATCH/DELETE).
  33. """
  34. def __init__(self):
  35. # LOGIN_REQUIRED determines whether read-only access is provided to anonymous users.
  36. self.authenticated_users_only = settings.LOGIN_REQUIRED
  37. super(TokenPermissions, self).__init__()
  38. def has_permission(self, request, view):
  39. # If token authentication is in use, verify that the token allows write operations (for unsafe methods).
  40. if request.method not in SAFE_METHODS and isinstance(request.auth, Token):
  41. if not request.auth.write_enabled:
  42. return False
  43. return super(TokenPermissions, self).has_permission(request, view)
  44. class ChoiceFieldSerializer(Field):
  45. """
  46. Represent a ChoiceField as {'value': <DB value>, 'label': <string>}.
  47. """
  48. def __init__(self, choices, **kwargs):
  49. self._choices = dict()
  50. for k, v in choices:
  51. # Unpack grouped choices
  52. if type(v) in [list, tuple]:
  53. for k2, v2 in v:
  54. self._choices[k2] = v2
  55. else:
  56. self._choices[k] = v
  57. super(ChoiceFieldSerializer, self).__init__(**kwargs)
  58. def to_representation(self, obj):
  59. return {'value': obj, 'label': self._choices[obj]}
  60. def to_internal_value(self, data):
  61. return self._choices.get(data)
  62. class ContentTypeFieldSerializer(Field):
  63. """
  64. Represent a ContentType as '<app_label>.<model>'
  65. """
  66. def to_representation(self, obj):
  67. return "{}.{}".format(obj.app_label, obj.model)
  68. def to_internal_value(self, data):
  69. app_label, model = data.split('.')
  70. try:
  71. return ContentType.objects.get_by_natural_key(app_label=app_label, model=model)
  72. except ContentType.DoesNotExist:
  73. raise ValidationError("Invalid content type")
  74. class WritableSerializerMixin(object):
  75. """
  76. Allow for the use of an alternate, writable serializer class for write operations (e.g. POST, PUT).
  77. """
  78. def get_serializer_class(self):
  79. if self.action in WRITE_OPERATIONS and hasattr(self, 'write_serializer_class'):
  80. return self.write_serializer_class
  81. return self.serializer_class