Commit cf83d7a8 authored by Michael Fladischer's avatar Michael Fladischer

Import djoser_0.5.0.orig.tar.gz

parent 582ab86e
Metadata-Version: 1.1
Name: djoser
Version: 0.4.3
Version: 0.5.0
Summary: REST version of Django authentication system.
Home-page: https://github.com/sunscrapers/djoser
Author: SUNSCRAPERS
......@@ -51,15 +51,12 @@ Description: djoser
Supported Python versions:
- Python 2.6
- Python 2.7
- Python 3.4
- Python 3.5
Supported Django versions:
- Django 1.5
- Django 1.6
- Django 1.7
- Django 1.8
- Django 1.9
......@@ -182,7 +179,8 @@ Description: djoser
'PASSWORD_RESET_CONFIRM_URL': '#/password/reset/confirm/{uid}/{token}',
'ACTIVATION_URL': '#/activate/{uid}/{token}',
'SEND_ACTIVATION_EMAIL': True,
'PASSWORD_VALIDATORS': []
'PASSWORD_VALIDATORS': [],
'SERIALIZERS': {},
}
Check "Settings" section for more info.
......@@ -531,6 +529,39 @@ Description: djoser
**Example**: ``[my_validator1, my_validator2]``
SERIALIZERS
~~~~~~~~~~~
This dictionary is used to update the defaults, so by providing, let's
say, one key, all the others will still be used.
**Examples**
::
{
'user': 'myapp.serializers.SpecialUserSerializer',
}
**Default**:
::
{
'activation': 'djoser.serializers.ActivationSerializer',
'login': 'djoser.serializers.LoginSerializer',
'password_reset': 'djoser.serializers.PasswordResetSerializer',
'password_reset_confirm': 'djoser.serializers.PasswordResetConfirmSerializer',
'password_reset_confirm_retype': 'djoser.serializers.PasswordResetConfirmRetypeSerializer',
'set_password': 'djoser.serializers.SetPasswordSerializer',
'set_password_retype': 'djoser.serializers.SetPasswordRetypeSerializer',
'set_username': 'djoser.serializers.SetUsernameSerializer',
'set_username_retype': 'djoser.serializers.SetUsernameRetypeSerializer',
'user_registration': 'djoser.serializers.UserRegistrationSerializer',
'user': 'djoser.serializers.UserSerializer',
'token': 'djoser.serializers.TokenSerializer',
}
Emails
------
......@@ -635,10 +666,13 @@ Description: djoser
Customization
-------------
If you need to override some ``djoser`` behaviour, you could define your
custom view/serializer.
If you need to customize any serializer behavior you can use the
DJOSER['SERIALIZERS'] setting to use your own serializer classes in the
built-in views. Or if you need to completely change the default djoser
behaviour, you can always override djoser views with your own custom
ones.
Define custom urls instead of reusing ``djoser.urls``:
Define custom ``urls`` instead of reusing ``djoser.urls``:
.. code:: python
......
......@@ -35,15 +35,12 @@ Available endpoints:
Supported Python versions:
* Python 2.6
* Python 2.7
* Python 3.4
* Python 3.5
Supported Django versions:
* Django 1.5
* Django 1.6
* Django 1.7
* Django 1.8
* Django 1.9
......@@ -149,7 +146,8 @@ DJOSER = {
'PASSWORD_RESET_CONFIRM_URL': '#/password/reset/confirm/{uid}/{token}',
'ACTIVATION_URL': '#/activate/{uid}/{token}',
'SEND_ACTIVATION_EMAIL': True,
'PASSWORD_VALIDATORS': []
'PASSWORD_VALIDATORS': [],
'SERIALIZERS': {},
}
```
......@@ -456,6 +454,36 @@ These validators are run on `/register/` and `/password/reset/confirm/`.
**Example**: `[my_validator1, my_validator2]`
### SERIALIZERS
This dictionary is used to update the defaults, so by providing,
let's say, one key, all the others will still be used.
**Examples**
```
{
'user': 'myapp.serializers.SpecialUserSerializer',
}
```
**Default**:
```
{
'activation': 'djoser.serializers.ActivationSerializer',
'login': 'djoser.serializers.LoginSerializer',
'password_reset': 'djoser.serializers.PasswordResetSerializer',
'password_reset_confirm': 'djoser.serializers.PasswordResetConfirmSerializer',
'password_reset_confirm_retype': 'djoser.serializers.PasswordResetConfirmRetypeSerializer',
'set_password': 'djoser.serializers.SetPasswordSerializer',
'set_password_retype': 'djoser.serializers.SetPasswordRetypeSerializer',
'set_username': 'djoser.serializers.SetUsernameSerializer',
'set_username_retype': 'djoser.serializers.SetUsernameRetypeSerializer',
'user_registration': 'djoser.serializers.UserRegistrationSerializer',
'user': 'djoser.serializers.UserSerializer',
'token': 'djoser.serializers.TokenSerializer',
}
```
## Emails
There are few email templates which you may want to override:
......@@ -549,9 +577,12 @@ In this extremely short tutorial we are going to mimic the simplest flow: regist
## Customization
If you need to override some `djoser` behaviour, you could define your custom view/serializer.
If you need to customize any serializer behavior you can use
the DJOSER['SERIALIZERS'] setting to use your own serializer classes in the built-in views.
Or if you need to completely change the default djoser behaviour,
you can always override djoser views with your own custom ones.
Define custom urls instead of reusing `djoser.urls`:
Define custom `urls` instead of reusing `djoser.urls`:
```python
urlpatterns = patterns('',
......
Metadata-Version: 1.1
Name: djoser
Version: 0.4.3
Version: 0.5.0
Summary: REST version of Django authentication system.
Home-page: https://github.com/sunscrapers/djoser
Author: SUNSCRAPERS
......@@ -51,15 +51,12 @@ Description: djoser
Supported Python versions:
- Python 2.6
- Python 2.7
- Python 3.4
- Python 3.5
Supported Django versions:
- Django 1.5
- Django 1.6
- Django 1.7
- Django 1.8
- Django 1.9
......@@ -182,7 +179,8 @@ Description: djoser
'PASSWORD_RESET_CONFIRM_URL': '#/password/reset/confirm/{uid}/{token}',
'ACTIVATION_URL': '#/activate/{uid}/{token}',
'SEND_ACTIVATION_EMAIL': True,
'PASSWORD_VALIDATORS': []
'PASSWORD_VALIDATORS': [],
'SERIALIZERS': {},
}
Check "Settings" section for more info.
......@@ -531,6 +529,39 @@ Description: djoser
**Example**: ``[my_validator1, my_validator2]``
SERIALIZERS
~~~~~~~~~~~
This dictionary is used to update the defaults, so by providing, let's
say, one key, all the others will still be used.
**Examples**
::
{
'user': 'myapp.serializers.SpecialUserSerializer',
}
**Default**:
::
{
'activation': 'djoser.serializers.ActivationSerializer',
'login': 'djoser.serializers.LoginSerializer',
'password_reset': 'djoser.serializers.PasswordResetSerializer',
'password_reset_confirm': 'djoser.serializers.PasswordResetConfirmSerializer',
'password_reset_confirm_retype': 'djoser.serializers.PasswordResetConfirmRetypeSerializer',
'set_password': 'djoser.serializers.SetPasswordSerializer',
'set_password_retype': 'djoser.serializers.SetPasswordRetypeSerializer',
'set_username': 'djoser.serializers.SetUsernameSerializer',
'set_username_retype': 'djoser.serializers.SetUsernameRetypeSerializer',
'user_registration': 'djoser.serializers.UserRegistrationSerializer',
'user': 'djoser.serializers.UserSerializer',
'token': 'djoser.serializers.TokenSerializer',
}
Emails
------
......@@ -635,10 +666,13 @@ Description: djoser
Customization
-------------
If you need to override some ``djoser`` behaviour, you could define your
custom view/serializer.
If you need to customize any serializer behavior you can use the
DJOSER['SERIALIZERS'] setting to use your own serializer classes in the
built-in views. Or if you need to completely change the default djoser
behaviour, you can always override djoser views with your own custom
ones.
Define custom urls instead of reusing ``djoser.urls``:
Define custom ``urls`` instead of reusing ``djoser.urls``:
.. code:: python
......
from django.contrib.auth import authenticate, get_user_model
from django.utils import six
from django.utils.module_loading import import_string
from rest_framework import exceptions, serializers
from rest_framework.authtoken.models import Token
from . import constants, utils, settings
User = get_user_model()
......@@ -191,3 +195,25 @@ class TokenSerializer(serializers.ModelSerializer):
fields = (
'auth_token',
)
class SerializersManager(object):
def __init__(self, serializer_confs):
self.serializers = serializer_confs.copy()
def get(self, serializer_name):
try:
if isinstance(self.serializers[serializer_name], six.string_types):
self.serializers[serializer_name] = self.load_serializer(
self.serializers[serializer_name])
return self.serializers[serializer_name]
except KeyError:
raise Exception("Try to use serializer name '%s' that is not one of: %s" % (
serializer_name,
tuple(settings.get('SERIALIZERS').keys())
))
def load_serializer(self, serializer_class):
return import_string(serializer_class)
serializers_manager = SerializersManager(settings.get('SERIALIZERS'))
from copy import deepcopy
from django.core.exceptions import ImproperlyConfigured
from django.conf import settings
default_settings = {
'SEND_ACTIVATION_EMAIL': False,
'SET_PASSWORD_RETYPE': False,
'SET_USERNAME_RETYPE': False,
'PASSWORD_RESET_CONFIRM_RETYPE': False,
'ROOT_VIEW_URLS_MAPPING': {},
'PASSWORD_VALIDATORS': [],
'SERIALIZERS': {
'activation': 'djoser.serializers.ActivationSerializer',
'login': 'djoser.serializers.LoginSerializer',
'password_reset': 'djoser.serializers.PasswordResetSerializer',
'password_reset_confirm': 'djoser.serializers.PasswordResetConfirmSerializer',
'password_reset_confirm_retype': 'djoser.serializers.PasswordResetConfirmRetypeSerializer',
'set_password': 'djoser.serializers.SetPasswordSerializer',
'set_password_retype': 'djoser.serializers.SetPasswordRetypeSerializer',
'set_username': 'djoser.serializers.SetUsernameSerializer',
'set_username_retype': 'djoser.serializers.SetUsernameRetypeSerializer',
'user_registration': 'djoser.serializers.UserRegistrationSerializer',
'user': 'djoser.serializers.UserSerializer',
'token': 'djoser.serializers.TokenSerializer',
},
}
def get(key):
from django.conf import settings
defaults = {
'SEND_ACTIVATION_EMAIL': False,
'SET_PASSWORD_RETYPE': False,
'SET_USERNAME_RETYPE': False,
'PASSWORD_RESET_CONFIRM_RETYPE': False,
'ROOT_VIEW_URLS_MAPPING': {},
'PASSWORD_VALIDATORS': []
}
defaults.update(getattr(settings, 'DJOSER', {}))
user_settings = merge_settings_dicts(
deepcopy(default_settings), getattr(settings, 'DJOSER', {}))
try:
return defaults[key]
return user_settings[key]
except KeyError:
raise ImproperlyConfigured('Missing settings: DJOSER[\'{}\']'.format(key))
def merge_settings_dicts(a, b, path=None, overwrite_conflicts=True):
"""merges b into a, modify a in place
Found at http://stackoverflow.com/a/7205107/1472229
"""
if path is None:
path = []
for key in b:
if key in a:
if isinstance(a[key], dict) and isinstance(b[key], dict):
merge_settings_dicts(a[key], b[key], path + [str(key)], overwrite_conflicts=overwrite_conflicts)
elif a[key] == b[key]:
pass # same leaf value
else:
if overwrite_conflicts:
a[key] = b[key]
else:
conflict_path = '.'.join(path + [str(key)])
raise Exception('Conflict at %s' % conflict_path)
else:
a[key] = b[key]
# Don't let this fool you that a is not modified in place
return a
from django.conf import settings as django_settings
from django.contrib.sites.shortcuts import get_current_site
from django.core.mail import EmailMultiAlternatives, EmailMessage
from django.template import loader
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.utils.encoding import force_bytes, force_text
from rest_framework import response, status
try:
from django.contrib.sites.shortcuts import get_current_site
except ImportError:
from django.contrib.sites.models import get_current_site
def encode_uid(pk):
try:
from django.utils.http import urlsafe_base64_encode
from django.utils.encoding import force_bytes
return urlsafe_base64_encode(force_bytes(pk)).decode()
except ImportError:
from django.utils.http import int_to_base36
return int_to_base36(pk)
return urlsafe_base64_encode(force_bytes(pk)).decode()
def decode_uid(pk):
try:
from django.utils.http import urlsafe_base64_decode
from django.utils.encoding import force_text
return force_text(urlsafe_base64_decode(pk))
except ImportError:
from django.utils.http import base36_to_int
return base36_to_int(pk)
return force_text(urlsafe_base64_decode(pk))
def send_email(to_email, from_email, context, subject_template_name,
......
from django.contrib.auth import get_user_model, user_logged_in, user_logged_out
from django.contrib.auth.tokens import default_token_generator
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()
......@@ -46,7 +48,7 @@ class RegistrationView(utils.SendEmailViewMixin, generics.CreateAPIView):
"""
Use this endpoint to register new user.
"""
serializer_class = serializers.UserRegistrationSerializer
serializer_class = serializers.serializers_manager.get('user_registration')
permission_classes = (
permissions.AllowAny,
)
......@@ -70,7 +72,7 @@ class LoginView(utils.ActionViewMixin, generics.GenericAPIView):
"""
Use this endpoint to obtain user authentication token.
"""
serializer_class = serializers.LoginSerializer
serializer_class = serializers.serializers_manager.get('login')
permission_classes = (
permissions.AllowAny,
)
......@@ -79,8 +81,9 @@ class LoginView(utils.ActionViewMixin, generics.GenericAPIView):
user = serializer.user
token, _ = Token.objects.get_or_create(user=user)
user_logged_in.send(sender=user.__class__, request=self.request, user=user)
token_serializer_class = serializers.serializers_manager.get('token')
return Response(
data=serializers.TokenSerializer(token).data,
data=token_serializer_class(token).data,
status=status.HTTP_200_OK,
)
......@@ -96,14 +99,14 @@ class LogoutView(views.APIView):
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)
return response.Response(status=status.HTTP_204_NO_CONTENT)
class PasswordResetView(utils.ActionViewMixin, utils.SendEmailViewMixin, generics.GenericAPIView):
"""
Use this endpoint to send email to user with password reset link.
"""
serializer_class = serializers.PasswordResetSerializer
serializer_class = serializers.serializers_manager.get('password_reset')
permission_classes = (
permissions.AllowAny,
)
......@@ -114,7 +117,7 @@ class PasswordResetView(utils.ActionViewMixin, utils.SendEmailViewMixin, generic
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)
return response.Response(status=status.HTTP_204_NO_CONTENT)
def get_users(self, email):
active_users = User._default_manager.filter(
......@@ -139,13 +142,13 @@ class SetPasswordView(utils.ActionViewMixin, generics.GenericAPIView):
def get_serializer_class(self):
if settings.get('SET_PASSWORD_RETYPE'):
return serializers.SetPasswordRetypeSerializer
return serializers.SetPasswordSerializer
return serializers.serializers_manager.get('set_password_retype')
return serializers.serializers_manager.get('set_password')
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)
return response.Response(status=status.HTTP_204_NO_CONTENT)
class PasswordResetConfirmView(utils.ActionViewMixin, generics.GenericAPIView):
......@@ -159,20 +162,20 @@ class PasswordResetConfirmView(utils.ActionViewMixin, generics.GenericAPIView):
def get_serializer_class(self):
if settings.get('PASSWORD_RESET_CONFIRM_RETYPE'):
return serializers.PasswordResetConfirmRetypeSerializer
return serializers.PasswordResetConfirmSerializer
return serializers.serializers_manager.get('password_reset_confirm_retype')
return serializers.serializers_manager.get('password_reset_confirm')
def action(self, serializer):
serializer.user.set_password(serializer.data['new_password'])
serializer.user.save()
return response.Response(status=status.HTTP_200_OK)
return response.Response(status=status.HTTP_204_NO_CONTENT)
class ActivationView(utils.ActionViewMixin, generics.GenericAPIView):
"""
Use this endpoint to activate user account.
"""
serializer_class = serializers.ActivationSerializer
serializer_class = serializers.serializers_manager.get('activation')
permission_classes = (
permissions.AllowAny,
)
......@@ -183,27 +186,26 @@ class ActivationView(utils.ActionViewMixin, generics.GenericAPIView):
serializer.user.save()
signals.user_activated.send(
sender=self.__class__, user=serializer.user, request=self.request)
return Response(status=status.HTTP_200_OK)
return Response(status=status.HTTP_204_NO_CONTENT)
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
return serializers.serializers_manager.get('set_username_retype')
return serializers.serializers_manager.get('set_username')
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)
return response.Response(status=status.HTTP_204_NO_CONTENT)
class UserView(generics.RetrieveUpdateAPIView):
......@@ -211,7 +213,7 @@ class UserView(generics.RetrieveUpdateAPIView):
Use this endpoint to retrieve/update user.
"""
model = User
serializer_class = serializers.UserSerializer
serializer_class = serializers.serializers_manager.get('user')
permission_classes = (
permissions.IsAuthenticated,
)
......
......@@ -22,7 +22,7 @@ def get_requirements(file_name):
setup(
name='djoser',
version='0.4.3',
version='0.5.0',
packages=get_packages('djoser'),
license='MIT',
author='SUNSCRAPERS',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment