/usr/lib/python2.7/dist-packages/djoser/views.py is in python-djoser 0.4.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | from django.contrib.auth import get_user_model, user_logged_in, user_logged_out
from rest_framework import generics, permissions, status, response, views
from rest_framework.authtoken.models import Token
from rest_framework.response import Response
from rest_framework.reverse import reverse
from django.contrib.auth.tokens import default_token_generator
from . import serializers, settings, utils, signals
User = get_user_model()
class RootView(views.APIView):
"""
Root endpoint - use one of sub endpoints.
"""
permission_classes = (
permissions.AllowAny,
)
urls_mapping = {
'me': 'user',
'register': 'register',
'activate': 'activate',
'change-' + User.USERNAME_FIELD: 'set_username',
'change-password': 'set_password',
'password-reset': 'password_reset',
'password-reset-confirm': 'password_reset_confirm',
}
urls_extra_mapping = None
def get_urls_mapping(self, **kwargs):
mapping = self.urls_mapping.copy()
mapping.update(kwargs)
if self.urls_extra_mapping:
mapping.update(self.urls_extra_mapping)
mapping.update(settings.get('ROOT_VIEW_URLS_MAPPING'))
return mapping
def get(self, request, format=None):
return Response(
dict([(key, reverse(url_name, request=request, format=format))
for key, url_name in self.get_urls_mapping().items()])
)
class RegistrationView(utils.SendEmailViewMixin, generics.CreateAPIView):
"""
Use this endpoint to register new user.
"""
serializer_class = serializers.UserRegistrationSerializer
permission_classes = (
permissions.AllowAny,
)
token_generator = default_token_generator
subject_template_name = 'activation_email_subject.txt'
plain_body_template_name = 'activation_email_body.txt'
def perform_create(self, serializer):
instance = serializer.save()
signals.user_registered.send(sender=self.__class__, user=instance, request=self.request)
if settings.get('SEND_ACTIVATION_EMAIL'):
self.send_email(**self.get_send_email_kwargs(instance))
def get_email_context(self, user):
context = super(RegistrationView, self).get_email_context(user)
context['url'] = settings.get('ACTIVATION_URL').format(**context)
return context
class LoginView(utils.ActionViewMixin, generics.GenericAPIView):
"""
Use this endpoint to obtain user authentication token.
"""
serializer_class = serializers.LoginSerializer
permission_classes = (
permissions.AllowAny,
)
def action(self, serializer):
user = serializer.user
token, _ = Token.objects.get_or_create(user=user)
user_logged_in.send(sender=user.__class__, request=self.request, user=user)
return Response(
data=serializers.TokenSerializer(token).data,
status=status.HTTP_200_OK,
)
class LogoutView(views.APIView):
"""
Use this endpoint to logout user (remove user authentication token).
"""
permission_classes = (
permissions.IsAuthenticated,
)
def post(self, request):
Token.objects.filter(user=request.user).delete()
user_logged_out.send(sender=request.user.__class__, request=request, user=request.user)
return response.Response(status=status.HTTP_200_OK)
class PasswordResetView(utils.ActionViewMixin, utils.SendEmailViewMixin, generics.GenericAPIView):
"""
Use this endpoint to send email to user with password reset link.
"""
serializer_class = serializers.PasswordResetSerializer
permission_classes = (
permissions.AllowAny,
)
token_generator = default_token_generator
subject_template_name = 'password_reset_email_subject.txt'
plain_body_template_name = 'password_reset_email_body.txt'
def action(self, serializer):
for user in self.get_users(serializer.data['email']):
self.send_email(**self.get_send_email_kwargs(user))
return response.Response(status=status.HTTP_200_OK)
def get_users(self, email):
active_users = User._default_manager.filter(
email__iexact=email,
is_active=True,
)
return (u for u in active_users if u.has_usable_password())
def get_email_context(self, user):
context = super(PasswordResetView, self).get_email_context(user)
context['url'] = settings.get('PASSWORD_RESET_CONFIRM_URL').format(**context)
return context
class SetPasswordView(utils.ActionViewMixin, generics.GenericAPIView):
"""
Use this endpoint to change user password.
"""
permission_classes = (
permissions.IsAuthenticated,
)
def get_serializer_class(self):
if settings.get('SET_PASSWORD_RETYPE'):
return serializers.SetPasswordRetypeSerializer
return serializers.SetPasswordSerializer
def action(self, serializer):
self.request.user.set_password(serializer.data['new_password'])
self.request.user.save()
return response.Response(status=status.HTTP_200_OK)
class PasswordResetConfirmView(utils.ActionViewMixin, generics.GenericAPIView):
"""
Use this endpoint to finish reset password process.
"""
permission_classes = (
permissions.AllowAny,
)
token_generator = default_token_generator
def get_serializer_class(self):
if settings.get('PASSWORD_RESET_CONFIRM_RETYPE'):
return serializers.PasswordResetConfirmRetypeSerializer
return serializers.PasswordResetConfirmSerializer
def action(self, serializer):
serializer.user.set_password(serializer.data['new_password'])
serializer.user.save()
return response.Response(status=status.HTTP_200_OK)
class ActivationView(utils.ActionViewMixin, generics.GenericAPIView):
"""
Use this endpoint to activate user account.
"""
serializer_class = serializers.UidAndTokenSerializer
permission_classes = (
permissions.AllowAny,
)
token_generator = default_token_generator
def action(self, serializer):
serializer.user.is_active = True
serializer.user.save()
signals.user_activated.send(
sender=self.__class__, user=serializer.user, request=self.request)
return Response(status=status.HTTP_200_OK)
class SetUsernameView(utils.ActionViewMixin, generics.GenericAPIView):
"""
Use this endpoint to change user username.
"""
serializer_class = serializers.SetUsernameSerializer
permission_classes = (
permissions.IsAuthenticated,
)
def get_serializer_class(self):
if settings.get('SET_USERNAME_RETYPE'):
return serializers.SetUsernameRetypeSerializer
return serializers.SetUsernameSerializer
def action(self, serializer):
setattr(self.request.user, User.USERNAME_FIELD, serializer.data['new_' + User.USERNAME_FIELD])
self.request.user.save()
return response.Response(status=status.HTTP_200_OK)
class UserView(generics.RetrieveUpdateAPIView):
"""
Use this endpoint to retrieve/update user.
"""
model = User
serializer_class = serializers.UserSerializer
permission_classes = (
permissions.IsAuthenticated,
)
def get_object(self, *args, **kwargs):
return self.request.user
|