From 7984cb5f0074658ee5021cc92b01177d759f228c Mon Sep 17 00:00:00 2001 From: ante Date: Fri, 2 Jan 2015 09:04:29 +0100 Subject: [PATCH 1/9] Test za DRF3 --- demo/demo/settings.py | 18 +++++++++++++++--- demo/demo/urls.py | 1 + rest_auth/views.py | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/demo/demo/settings.py b/demo/demo/settings.py index b0b3941..1bc1149 100644 --- a/demo/demo/settings.py +++ b/demo/demo/settings.py @@ -44,17 +44,20 @@ INSTALLED_APPS = ( 'django.contrib.staticfiles', 'django.contrib.sites', + 'corsheaders', + 'rest_framework', 'rest_framework.authtoken', 'rest_auth', - 'allauth', - 'allauth.account', - 'rest_auth.registration', + # 'allauth', + # 'allauth.account', + # 'rest_auth.registration', ) MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', + 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', @@ -105,3 +108,12 @@ SITE_ID = 1 ACCOUNT_EMAIL_REQUIRED = True ACCOUNT_AUTHENTICATION_METHOD = 'email' ACCOUNT_EMAIL_VERIFICATION = 'mandatory' + +CORS_ORIGIN_ALLOW_ALL = True + +REST_FRAMEWORK = { + 'DEFAULT_AUTHENTICATION_CLASSES': ( + 'rest_framework.authentication.SessionAuthentication', + 'rest_framework.authentication.TokenAuthentication', + ) +} \ No newline at end of file diff --git a/demo/demo/urls.py b/demo/demo/urls.py index 42b4a88..1eb8778 100644 --- a/demo/demo/urls.py +++ b/demo/demo/urls.py @@ -33,5 +33,6 @@ urlpatterns = patterns('', url(r'^rest-auth/', include('rest_auth.urls')), url(r'^rest-auth/registration/', include('rest_auth.registration.urls')), + url(r'^account/', include('allauth.urls')), url(r'^admin/', include(admin.site.urls)), ) diff --git a/rest_auth/views.py b/rest_auth/views.py index 0f18fd5..05217cf 100644 --- a/rest_auth/views.py +++ b/rest_auth/views.py @@ -31,7 +31,7 @@ class Login(GenericAPIView): response_serializer = TokenSerializer def login(self): - self.user = self.serializer.object['user'] + self.user = self.serializer.validated_data['user'] self.token, created = self.token_model.objects.get_or_create( user=self.user) if getattr(settings, 'REST_SESSION_LOGIN', True): From 59f482fd65da542c4cd63d16c2b7645a283a7493 Mon Sep 17 00:00:00 2001 From: ante Date: Sat, 3 Jan 2015 14:55:33 +0100 Subject: [PATCH 2/9] Making PasswordChangeSerializer compatible with DRF3 - validate_old_password - works with field value and not with dictionary of all values - validate - should not return None but raise ValidationError --- rest_auth/serializers.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/rest_auth/serializers.py b/rest_auth/serializers.py index 00fc438..58c1809 100644 --- a/rest_auth/serializers.py +++ b/rest_auth/serializers.py @@ -138,19 +138,20 @@ class PasswordChangeSerializer(serializers.Serializer): self.request = self.context.get('request') self.user = getattr(self.request, 'user', None) - def validate_old_password(self, attrs, source): + def validate_old_password(self, value): if self.old_password_field_enabled and self.user and \ - not self.user.check_password(attrs.get(source, '')): + not self.user.check_password(value): raise serializers.ValidationError('Invalid password') - return attrs + return value def validate(self, attrs): self.set_password_form = self.set_password_form_class(user=self.user, data=attrs) if not self.set_password_form.is_valid(): - self._errors = self.set_password_form.errors - return None + #self._errors = self.set_password_form.errors + #return None + raise serializers.ValidationError(self.set_password_form.errors) return attrs def save(self): From daa446dc3256b493712c565800d0f7c0b8eb02de Mon Sep 17 00:00:00 2001 From: ante Date: Sat, 3 Jan 2015 14:57:32 +0100 Subject: [PATCH 3/9] Problems finding authtoken module --- test_settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_settings.py b/test_settings.py index 2fcc1e3..0fc7eaa 100644 --- a/test_settings.py +++ b/test_settings.py @@ -75,5 +75,5 @@ ACCOUNT_ACTIVATION_DAYS = 1 SITE_ID = 1 MIGRATION_MODULES = { - 'authtoken': 'authtoken.migrations', + 'rest_framework.authtoken': 'rest_framework.authtoken.migrations', } From 4065952f705e8dba2faf368e55a29162c7975b96 Mon Sep 17 00:00:00 2001 From: ante Date: Sat, 3 Jan 2015 15:05:40 +0100 Subject: [PATCH 4/9] Making SocialLoginSerializerPasswordChangeSerializer compatible with DRF3 - validate_access_token - works with field value and not with dictionary of all values --- rest_auth/registration/serializers.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/rest_auth/registration/serializers.py b/rest_auth/registration/serializers.py index 7a403a0..5cfe049 100644 --- a/rest_auth/registration/serializers.py +++ b/rest_auth/registration/serializers.py @@ -7,8 +7,7 @@ class SocialLoginSerializer(serializers.Serializer): access_token = serializers.CharField(required=True) - def validate_access_token(self, attrs, source): - access_token = attrs[source] + def validate_access_token(self, value): view = self.context.get('view') request = self.context.get('request') @@ -23,12 +22,12 @@ class SocialLoginSerializer(serializers.Serializer): self.adapter = self.adapter_class() app = self.adapter.get_provider().get_app(request) - token = self.adapter.parse_token({'access_token': access_token}) + token = self.adapter.parse_token({'access_token': value}) token.app = app try: login = self.adapter.complete_login(request, app, token, - response=access_token) + response=value) token.account = login.account login.token = token complete_social_login(request, login) @@ -40,4 +39,4 @@ class SocialLoginSerializer(serializers.Serializer): login.save(request, connect=True) self.object = {'user': login.account.user} - return attrs + return value From b4ed9b5f44ca4a555e5bd16bafaa94b0a7ea469e Mon Sep 17 00:00:00 2001 From: ante Date: Sat, 3 Jan 2015 16:49:27 +0100 Subject: [PATCH 5/9] Making PasswordResetSerializer & PasswordResetConfirmSerializer compatible with DRF3 - validate_email - works with field value and not with dictionary of all values - raise exception or return serialized value --- rest_auth/serializers.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/rest_auth/serializers.py b/rest_auth/serializers.py index 58c1809..d498565 100644 --- a/rest_auth/serializers.py +++ b/rest_auth/serializers.py @@ -58,12 +58,12 @@ class PasswordResetSerializer(serializers.Serializer): password_reset_form_class = PasswordResetForm - def validate_email(self, attrs, source): + def validate_email(self, value): # Create PasswordResetForm with the serializer - self.reset_form = self.password_reset_form_class(data=attrs) + self.reset_form = self.password_reset_form_class(data={'email': value}) if not self.reset_form.is_valid(): raise serializers.ValidationError('Error') - return attrs + return value def save(self): request = self.context.get('request') @@ -103,6 +103,7 @@ class PasswordResetConfirmSerializer(serializers.Serializer): self.user = UserModel._default_manager.get(pk=uid) except (TypeError, ValueError, OverflowError, UserModel.DoesNotExist): self._errors['uid'] = ['Invalid value'] + raise serializers.ValidationError('Invalid UID') self.custom_validation(attrs) @@ -115,6 +116,8 @@ class PasswordResetConfirmSerializer(serializers.Serializer): if not default_token_generator.check_token(self.user, attrs['token']): self._errors['token'] = ['Invalid value'] + return attrs + def save(self): self.set_password_form.save() From 54fb1de5d27b260e440010cb191ec2fd0204cbe6 Mon Sep 17 00:00:00 2001 From: ante Date: Sat, 3 Jan 2015 17:29:17 +0100 Subject: [PATCH 6/9] Python 3 fixes --- rest_auth/tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest_auth/tests.py b/rest_auth/tests.py index 1134940..ebfc704 100644 --- a/rest_auth/tests.py +++ b/rest_auth/tests.py @@ -64,7 +64,7 @@ class BaseAPITestCase(object): is_json = bool( filter(lambda x: 'json' in x, self.response._headers['content-type'])) if is_json and self.response.content: - self.response.json = json.loads(self.response.content) + self.response.json = json.loads(self.response.content.decode()) else: self.response.json = {} if status_code: @@ -176,7 +176,7 @@ class APITestCase1(TestCase, BaseAPITestCase): result['uid'] = int_to_base36(user.pk) else: from django.utils.http import urlsafe_base64_encode - result['uid'] = urlsafe_base64_encode(force_bytes(user.pk)) + result['uid'] = urlsafe_base64_encode(force_bytes(user.pk)).decode() result['token'] = default_token_generator.make_token(user) return result From e5f1bf62ab3e1042115599e37e51a5dbca0d7b3d Mon Sep 17 00:00:00 2001 From: ante Date: Sat, 3 Jan 2015 19:04:50 +0100 Subject: [PATCH 7/9] Fixing SocialLoginSerializer --- demo/demo/settings.py | 3 +-- rest_auth/registration/serializers.py | 12 +++++++----- test_settings.py | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/demo/demo/settings.py b/demo/demo/settings.py index 1bc1149..89341b1 100644 --- a/demo/demo/settings.py +++ b/demo/demo/settings.py @@ -44,7 +44,6 @@ INSTALLED_APPS = ( 'django.contrib.staticfiles', 'django.contrib.sites', - 'corsheaders', 'rest_framework', 'rest_framework.authtoken', @@ -57,7 +56,7 @@ INSTALLED_APPS = ( MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', - 'corsheaders.middleware.CorsMiddleware', + 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', diff --git a/rest_auth/registration/serializers.py b/rest_auth/registration/serializers.py index 5cfe049..6682ba8 100644 --- a/rest_auth/registration/serializers.py +++ b/rest_auth/registration/serializers.py @@ -7,7 +7,9 @@ class SocialLoginSerializer(serializers.Serializer): access_token = serializers.CharField(required=True) - def validate_access_token(self, value): + + def validate(self, attrs): + access_token = attrs['access_token'] view = self.context.get('view') request = self.context.get('request') @@ -22,12 +24,12 @@ class SocialLoginSerializer(serializers.Serializer): self.adapter = self.adapter_class() app = self.adapter.get_provider().get_app(request) - token = self.adapter.parse_token({'access_token': value}) + token = self.adapter.parse_token({'access_token': access_token}) token.app = app try: login = self.adapter.complete_login(request, app, token, - response=value) + response=access_token) token.account = login.account login.token = token complete_social_login(request, login) @@ -37,6 +39,6 @@ class SocialLoginSerializer(serializers.Serializer): if not login.is_existing: login.lookup() login.save(request, connect=True) - self.object = {'user': login.account.user} + attrs['user'] = login.account.user - return value + return attrs diff --git a/test_settings.py b/test_settings.py index 0fc7eaa..2fcc1e3 100644 --- a/test_settings.py +++ b/test_settings.py @@ -75,5 +75,5 @@ ACCOUNT_ACTIVATION_DAYS = 1 SITE_ID = 1 MIGRATION_MODULES = { - 'rest_framework.authtoken': 'rest_framework.authtoken.migrations', + 'authtoken': 'authtoken.migrations', } From b5682fdda2e03eaf9f1f394f90dd0983dc0ea6e1 Mon Sep 17 00:00:00 2001 From: ante Date: Thu, 8 Jan 2015 20:08:53 +0100 Subject: [PATCH 8/9] Requirements for DRF >= 3.0 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 1a4fdfe..b8aef1a 100644 --- a/setup.py +++ b/setup.py @@ -29,7 +29,7 @@ setup( zip_safe=False, install_requires=[ 'Django>=1.5.0', - 'djangorestframework>=2.3.13, <3.0', + 'djangorestframework>=3.0', 'six>=1.8.0', ], test_suite='runtests.runtests', From cc6919ab2a463401c43793708777261c1e8a92f1 Mon Sep 17 00:00:00 2001 From: ante Date: Thu, 8 Jan 2015 20:56:38 +0100 Subject: [PATCH 9/9] django-allauth>=0.18 is not good anymore, back to 0.18 until issue resolved --- test_requirements.pip | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_requirements.pip b/test_requirements.pip index 667775b..b3632bb 100644 --- a/test_requirements.pip +++ b/test_requirements.pip @@ -1,2 +1,2 @@ -django-allauth>=0.18.0 +django-allauth==0.18.0 responses>=0.2.2