From f0d4232c1de2c3154535312d1d48c59854ffa162 Mon Sep 17 00:00:00 2001 From: Jonas Liljestrand Date: Sat, 17 Nov 2012 17:46:16 +0100 Subject: [PATCH 1/9] Django 1.5 support, and awareness for AUTH_USER_MODEL --- rest_framework/authtoken/models.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/rest_framework/authtoken/models.py b/rest_framework/authtoken/models.py index 5b3071aa7..610de8d8d 100644 --- a/rest_framework/authtoken/models.py +++ b/rest_framework/authtoken/models.py @@ -2,14 +2,28 @@ import uuid import hmac from hashlib import sha1 from django.db import models +from django import VERSION +try: + from django.db.models.auth import User + user_model = User + except ImportError: + raise ImportError + else: + raise + +if VERSION[:2] in ((1, 5,),): + from django.conf import settings + if hasattr(settings, AUTH_USER_MODEL): + user_model = settings.AUTH_USER_MODEL + class Token(models.Model): """ The default authorization token model. """ key = models.CharField(max_length=40, primary_key=True) - user = models.OneToOneField('auth.User', related_name='auth_token') + user = models.OneToOneField(user_model, related_name='auth_token') created = models.DateTimeField(auto_now_add=True) def save(self, *args, **kwargs): From 3c1b5c34356eef4ff1a2ecdec26c761c7a27eb27 Mon Sep 17 00:00:00 2001 From: Jonas Liljestrand Date: Sat, 17 Nov 2012 17:53:08 +0100 Subject: [PATCH 2/9] indent error --- rest_framework/authtoken/models.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rest_framework/authtoken/models.py b/rest_framework/authtoken/models.py index 610de8d8d..08b70f529 100644 --- a/rest_framework/authtoken/models.py +++ b/rest_framework/authtoken/models.py @@ -6,11 +6,11 @@ from django import VERSION try: from django.db.models.auth import User - user_model = User - except ImportError: - raise ImportError - else: - raise + user_model = User +except ImportError: + raise ImportError +else: + raise if VERSION[:2] in ((1, 5,),): from django.conf import settings From bbb5a8a1d90573665c18b70add60d12e8a36882f Mon Sep 17 00:00:00 2001 From: Jonas Liljestrand Date: Sat, 17 Nov 2012 18:01:46 +0100 Subject: [PATCH 3/9] fixed import error --- rest_framework/authtoken/models.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/rest_framework/authtoken/models.py b/rest_framework/authtoken/models.py index 08b70f529..fa1f4ea61 100644 --- a/rest_framework/authtoken/models.py +++ b/rest_framework/authtoken/models.py @@ -4,18 +4,18 @@ from hashlib import sha1 from django.db import models from django import VERSION -try: - from django.db.models.auth import User - user_model = User -except ImportError: - raise ImportError -else: - raise if VERSION[:2] in ((1, 5,),): from django.conf import settings if hasattr(settings, AUTH_USER_MODEL): user_model = settings.AUTH_USER_MODEL + else: + from django.contrib.auth.models import User as user_model +else: + try: + from django.db.models.auth import User as user_model + except ImportError: + raise ImportError('User model is not to be found.') class Token(models.Model): From cd482c0ad22bbb810378c61e02a790e5e3796aa7 Mon Sep 17 00:00:00 2001 From: Jonas Liljestrand Date: Sat, 17 Nov 2012 18:04:37 +0100 Subject: [PATCH 4/9] Added support for Django 1.5 for TokenAuth --- rest_framework/authtoken/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/authtoken/models.py b/rest_framework/authtoken/models.py index fa1f4ea61..3b8bffeb8 100644 --- a/rest_framework/authtoken/models.py +++ b/rest_framework/authtoken/models.py @@ -7,7 +7,7 @@ from django import VERSION if VERSION[:2] in ((1, 5,),): from django.conf import settings - if hasattr(settings, AUTH_USER_MODEL): + if hasattr(settings, 'AUTH_USER_MODEL'): user_model = settings.AUTH_USER_MODEL else: from django.contrib.auth.models import User as user_model From 8eb4bb8090a84282c3537641e8ecb5c38a33fc41 Mon Sep 17 00:00:00 2001 From: Jonas Liljestrand Date: Sat, 17 Nov 2012 20:35:15 +0100 Subject: [PATCH 5/9] Moved function for getting correct user model to compat.py --- rest_framework/authtoken/models.py | 17 ++--------------- rest_framework/compat.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/rest_framework/authtoken/models.py b/rest_framework/authtoken/models.py index 3b8bffeb8..4da2aa625 100644 --- a/rest_framework/authtoken/models.py +++ b/rest_framework/authtoken/models.py @@ -1,29 +1,16 @@ import uuid import hmac from hashlib import sha1 +from rest_framework.compat import User from django.db import models -from django import VERSION -if VERSION[:2] in ((1, 5,),): - from django.conf import settings - if hasattr(settings, 'AUTH_USER_MODEL'): - user_model = settings.AUTH_USER_MODEL - else: - from django.contrib.auth.models import User as user_model -else: - try: - from django.db.models.auth import User as user_model - except ImportError: - raise ImportError('User model is not to be found.') - - class Token(models.Model): """ The default authorization token model. """ key = models.CharField(max_length=40, primary_key=True) - user = models.OneToOneField(user_model, related_name='auth_token') + user = models.OneToOneField(User, related_name='auth_token') created = models.DateTimeField(auto_now_add=True) def save(self, *args, **kwargs): diff --git a/rest_framework/compat.py b/rest_framework/compat.py index e38e7c33a..d5ad2a7aa 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -27,6 +27,20 @@ def get_concrete_model(model_cls): return model_cls +# Django 1.5 add support for custom auth user model +if django.VERSION >= (1, 5): + from django.conf import settings + if hasattr(settings, 'AUTH_USER_MODEL'): + User = settings.AUTH_USER_MODEL + else: + from django.contrib.auth.models import User +else: + try: + from django.db.models.auth import User + except ImportError: + raise ImportError('User model is not to be found.') + + # First implementation of Django class-based views did not include head method # in base View class - https://code.djangoproject.com/ticket/15668 if django.VERSION >= (1, 4): From 9f378d0dd44789cb98f3409604d9130d7a0032a8 Mon Sep 17 00:00:00 2001 From: Jonas Liljestrand Date: Sat, 17 Nov 2012 23:51:05 +0100 Subject: [PATCH 6/9] fixed bug --- rest_framework/compat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index d5ad2a7aa..2f59be95a 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -36,7 +36,7 @@ if django.VERSION >= (1, 5): from django.contrib.auth.models import User else: try: - from django.db.models.auth import User + from django.contrib.auth.models import User except ImportError: raise ImportError('User model is not to be found.') From 91c0249c9d622670252030cb36ea872c08d91471 Mon Sep 17 00:00:00 2001 From: Jonas Liljestrand Date: Sun, 18 Nov 2012 21:12:06 +0100 Subject: [PATCH 7/9] fixed migration to support django 1.5 --- rest_framework/authtoken/migrations/0001_initial.py | 3 ++- rest_framework/compat.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/rest_framework/authtoken/migrations/0001_initial.py b/rest_framework/authtoken/migrations/0001_initial.py index 9d750381d..2c0c40b10 100644 --- a/rest_framework/authtoken/migrations/0001_initial.py +++ b/rest_framework/authtoken/migrations/0001_initial.py @@ -3,6 +3,7 @@ import datetime from south.db import db from south.v2 import SchemaMigration from django.db import models +from rest_framework.compat import User class Migration(SchemaMigration): @@ -11,7 +12,7 @@ class Migration(SchemaMigration): # Adding model 'Token' db.create_table('authtoken_token', ( ('key', self.gf('django.db.models.fields.CharField')(max_length=40, primary_key=True)), - ('user', self.gf('django.db.models.fields.related.OneToOneField')(related_name='auth_token', unique=True, to=orm['auth.User'])), + ('user', self.gf('django.db.models.fields.related.OneToOneField')(related_name='auth_token', unique=True, to=orm["%s.%s" % (User._meta.app_label, User._meta.object_name)])), ('created', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, blank=True)), )) db.send_create_signal('authtoken', ['Token']) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 2f59be95a..09b763681 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -38,7 +38,7 @@ else: try: from django.contrib.auth.models import User except ImportError: - raise ImportError('User model is not to be found.') + raise ImportError(u"User model is not to be found.") # First implementation of Django class-based views did not include head method From 0ce792eac2f234d3858ff9e892845715b7460a4f Mon Sep 17 00:00:00 2001 From: Jonas Liljestrand Date: Mon, 19 Nov 2012 11:19:45 +0100 Subject: [PATCH 8/9] Update rest_framework/authtoken/migrations/0001_initial.py revert migration --- rest_framework/authtoken/migrations/0001_initial.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rest_framework/authtoken/migrations/0001_initial.py b/rest_framework/authtoken/migrations/0001_initial.py index 2c0c40b10..df6749fbb 100644 --- a/rest_framework/authtoken/migrations/0001_initial.py +++ b/rest_framework/authtoken/migrations/0001_initial.py @@ -3,7 +3,6 @@ import datetime from south.db import db from south.v2 import SchemaMigration from django.db import models -from rest_framework.compat import User class Migration(SchemaMigration): @@ -12,7 +11,7 @@ class Migration(SchemaMigration): # Adding model 'Token' db.create_table('authtoken_token', ( ('key', self.gf('django.db.models.fields.CharField')(max_length=40, primary_key=True)), - ('user', self.gf('django.db.models.fields.related.OneToOneField')(related_name='auth_token', unique=True, to=orm["%s.%s" % (User._meta.app_label, User._meta.object_name)])), + ('user', self.gf('django.db.models.fields.related.OneToOneField')(related_name='auth_token', unique=True, to=orm["auth.User"])), ('created', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, blank=True)), )) db.send_create_signal('authtoken', ['Token']) From 0bcc840927ab08d0e7d64844f3242036a142113d Mon Sep 17 00:00:00 2001 From: Jonas Liljestrand Date: Mon, 19 Nov 2012 11:37:37 +0100 Subject: [PATCH 9/9] Complete fix for migration --- .../authtoken/migrations/0001_initial.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/rest_framework/authtoken/migrations/0001_initial.py b/rest_framework/authtoken/migrations/0001_initial.py index 2c0c40b10..f4e052e48 100644 --- a/rest_framework/authtoken/migrations/0001_initial.py +++ b/rest_framework/authtoken/migrations/0001_initial.py @@ -3,7 +3,14 @@ import datetime from south.db import db from south.v2 import SchemaMigration from django.db import models -from rest_framework.compat import User + + +try: + from django.contrib.auth import get_user_model +except ImportError: # django < 1.5 + from django.contrib.auth.models import User +else: + User = get_user_model() class Migration(SchemaMigration): @@ -12,7 +19,7 @@ class Migration(SchemaMigration): # Adding model 'Token' db.create_table('authtoken_token', ( ('key', self.gf('django.db.models.fields.CharField')(max_length=40, primary_key=True)), - ('user', self.gf('django.db.models.fields.related.OneToOneField')(related_name='auth_token', unique=True, to=orm["%s.%s" % (User._meta.app_label, User._meta.object_name)])), + ('user', self.gf('django.db.models.fields.related.OneToOneField')(related_name='auth_token', unique=True, to=orm['%s.%s' % (User._meta.app_label, User._meta.object_name)])), ('created', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, blank=True)), )) db.send_create_signal('authtoken', ['Token']) @@ -37,7 +44,7 @@ class Migration(SchemaMigration): 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) }, - 'auth.user': { + "%s.%s" % (User._meta.app_label, User._meta.module_name): { 'Meta': {'object_name': 'User'}, 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}), @@ -57,7 +64,7 @@ class Migration(SchemaMigration): 'Meta': {'object_name': 'Token'}, 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), 'key': ('django.db.models.fields.CharField', [], {'max_length': '40', 'primary_key': 'True'}), - 'user': ('django.db.models.fields.related.OneToOneField', [], {'related_name': "'auth_token'", 'unique': 'True', 'to': "orm['auth.User']"}) + 'user': ('django.db.models.fields.related.OneToOneField', [], {'related_name': "'auth_token'", 'unique': 'True', 'to': "orm['%s.%s']" % (User._meta.app_label, User._meta.object_name)}) }, 'contenttypes.contenttype': { 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},