Compatibility updates

+ removed some legacy code
+ added force_text for py3 support
This commit is contained in:
mario 2015-11-19 09:38:57 +01:00
parent 315f6f2844
commit 6ace9de268
6 changed files with 21 additions and 30 deletions

View File

@ -35,5 +35,5 @@ urlpatterns = patterns('',
url(r'^rest-auth/registration/', include('rest_auth.registration.urls')), url(r'^rest-auth/registration/', include('rest_auth.registration.urls')),
url(r'^account/', include('allauth.urls')), url(r'^account/', include('allauth.urls')),
url(r'^admin/', include(admin.site.urls)), url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/profile/$', RedirectView.as_view(url='/'), name='profile-redirect'), url(r'^accounts/profile/$', RedirectView.as_view(url='/', permanent=True), name='profile-redirect'),
) )

View File

@ -1,19 +1,18 @@
from django.contrib.auth import get_user_model, authenticate from django.contrib.auth import get_user_model, authenticate
from django.conf import settings from django.conf import settings
from django.contrib.auth.forms import PasswordResetForm, SetPasswordForm from django.contrib.auth.forms import PasswordResetForm, SetPasswordForm
try:
from django.utils.http import urlsafe_base64_decode as uid_decoder
except:
# make compatible with django 1.5
from django.utils.http import base36_to_int as uid_decoder
from django.contrib.auth.tokens import default_token_generator 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.translation import ugettext_lazy as _
from django import VERSION from django.utils.encoding import force_text
from rest_framework import serializers, exceptions from rest_framework import serializers, exceptions
from rest_framework.authtoken.models import Token from rest_framework.authtoken.models import Token
from rest_framework.exceptions import ValidationError from rest_framework.exceptions import ValidationError
# Get the UserModel
UserModel = get_user_model()
class LoginSerializer(serializers.Serializer): class LoginSerializer(serializers.Serializer):
username = serializers.CharField(required=False, allow_blank=True) username = serializers.CharField(required=False, allow_blank=True)
@ -146,11 +145,10 @@ class PasswordResetConfirmSerializer(serializers.Serializer):
def validate(self, attrs): def validate(self, attrs):
self._errors = {} self._errors = {}
# Get the UserModel
UserModel = get_user_model()
# Decode the uidb64 to uid to get User object # Decode the uidb64 to uid to get User object
try: try:
uid = uid_decoder(attrs['uid']) uid = force_text(uid_decoder(attrs['uid']))
self.user = UserModel._default_manager.get(pk=uid) self.user = UserModel._default_manager.get(pk=uid)
except (TypeError, ValueError, OverflowError, UserModel.DoesNotExist): except (TypeError, ValueError, OverflowError, UserModel.DoesNotExist):
raise ValidationError({'uid': ['Invalid value']}) raise ValidationError({'uid': ['Invalid value']})
@ -216,6 +214,6 @@ class PasswordChangeSerializer(serializers.Serializer):
def save(self): def save(self):
self.set_password_form.save() self.set_password_form.save()
if VERSION[1] > 6 and not self.logout_on_password_change: if not self.logout_on_password_change:
from django.contrib.auth import update_session_auth_hash from django.contrib.auth import update_session_auth_hash
update_session_auth_hash(self.request, self.user) update_session_auth_hash(self.request, self.user)

View File

@ -8,6 +8,7 @@ from django.contrib.auth import get_user_model
from django.core import mail from django.core import mail
from django.test.utils import override_settings from django.test.utils import override_settings
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.utils.encoding import force_text
from allauth.socialaccount.models import SocialApp from allauth.socialaccount.models import SocialApp
from allauth.socialaccount.providers.facebook.provider import GRAPH_API_URL from allauth.socialaccount.providers.facebook.provider import GRAPH_API_URL
@ -51,7 +52,7 @@ class BaseAPITestCase(object):
is_json = bool( is_json = bool(
[x for x in self.response._headers['content-type'] if 'json' in x]) [x for x in self.response._headers['content-type'] if 'json' in x])
if is_json and self.response.content: if is_json and self.response.content:
self.response.json = json.loads(self.response.content) self.response.json = json.loads(force_text(self.response.content))
else: else:
self.response.json = {} self.response.json = {}
if status_code: if status_code:
@ -298,7 +299,7 @@ class APITestCase1(TestCase, BaseAPITestCase):
data = { data = {
'new_password1': self.NEW_PASS, 'new_password1': self.NEW_PASS,
'new_password2': self.NEW_PASS, 'new_password2': self.NEW_PASS,
'uid': url_kwargs['uid'], 'uid': force_text(url_kwargs['uid']),
'token': '-wrong-token-' 'token': '-wrong-token-'
} }
self.post(url, data=data, status_code=400) self.post(url, data=data, status_code=400)
@ -325,7 +326,7 @@ class APITestCase1(TestCase, BaseAPITestCase):
data = { data = {
'new_password1': self.NEW_PASS, 'new_password1': self.NEW_PASS,
'new_password2': self.NEW_PASS, 'new_password2': self.NEW_PASS,
'uid': url_kwargs['uid'], 'uid': force_text(url_kwargs['uid']),
'token': url_kwargs['token'] 'token': url_kwargs['token']
} }
url = reverse('rest_password_reset_confirm') url = reverse('rest_password_reset_confirm')

View File

@ -1,9 +1,5 @@
from six import string_types from six import string_types
import sys from importlib import import_module
if sys.version_info < (2, 7):
from django.utils.importlib import import_module
else:
from importlib import import_module
def import_callable(path_or_callable): def import_callable(path_or_callable):

View File

@ -28,8 +28,8 @@ setup(
keywords='django rest auth registration rest-framework django-registration api', keywords='django rest auth registration rest-framework django-registration api',
zip_safe=False, zip_safe=False,
install_requires=[ install_requires=[
'Django>=1.5.0', 'Django>=1.7.0',
'djangorestframework>=3.0', 'djangorestframework>=3.1.0',
'six>=1.9.0', 'six>=1.9.0',
], ],
test_suite='runtests.runtests', test_suite='runtests.runtests',

View File

@ -1,4 +1,3 @@
import django
import os import os
import sys import sys
@ -18,15 +17,12 @@ IS_STAGING = False
IS_PROD = False IS_PROD = False
IS_TEST = 'test' in sys.argv or 'test_coverage' in sys.argv IS_TEST = 'test' in sys.argv or 'test_coverage' in sys.argv
if django.VERSION[:2] >= (1, 3): DATABASES = {
DATABASES = { 'default': {
'default': { 'ENGINE': 'django.db.backends.sqlite3',
'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:',
'NAME': ':memory:',
}
} }
else: }
DATABASE_ENGINE = 'sqlite3'
MIDDLEWARE_CLASSES = [ MIDDLEWARE_CLASSES = [
'django.middleware.common.CommonMiddleware', 'django.middleware.common.CommonMiddleware',