Skip to content

Commit f8a9cc1

Browse files
committed
Merge branch 'master' into jwt-support
# Conflicts: # docs/configuration.rst # rest_auth/registration/views.py # rest_auth/tests/test_api.py # rest_auth/utils.py # rest_auth/views.py
2 parents 317db1b + 7c8a34f commit f8a9cc1

19 files changed

+298
-184
lines changed

demo/demo/urls.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from django.conf.urls import patterns, include, url
1+
from django.conf.urls import include, url
22
from django.contrib import admin
33
from django.views.generic import TemplateView, RedirectView
44

5-
urlpatterns = patterns('',
5+
urlpatterns = [
66
url(r'^$', TemplateView.as_view(template_name="home.html"), name='home'),
77
url(r'^signup/$', TemplateView.as_view(template_name="signup.html"),
88
name='signup'),
@@ -36,4 +36,4 @@
3636
url(r'^account/', include('allauth.urls')),
3737
url(r'^admin/', include(admin.site.urls)),
3838
url(r'^accounts/profile/$', RedirectView.as_view(url='/', permanent=True), name='profile-redirect'),
39-
)
39+
]

docs/api_endpoints.rst

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Basic
77
- /rest-auth/login/ (POST)
88

99
- username (string)
10+
- email (string)
1011
- password (string)
1112

1213

@@ -55,16 +56,6 @@ Registration
5556
- password2
5657
- email
5758

58-
.. note:: This endpoint is based on ``allauth.account.views.SignupView`` and uses the same form as in this view. To override fields you have to create custom Signup Form and define it in django settings:
59-
60-
.. code-block:: python
61-
62-
ACCOUNT_FORMS = {
63-
'signup': 'path.to.custom.SignupForm'
64-
}
65-
66-
See allauth documentation for more details.
67-
6859
- /rest-auth/registration/verify-email/ (POST)
6960

7061
- key

docs/configuration.rst

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ Configuration
2929
...
3030
}
3131
32-
- **REST_SESSION_LOGIN** - Enable session login in Login API view (default: True)
32+
- **REST_AUTH_REGISTRATION_SERIALIZERS**
3333

34+
You can define your custom serializers for registration endpoint.
35+
Possible key values:
3436

35-
- **OLD_PASSWORD_FIELD_ENABLED** - set it to True if you want to have old password verification on password change enpoint (default: False)
37+
- REGISTER_SERIALIZER - serializer class in ``rest_auth.register.views.RegisterView``, default value ``rest_auth.register.serializers.RegisterSerializer``
3638

37-
- **LOGOUT_ON_PASSWORD_CHANGE** - set to False if you want to keep the current user logged in after a password change
39+
- **REST_AUTH_TOKEN_MODEL** - model class for tokens, default value ``rest_framework.authtoken.models``
3840

39-
- **REST_USE_JWT** - If enabled, this will use `django-rest-framework-jwt <http://getblimp.github.io/django-rest-framework-jwt/>` as a backend, and instead of session based tokens or Social Login keys, it will return a JWT.
41+
- **REST_AUTH_TOKEN_CREATOR** - callable to create tokens, default value ``rest_auth.utils.default_create_token``.
4042

43+
- **REST_SESSION_LOGIN** - Enable session login in Login API view (default: True)
44+
45+
- **OLD_PASSWORD_FIELD_ENABLED** - set it to True if you want to have old password verification on password change enpoint (default: False)
46+
47+
- **LOGOUT_ON_PASSWORD_CHANGE** - set to False if you want to keep the current user logged in after a password change

docs/faq.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ FAQ
1717
djang-allauth https://github.com/pennersr/django-allauth/blob/master/allauth/account/views.py#L190
1818

1919

20-
2. How can I update UserProfile assigned to User model?
20+
2. I get an error: Reverse for 'password_reset_confirm' not found.
21+
22+
You need to add `password_reset_confirm` url into your ``urls.py`` (at the top of any other included urls). Please check the ``urls.py`` module inside demo app example for more details.
23+
24+
25+
3. How can I update UserProfile assigned to User model?
2126

2227
Assuming you already have UserProfile model defined like this
2328

docs/installation.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ You're good to go now!
3838
Registration (optional)
3939
-----------------------
4040

41-
1. If you want to enable standard registration process you will need to install ``django-allauth`` - see this doc for installation http://django-allauth.readthedocs.org/en/latest/installation.html.
41+
1. If you want to enable standard registration process you will need to install ``django-allauth`` by using ``pip install django-rest-auth[extras]`` or ``pip install django-rest-auth[with_social]``.
4242

4343
2. Add ``allauth``, ``allauth.account`` and ``rest_auth.registration`` apps to INSTALLED_APPS in your django settings.py:
4444

rest_auth/app_settings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
PasswordResetSerializer as DefaultPasswordResetSerializer,
99
PasswordResetConfirmSerializer as DefaultPasswordResetConfirmSerializer,
1010
PasswordChangeSerializer as DefaultPasswordChangeSerializer)
11-
from .utils import import_callable
11+
from .utils import import_callable, default_create_token
1212

13+
create_token = import_callable(
14+
getattr(settings, 'REST_AUTH_TOKEN_CREATOR', default_create_token))
1315

1416
serializers = getattr(settings, 'REST_AUTH_SERIALIZERS', {})
1517

rest_auth/models.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
# from django.db import models
1+
from django.conf import settings
2+
3+
from rest_framework.authtoken.models import Token as DefaultTokenModel
4+
5+
from .utils import import_callable
26

37
# Register your models here.
8+
9+
TokenModel = import_callable(
10+
getattr(settings, 'REST_AUTH_TOKEN_MODEL', DefaultTokenModel))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django.conf import settings
2+
3+
from rest_auth.registration.serializers import (
4+
RegisterSerializer as DefaultRegisterSerializer)
5+
from ..utils import import_callable
6+
7+
8+
serializers = getattr(settings, 'REST_AUTH_REGISTER_SERIALIZERS', {})
9+
10+
RegisterSerializer = import_callable(
11+
serializers.get('REGISTER_SERIALIZER', DefaultRegisterSerializer))

rest_auth/registration/serializers.py

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
from django.http import HttpRequest
22
from django.conf import settings
33

4+
try:
5+
from allauth.account import app_settings as allauth_settings
6+
from allauth.utils import (email_address_exists,
7+
get_username_max_length)
8+
from allauth.account.adapter import get_adapter
9+
from allauth.account.utils import setup_user_email
10+
except ImportError:
11+
raise ImportError('allauth needs to be added to INSTALLED_APPS.')
12+
413
from rest_framework import serializers
514
from requests.exceptions import HTTPError
615
# Import is needed only if we are using social login, in which
716
# case the allauth.socialaccount will be declared
8-
try:
9-
from allauth.socialaccount.helpers import complete_social_login
10-
except ImportError:
11-
raise ImportError('allauth.socialaccount needs to be installed.')
1217

13-
if 'allauth.socialaccount' not in settings.INSTALLED_APPS:
14-
raise ImportError('allauth.socialaccount needs to be added to INSTALLED_APPS.')
18+
if 'allauth.socialaccount' in settings.INSTALLED_APPS:
19+
try:
20+
from allauth.socialaccount.helpers import complete_social_login
21+
except ImportError:
22+
pass
23+
1524

1625

1726
class SocialLoginSerializer(serializers.Serializer):
@@ -109,3 +118,57 @@ def validate(self, attrs):
109118
attrs['user'] = login.account.user
110119

111120
return attrs
121+
122+
123+
class RegisterSerializer(serializers.Serializer):
124+
username = serializers.CharField(
125+
max_length=get_username_max_length(),
126+
min_length=allauth_settings.USERNAME_MIN_LENGTH,
127+
required=allauth_settings.USERNAME_REQUIRED
128+
)
129+
email = serializers.EmailField(required=allauth_settings.EMAIL_REQUIRED)
130+
password1 = serializers.CharField(required=True, write_only=True)
131+
password2 = serializers.CharField(required=True, write_only=True)
132+
133+
def validate_username(self, username):
134+
username = get_adapter().clean_username(username)
135+
return username
136+
137+
def validate_email(self, email):
138+
email = get_adapter().clean_email(email)
139+
if allauth_settings.UNIQUE_EMAIL:
140+
if email and email_address_exists(email):
141+
raise serializers.ValidationError(
142+
"A user is already registered with this e-mail address.")
143+
return email
144+
145+
def validate_password1(self, password):
146+
return get_adapter().clean_password(password)
147+
148+
def validate(self, data):
149+
if data['password1'] != data['password2']:
150+
raise serializers.ValidationError("The two password fields didn't match.")
151+
return data
152+
153+
def custom_signup(self, request, user):
154+
pass
155+
156+
def get_cleaned_data(self):
157+
return {
158+
'username': self.validated_data.get('username', ''),
159+
'password1': self.validated_data.get('password1', ''),
160+
'email': self.validated_data.get('email', '')
161+
}
162+
163+
def save(self, request):
164+
adapter = get_adapter()
165+
user = adapter.new_user(request)
166+
self.cleaned_data = self.get_cleaned_data()
167+
adapter.save_user(request, user, self)
168+
self.custom_signup(request, user)
169+
setup_user_email(request, user, [])
170+
return user
171+
172+
173+
class VerifyEmailSerializer(serializers.Serializer):
174+
key = serializers.CharField()

rest_auth/registration/urls.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from django.views.generic import TemplateView
2-
from django.conf.urls import patterns, url
2+
from django.conf.urls import url
33

44
from .views import RegisterView, VerifyEmailView
55

6-
urlpatterns = patterns(
7-
'',
6+
urlpatterns = [
87
url(r'^$', RegisterView.as_view(), name='rest_register'),
98
url(r'^verify-email/$', VerifyEmailView.as_view(), name='rest_verify_email'),
109

@@ -21,4 +20,4 @@
2120
# djang-allauth https://github.com/pennersr/django-allauth/blob/master/allauth/account/views.py#L190
2221
url(r'^account-confirm-email/(?P<key>\w+)/$', TemplateView.as_view(),
2322
name='account_confirm_email'),
24-
)
23+
]

0 commit comments

Comments
 (0)