Commits

Peter Sagerson committed 03f3d75

Django 1.5 compatibility.

  • Participants
  • Parent commits 6a483c2

Comments (0)

Files changed (16)

File django-otp/django_otp/fixtures/django_otp/alice_and_bob.yaml

+- fields:
+    date_joined: 2012-07-15 16:03:02.420965
+    email: alice@example.com
+    first_name: ''
+    groups: []
+    is_active: true
+    is_staff: false
+    is_superuser: false
+    last_login: 2012-07-17 19:00:09.753345
+    last_name: ''
+    password: sha1$76223$c4f1daa1f2f3a7a3a6737538873ba335c60b5b7d
+    user_permissions: []
+    username: alice
+  model: auth.user
+  pk: 1
+
+- fields:
+    date_joined: 2012-07-15 16:03:02.420965
+    email: bob@example.com
+    first_name: ''
+    groups: []
+    is_active: true
+    is_staff: false
+    is_superuser: false
+    last_login: 2012-07-17 19:00:09.753345
+    last_name: ''
+    password: sha1$76223$c4f1daa1f2f3a7a3a6737538873ba335c60b5b7d
+    user_permissions: []
+    username: bob
+  model: auth.user
+  pk: 2
+
+- fields: {confirmed: true, name: Emergency Tokens, user: 1}
+  model: otp_static.staticdevice
+  pk: 1
+- fields: {device: 1, token: alice1}
+  model: otp_static.statictoken
+  pk: 1
+- fields: {device: 1, token: alice1}
+  model: otp_static.statictoken
+  pk: 2
+- fields: {device: 1, token: alice2}
+  model: otp_static.statictoken
+  pk: 3
+
+- fields: {confirmed: true, name: Emergency Tokens, user: 2}
+  model: otp_static.staticdevice
+  pk: 10
+- fields: {device: 10, token: bob1}
+  model: otp_static.statictoken
+  pk: 11
+- fields: {device: 10, token: bob1}
+  model: otp_static.statictoken
+  pk: 12
+- fields: {device: 10, token: bob2}
+  model: otp_static.statictoken
+  pk: 13
+
+- fields: 
+    confirmed: true
+    key: c2153fc45ab56736a27fce502496c3ebd4ae2725
+    name: Console
+    user: 1
+  model: otp_email.emaildevice
+  pk: 1

File django-otp/django_otp/fixtures/tests/alice_and_bob.yaml

-- fields:
-    date_joined: 2012-07-15 16:03:02.420965
-    email: alice@example.com
-    first_name: ''
-    groups: []
-    is_active: true
-    is_staff: false
-    is_superuser: false
-    last_login: 2012-07-17 19:00:09.753345
-    last_name: ''
-    password: sha1$76223$c4f1daa1f2f3a7a3a6737538873ba335c60b5b7d
-    user_permissions: []
-    username: alice
-  model: auth.user
-  pk: 1
-
-- fields:
-    date_joined: 2012-07-15 16:03:02.420965
-    email: bob@example.com
-    first_name: ''
-    groups: []
-    is_active: true
-    is_staff: false
-    is_superuser: false
-    last_login: 2012-07-17 19:00:09.753345
-    last_name: ''
-    password: sha1$76223$c4f1daa1f2f3a7a3a6737538873ba335c60b5b7d
-    user_permissions: []
-    username: bob
-  model: auth.user
-  pk: 2
-
-- fields: {confirmed: true, name: Emergency Tokens, user: 1}
-  model: otp_static.staticdevice
-  pk: 1
-- fields: {device: 1, token: alice1}
-  model: otp_static.statictoken
-  pk: 1
-- fields: {device: 1, token: alice1}
-  model: otp_static.statictoken
-  pk: 2
-- fields: {device: 1, token: alice2}
-  model: otp_static.statictoken
-  pk: 3
-
-- fields: {confirmed: true, name: Emergency Tokens, user: 2}
-  model: otp_static.staticdevice
-  pk: 10
-- fields: {device: 10, token: bob1}
-  model: otp_static.statictoken
-  pk: 11
-- fields: {device: 10, token: bob1}
-  model: otp_static.statictoken
-  pk: 12
-- fields: {device: 10, token: bob2}
-  model: otp_static.statictoken
-  pk: 13
-
-- fields: 
-    confirmed: true
-    key: c2153fc45ab56736a27fce502496c3ebd4ae2725
-    name: Console
-    user: 1
-  model: otp_email.emaildevice
-  pk: 1

File django-otp/django_otp/models.py

 
     .. attribute:: user
 
-        *ForeignKey*: Foreign key to :class:`~django.contrib.auth.models.User`.
+        *ForeignKey*: Foreign key to your user model, as configured by
+        :setting:`AUTH_USER_MODEL` (:class:`~django.contrib.auth.models.User`
+        by default).
 
     .. attribute:: name
 

File django-otp/django_otp/plugins/otp_static/management/commands/addstatictoken.py

 from optparse import make_option
 from textwrap import fill
 
+from django.core.exceptions import ObjectDoesNotExist
 from django.core.management.base import BaseCommand, CommandError
-from django.contrib.auth.models import User
+try:
+    from django.contrib.auth import get_user_model
+except ImportError:
+    from django.contrib.auth.models import User
+    get_user_model = lambda: User
 
 from django_otp.plugins.otp_static.models import StaticDevice, StaticToken
 
         make_option('-t', '--token', dest='token', help=u'The token to add. If omitted, one will be randomly generated.'),
     )
     args = u'<username>'
-    help = fill(u'Adds a single static OTP token to the given user. ' \
-        u'The token will be added to an arbitrary static device ' \
+    help = fill(u'Adds a single static OTP token to the given user. '
+        u'The token will be added to an arbitrary static device '
         u'attached to the user, creating one if necessary.', width=78)
 
     def handle(self, *args, **options):
         username = args[0]
 
         try:
-            user = User.objects.get(username=username)
-        except User.DoesNotExist:
+            user = get_user_model().objects.get_by_natural_key(username)
+        except ObjectDoesNotExist:
             raise CommandError('User "{0}" does not exist.'.format(username))
 
         device = next(StaticDevice.objects.filter(user=user).iterator(), None)

File django-otp/django_otp/tests/forms.py

 
 
 class AuthFormTest(TestCase):
-    fixtures = ['tests/alice_and_bob.yaml']
+    fixtures = ['django_otp/alice_and_bob.yaml']
 
     def test_empty(self):
         data = {}

File django-otp/test15/README

+Additional tests for Django 1.5. There's no way to set AUTH_USER_MODEL for
+individual tests, so we need a separate project.

File django-otp/test15/django_otp

+../django_otp

File django-otp/test15/manage.py

+#!/usr/bin/env python
+import os
+import sys
+
+if __name__ == "__main__":
+    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test15.settings")
+
+    from django.core.management import execute_from_command_line
+
+    execute_from_command_line(sys.argv)

File django-otp/test15/test15/__init__.py

Empty file added.

File django-otp/test15/test15/app/__init__.py

Empty file added.

File django-otp/test15/test15/app/fixtures/alice_and_bob.yaml

+- fields:
+    last_login: 2012-07-17 19:00:09.753345
+    password: sha1$76223$c4f1daa1f2f3a7a3a6737538873ba335c60b5b7d
+    identifier: alice
+  model: app.testuser
+  pk: 1
+
+- fields:
+    last_login: 2012-07-17 19:00:09.753345
+    password: sha1$76223$c4f1daa1f2f3a7a3a6737538873ba335c60b5b7d
+    identifier: bob
+  model: app.testuser
+  pk: 2
+
+- fields: {confirmed: true, name: Emergency Tokens, user: 1}
+  model: otp_static.staticdevice
+  pk: 1
+- fields: {device: 1, token: alice1}
+  model: otp_static.statictoken
+  pk: 1
+- fields: {device: 1, token: alice1}
+  model: otp_static.statictoken
+  pk: 2
+- fields: {device: 1, token: alice2}
+  model: otp_static.statictoken
+  pk: 3
+
+- fields: {confirmed: true, name: Emergency Tokens, user: 2}
+  model: otp_static.staticdevice
+  pk: 10
+- fields: {device: 10, token: bob1}
+  model: otp_static.statictoken
+  pk: 11
+- fields: {device: 10, token: bob1}
+  model: otp_static.statictoken
+  pk: 12
+- fields: {device: 10, token: bob2}
+  model: otp_static.statictoken
+  pk: 13

File django-otp/test15/test15/app/models.py

+from django.contrib.auth.hashers import make_password
+from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
+from django.db import models
+
+
+class TestUserManager(BaseUserManager):
+    def create_user(self, identifier, password=None):
+        if password is not None:
+            password = make_password(password)
+
+        return self.create(identifier=identifier, password=password)
+
+    create_superuser = create_user
+
+
+class TestUser(AbstractBaseUser):
+    identifier = models.CharField(max_length=40, unique=True, db_index=True)
+
+    objects = TestUserManager()
+
+    USERNAME_FIELD = 'identifier'
+
+    def get_full_name(self):
+        return self.identifier
+
+    def get_short_name(self):
+        return self.identifier

File django-otp/test15/test15/app/tests.py

+from django.test import TestCase
+
+from django_otp.forms import OTPAuthenticationForm
+from .models import TestUser
+
+
+class AuthFormTest(TestCase):
+    fixtures = ['alice_and_bob.yaml']
+
+    def test_custom_user(self):
+        data = {
+            'username': 'alice',
+            'password': 'password',
+            'otp_device': 'django_otp.plugins.otp_static.models.StaticDevice/1',
+            'otp_token': 'alice1',
+        }
+        form = OTPAuthenticationForm(None, data)
+
+        self.assert_(form.is_valid())
+        alice = form.get_user()
+        self.assert_(isinstance(alice, TestUser))
+        if hasattr(alice, 'get_username'):
+            self.assertEqual(alice.get_username(), 'alice')
+        else:
+            self.assertEqual(alice.username, 'alice')
+        self.assert_(alice.otp_device is not None)

File django-otp/test15/test15/settings.py

+from os.path import dirname, join, abspath
+
+
+def project_path(path):
+    return abspath(join(dirname(__file__), path))
+
+
+DEBUG = True
+
+DATABASES = {
+    'default': {
+        'ENGINE': 'django.db.backends.sqlite3',
+        'NAME': 'db.sqlite3',
+    }
+}
+
+INSTALLED_APPS = [
+    'django.contrib.auth',
+    'django.contrib.contenttypes',
+    'django.contrib.sessions',
+    'django.contrib.messages',
+
+    'django_otp',
+    'django_otp.plugins.otp_static',
+
+    'test15.app',
+]
+
+MIDDLEWARE_CLASSES = [
+    'django.middleware.common.CommonMiddleware',
+    'django.contrib.sessions.middleware.SessionMiddleware',
+    'django.contrib.auth.middleware.AuthenticationMiddleware',
+    'django_otp.middleware.OTPMiddleware',
+    'django.contrib.messages.middleware.MessageMiddleware',
+]
+
+TEMPLATE_DIRS = [
+    project_path('templates'),
+]
+
+SECRET_KEY = '9xZjgb6lM998dPRNQ3j7au86X5ZL17Jtme5N910Cp06u7j0QLWara6BH7N90clGQ'
+
+ROOT_URLCONF = 'test15.urls'
+
+AUTH_USER_MODEL = 'app.TestUser'

File django-otp/test15/test15/urls.py

+from django.conf.urls import patterns
+
+
+urlpatterns = patterns('')

File django-otp/test15/test15/wsgi.py

+"""
+WSGI config for test15 project.
+
+This module contains the WSGI application used by Django's development server
+and any production WSGI deployments. It should expose a module-level variable
+named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover
+this application via the ``WSGI_APPLICATION`` setting.
+
+Usually you will have the standard Django WSGI application here, but it also
+might make sense to replace the whole Django WSGI application with a custom one
+that later delegates to the Django one. For example, you could introduce WSGI
+middleware here, or combine a Django application with an application of another
+framework.
+
+"""
+import os
+
+# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
+# if running multiple sites in the same mod_wsgi process. To fix this, use
+# mod_wsgi daemon mode with each site in its own daemon process, or use
+# os.environ["DJANGO_SETTINGS_MODULE"] = "test15.settings"
+os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test15.settings")
+
+# This application object is used by any WSGI server configured to use this
+# file. This includes Django's development server, if the WSGI_APPLICATION
+# setting points here.
+from django.core.wsgi import get_wsgi_application
+application = get_wsgi_application()
+
+# Apply WSGI middleware here.
+# from helloworld.wsgi import HelloWorldApplication
+# application = HelloWorldApplication(application)