Skip to content

Commit

Permalink
Merge pull request #7 from supercodepoet/master
Browse files Browse the repository at this point in the history
Another option for updating the code for Django 1.5 custom user model
  • Loading branch information
jlward committed Jun 13, 2013
2 parents 9d85941 + 9c17430 commit 37a8e21
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@
*.egg-info
*.sql
docs/build/*
.DS_Store
20 changes: 20 additions & 0 deletions authority/fixtures/tests_custom.json
@@ -0,0 +1,20 @@
[
{
"pk": 1,
"model": "users.user",
"fields": {
"first_name": "Jez",
"last_name": "Dez",
"is_active": true,
"is_superuser": false,
"is_staff": false,
"last_login": "2009-11-02 03:06:19",
"groups": [],
"user_permissions": [],
"password": "",
"email": "jezdez@github.com",
"date_joined": "2009-11-02 03:06:19",
"greeting_message": "Hello customer user model"
}
}
]
4 changes: 3 additions & 1 deletion authority/forms.py
@@ -1,11 +1,13 @@
from django import forms
from django.utils.translation import ugettext_lazy as _
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User, Group
from django.contrib.auth.models import Group
from django.utils.safestring import mark_safe

from authority import permissions, get_choices_for
from authority.models import Permission
from authority.utils import User


class BasePermissionForm(forms.ModelForm):
codename = forms.CharField(label=_('Permission'))
Expand Down
3 changes: 3 additions & 0 deletions authority/models.py
@@ -1,11 +1,14 @@
from datetime import datetime
from django.conf import settings
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.contrib.auth.models import User, Group
from django.utils.translation import ugettext_lazy as _

from authority.managers import PermissionManager
from authority.utils import User


class Permission(models.Model):
"""
Expand Down
5 changes: 4 additions & 1 deletion authority/templatetags/permissions.py
@@ -1,16 +1,19 @@
from django import template
from django.core.urlresolvers import reverse
from django.core.exceptions import ImproperlyConfigured
from django.contrib.auth.models import User, AnonymousUser
from django.contrib.auth.models import AnonymousUser
from django.core.urlresolvers import reverse

from authority import get_check
from authority import permissions
from authority.models import Permission
from authority.forms import UserPermissionForm
from authority.utils import User


register = template.Library()


@register.simple_tag
def url_for_obj(view_name, obj):
return reverse(view_name, kwargs={
Expand Down
36 changes: 24 additions & 12 deletions authority/tests.py
@@ -1,13 +1,25 @@
from django import VERSION
from django.conf import settings
from django.contrib import auth
from django.contrib.auth.models import Permission as DjangoPermission
from django.contrib.auth.models import User, Group
from django.contrib.auth.models import Group
from django.db.models import Q
from django.test import TestCase
from django.contrib.contenttypes.models import ContentType

import authority
from authority import permissions
from authority.models import Permission
from authority.exceptions import NotAModel, UnsavedModelInstance
from authority.utils import User


if VERSION >= (1, 5):
FIXTURES = ['tests_custom.json']
QUERY = Q(email="jezdez@github.com")
else:
FIXTURES = ['tests.json']
QUERY = Q(username="jezdez")


class UserPermission(permissions.BasePermission):
Expand All @@ -34,10 +46,10 @@ class DjangoPermissionChecksTestCase(TestCase):
This permissions are given in the test case and not in the fixture, for
later reference.
"""
fixtures = ['tests.json']
fixtures = FIXTURES

def setUp(self):
self.user = User.objects.get(username='jezdez')
self.user = User.objects.get(QUERY)
self.check = UserPermission(self.user)

def test_no_permission(self):
Expand Down Expand Up @@ -65,7 +77,7 @@ def test_delete(self):
# test
self.assertFalse(self.check.delete_user())
self.assertTrue(self.check.delete_user(self.user))


class AssignBehaviourTest(TestCase):
"""
Expand All @@ -74,10 +86,10 @@ class AssignBehaviourTest(TestCase):
- permission delete_user for him (test_delete),
- all existing codenames permissions: a/b/c/d (test_all),
"""
fixtures = ['tests.json']
fixtures = FIXTURES

def setUp(self):
self.user = User.objects.get(username='jezdez')
self.user = User.objects.get(QUERY)
self.check = UserPermission(self.user)

def test_add(self):
Expand Down Expand Up @@ -111,10 +123,10 @@ class GenericAssignBehaviourTest(TestCase):
- permission add (test_add),
- permission delete for him (test_delete),
"""
fixtures = ['tests.json']
fixtures = FIXTURES

def setUp(self):
self.user = User.objects.get(username='jezdez')
self.user = User.objects.get(QUERY)
self.check = UserPermission(self.user)

def test_add(self):
Expand All @@ -140,10 +152,10 @@ class AssignExceptionsTest(TestCase):
Tests that exceptions are thrown if assign() was called with inconsistent
arguments.
"""
fixtures = ['tests.json']
fixtures = FIXTURES

def setUp(self):
self.user = User.objects.get(username='jezdez')
self.user = User.objects.get(QUERY)
self.check = UserPermission(self.user)

def test_unsaved_model(self):
Expand All @@ -165,11 +177,11 @@ class SmartCachingTestCase(TestCase):
"""
The base test case for all tests that have to do with smart caching.
"""
fixtures = ['tests.json']
fixtures = FIXTURES

def setUp(self):
# Create a user.
self.user = User.objects.get(username='jezdez')
self.user = User.objects.get(QUERY)

# Create a group.
self.group = Group.objects.create()
Expand Down
11 changes: 11 additions & 0 deletions authority/utils.py
@@ -0,0 +1,11 @@
from django.contrib import auth


def get_user_class():
if hasattr(auth, "get_user_model"):
return auth.get_user_model()
else:
return auth.models.User


User = get_user_class()
6 changes: 6 additions & 0 deletions example/settings.py
@@ -1,5 +1,7 @@
import os

from django import VERSION

PROJECT_ROOT = os.path.realpath(os.path.dirname(__file__))

ADMINS = (
Expand Down Expand Up @@ -79,6 +81,10 @@
'example.exampleapp',
)

if VERSION >= (1, 5):
INSTALLED_APPS = INSTALLED_APPS + ('example.users',)
AUTH_USER_MODEL = 'users.User'

TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
Expand Down
Empty file added example/users/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions example/users/admin.py
@@ -0,0 +1,5 @@
from django.contrib.auth.admin import UserAdmin
from example.users.User


admin.site.register(User, UserAdmin)
16 changes: 16 additions & 0 deletions example/users/models.py
@@ -0,0 +1,16 @@
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.db import models
from django.utils import timezone


class User(AbstractBaseUser, PermissionsMixin):
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name']

first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField(unique=True)
greeting_message = models.TextField()
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
date_joined = models.DateTimeField(default=timezone.now)

0 comments on commit 37a8e21

Please sign in to comment.