Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

made compatible with django-4.0 #652

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions rest_auth/registration/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.http import HttpRequest
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import gettext_lazy as _
from django.contrib.auth import get_user_model

try:
Expand Down Expand Up @@ -54,7 +54,8 @@ def get_social_login(self, adapter, app, token, response):
`allauth.socialaccount.SocialLoginView` instance
"""
request = self._get_request()
social_login = adapter.complete_login(request, app, token, response=response)
social_login = adapter.complete_login(
request, app, token, response=response)
social_login.token = token
return social_login

Expand All @@ -69,7 +70,8 @@ def validate(self, attrs):

adapter_class = getattr(view, 'adapter_class', None)
if not adapter_class:
raise serializers.ValidationError(_("Define adapter_class in view"))
raise serializers.ValidationError(
_("Define adapter_class in view"))

adapter = adapter_class(request)
app = adapter.get_provider().get_app(request)
Expand Down Expand Up @@ -119,7 +121,8 @@ def validate(self, attrs):
social_token.app = app

try:
login = self.get_social_login(adapter, app, social_token, access_token)
login = self.get_social_login(
adapter, app, social_token, access_token)
complete_social_login(request, login)
except HTTPError:
raise serializers.ValidationError(_("Incorrect value"))
Expand Down Expand Up @@ -154,7 +157,8 @@ def get_social_login(self, *args, **kwargs):
Refer to the implementation of get_social_login in base class and to the
allauth.socialaccount.helpers module complete_social_login function.
"""
social_login = super(SocialConnectMixin, self).get_social_login(*args, **kwargs)
social_login = super(SocialConnectMixin,
self).get_social_login(*args, **kwargs)
social_login.state['process'] = AuthProcess.CONNECT
return social_login

Expand Down Expand Up @@ -190,7 +194,8 @@ def validate_password1(self, password):

def validate(self, data):
if data['password1'] != data['password2']:
raise serializers.ValidationError(_("The two password fields didn't match."))
raise serializers.ValidationError(
_("The two password fields didn't match."))
return data

def custom_signup(self, request, user):
Expand Down
10 changes: 5 additions & 5 deletions rest_auth/registration/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from django.views.generic import TemplateView
from django.conf.urls import url
from django.urls import path, re_path

from .views import RegisterView, VerifyEmailView

urlpatterns = [
url(r'^$', RegisterView.as_view(), name='rest_register'),
url(r'^verify-email/$', VerifyEmailView.as_view(), name='rest_verify_email'),
path('', RegisterView.as_view(), name='rest_register'),
path('verify-email/', VerifyEmailView.as_view(), name='rest_verify_email'),

# This url is used by django-allauth and empty TemplateView is
# defined just to allow reverse() call inside app, for example when email
Expand All @@ -18,6 +18,6 @@
# If you don't want to use API on that step, then just use ConfirmEmailView
# view from:
# django-allauth https://github.com/pennersr/django-allauth/blob/master/allauth/account/views.py
url(r'^account-confirm-email/(?P<key>[-:\w]+)/$', TemplateView.as_view(),
name='account_confirm_email'),
re_path(r'account-confirm-email/(?P<key>[-:\w]+)/', TemplateView.as_view(),
name='account_confirm_email'),
]
2 changes: 1 addition & 1 deletion rest_auth/registration/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.conf import settings
from django.utils.decorators import method_decorator
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import gettext_lazy as _
from django.views.decorators.debug import sensitive_post_parameters

from rest_framework.views import APIView
Expand Down
24 changes: 14 additions & 10 deletions rest_auth/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from django.contrib.auth.forms import PasswordResetForm, SetPasswordForm
from django.contrib.auth.tokens import default_token_generator
from django.utils.http import urlsafe_base64_decode as uid_decoder
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import force_text
from django.utils.translation import gettext_lazy as _
from django.utils.encoding import force_str

from rest_framework import serializers, exceptions
from rest_framework.exceptions import ValidationError
Expand Down Expand Up @@ -85,7 +85,8 @@ def validate(self, attrs):
# Authentication without using allauth
if email:
try:
username = UserModel.objects.get(email__iexact=email).get_username()
username = UserModel.objects.get(
email__iexact=email).get_username()
except UserModel.DoesNotExist:
pass

Expand All @@ -107,7 +108,8 @@ def validate(self, attrs):
if app_settings.EMAIL_VERIFICATION == app_settings.EmailVerificationMethod.MANDATORY:
email_address = user.emailaddress_set.get(email=user.email)
if not email_address.verified:
raise serializers.ValidationError(_('E-mail is not verified.'))
raise serializers.ValidationError(
_('E-mail is not verified.'))

attrs['user'] = user
return attrs
Expand Down Expand Up @@ -147,9 +149,11 @@ def get_user(self, obj):
"""
rest_auth_serializers = getattr(settings, 'REST_AUTH_SERIALIZERS', {})
JWTUserDetailsSerializer = import_callable(
rest_auth_serializers.get('USER_DETAILS_SERIALIZER', UserDetailsSerializer)
rest_auth_serializers.get(
'USER_DETAILS_SERIALIZER', UserDetailsSerializer)
)
user_data = JWTUserDetailsSerializer(obj['user'], context=self.context).data
user_data = JWTUserDetailsSerializer(
obj['user'], context=self.context).data
return user_data


Expand All @@ -167,7 +171,8 @@ def get_email_options(self):

def validate_email(self, value):
# Create PasswordResetForm with the serializer
self.reset_form = self.password_reset_form_class(data=self.initial_data)
self.reset_form = self.password_reset_form_class(
data=self.initial_data)
if not self.reset_form.is_valid():
raise serializers.ValidationError(self.reset_form.errors)

Expand Down Expand Up @@ -205,7 +210,7 @@ def validate(self, attrs):

# Decode the uidb64 to uid to get User object
try:
uid = force_text(uid_decoder(attrs['uid']))
force_str(uid_decoder(attrs['uid']))
self.user = UserModel._default_manager.get(pk=uid)
except (TypeError, ValueError, OverflowError, UserModel.DoesNotExist):
raise ValidationError({'uid': ['Invalid value']})
Expand Down Expand Up @@ -256,8 +261,7 @@ def validate_old_password(self, value):
)

if all(invalid_password_conditions):
err_msg = _("Your old password was entered incorrectly. Please enter it again.")
raise serializers.ValidationError(err_msg)
raise serializers.ValidationError('Invalid password')
return value

def validate(self, attrs):
Expand Down
28 changes: 14 additions & 14 deletions rest_auth/tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.conf.urls import url, include
from django.urls import path, re_path, include
from django.views.generic import TemplateView
from . import django_urls

Expand Down Expand Up @@ -53,20 +53,20 @@ class TwitterLoginNoAdapter(SocialLoginView):


urlpatterns += [
url(r'^rest-registration/', include('rest_auth.registration.urls')),
url(r'^test-admin/', include(django_urls)),
url(r'^account-email-verification-sent/$', TemplateView.as_view(),
path('rest-registration/', include('rest_auth.registration.urls')),
path('test-admin/', include(django_urls)),
re_path(r'^account-email-verification-sent/$', TemplateView.as_view(),
name='account_email_verification_sent'),
url(r'^account-confirm-email/(?P<key>[-:\w]+)/$', TemplateView.as_view(),
re_path(r'^account-confirm-email/(?P<key>[-:\w]+)/$', TemplateView.as_view(),
name='account_confirm_email'),
url(r'^social-login/facebook/$', FacebookLogin.as_view(), name='fb_login'),
url(r'^social-login/twitter/$', TwitterLogin.as_view(), name='tw_login'),
url(r'^social-login/twitter-no-view/$', twitter_login_view, name='tw_login_no_view'),
url(r'^social-login/twitter-no-adapter/$', TwitterLoginNoAdapter.as_view(), name='tw_login_no_adapter'),
url(r'^social-login/facebook/connect/$', FacebookConnect.as_view(), name='fb_connect'),
url(r'^social-login/twitter/connect/$', TwitterConnect.as_view(), name='tw_connect'),
url(r'^socialaccounts/$', SocialAccountListView.as_view(), name='social_account_list'),
url(r'^socialaccounts/(?P<pk>\d+)/disconnect/$', SocialAccountDisconnectView.as_view(),
re_path(r'^social-login/facebook/$', FacebookLogin.as_view(), name='fb_login'),
re_path(r'^social-login/twitter/$', TwitterLogin.as_view(), name='tw_login'),
re_path(r'^social-login/twitter-no-view/$', twitter_login_view, name='tw_login_no_view'),
re_path(r'^social-login/twitter-no-adapter/$', TwitterLoginNoAdapter.as_view(), name='tw_login_no_adapter'),
re_path(r'^social-login/facebook/connect/$', FacebookConnect.as_view(), name='fb_connect'),
re_path(r'^social-login/twitter/connect/$', TwitterConnect.as_view(), name='tw_connect'),
re_path(r'^socialaccounts/$', SocialAccountListView.as_view(), name='social_account_list'),
re_path(r'^socialaccounts/(?P<pk>\d+)/disconnect/$', SocialAccountDisconnectView.as_view(),
name='social_account_disconnect'),
url(r'^accounts/', include('allauth.socialaccount.urls'))
path('accounts/', include('allauth.socialaccount.urls'))
]
20 changes: 10 additions & 10 deletions rest_auth/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.conf.urls import url
from django.urls import path

from rest_auth.views import (
LoginView, LogoutView, UserDetailsView, PasswordChangeView,
Expand All @@ -7,14 +7,14 @@

urlpatterns = [
# URLs that do not require a session or valid token
url(r'^password/reset/$', PasswordResetView.as_view(),
name='rest_password_reset'),
url(r'^password/reset/confirm/$', PasswordResetConfirmView.as_view(),
name='rest_password_reset_confirm'),
url(r'^login/$', LoginView.as_view(), name='rest_login'),
path('password/reset/', PasswordResetView.as_view(),
name='rest_password_reset'),
path('password/reset/confirm/', PasswordResetConfirmView.as_view(),
name='rest_password_reset_confirm'),
path('login/', LoginView.as_view(), name='rest_login'),
# URLs that require a user to be logged in with a valid session / token.
url(r'^logout/$', LogoutView.as_view(), name='rest_logout'),
url(r'^user/$', UserDetailsView.as_view(), name='rest_user_details'),
url(r'^password/change/$', PasswordChangeView.as_view(),
name='rest_password_change'),
path('logout/', LogoutView.as_view(), name='rest_logout'),
path('user/', UserDetailsView.as_view(), name='rest_user_details'),
path('password/change/', PasswordChangeView.as_view(),
name='rest_password_change'),
]
5 changes: 3 additions & 2 deletions rest_auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.contrib.auth import get_user_model
from django.core.exceptions import ObjectDoesNotExist
from django.utils.decorators import method_decorator
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import gettext_lazy as _
from django.views.decorators.debug import sensitive_post_parameters

from rest_framework import status
Expand Down Expand Up @@ -89,7 +89,8 @@ def get_response(self):
from rest_framework_jwt.settings import api_settings as jwt_settings
if jwt_settings.JWT_AUTH_COOKIE:
from datetime import datetime
expiration = (datetime.utcnow() + jwt_settings.JWT_EXPIRATION_DELTA)
expiration = (datetime.utcnow() +
jwt_settings.JWT_EXPIRATION_DELTA)
response.set_cookie(jwt_settings.JWT_AUTH_COOKIE,
self.token,
expires=expiration,
Expand Down