Browse files

Merge pull request #110 from abec/master

AnonymousUser creation in Django 1.5
  • Loading branch information...
2 parents a0539b3 + 0e123b2 commit 29e119866aa85d892c7ab5d008f1b60ef63c1329 @lukaszb lukaszb committed Feb 26, 2013
Showing with 18 additions and 5 deletions.
  1. +1 −0 guardian/compat.py
  2. +1 −0 guardian/conf/settings.py
  3. +9 −3 guardian/management/__init__.py
  4. +5 −2 guardian/models.py
  5. +2 −0 guardian/utils.py
View
1 guardian/compat.py
@@ -70,3 +70,4 @@ def get_user_permission_codename(perm):
except NameError:
basestring = unicode = str = str
+__all__ = ['User', 'Group', 'Permission', 'AnonymousUser']
View
1 guardian/conf/settings.py
@@ -2,6 +2,7 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
+ANONYMOUS_DEFAULT_USERNAME_VALUE = getattr(settings, 'ANONYMOUS_DEFAULT_USERNAME_VALUE', 'AnonymousUser')
ANONYMOUS_USER_ID = getattr(settings, 'ANONYMOUS_USER_ID', None)
if ANONYMOUS_USER_ID is None:
raise ImproperlyConfigured("In order to use django-guardian's "
View
12 guardian/management/__init__.py
@@ -1,21 +1,27 @@
from __future__ import unicode_literals
+import django
from django.db.models import signals
+from django.conf import settings
from guardian import models as guardian_app
from guardian.conf import settings as guardian_settings
from guardian.compat import get_user_model
def create_anonymous_user(sender, **kwargs):
"""
- Creates anonymous User instance with id from settings.
+ Creates anonymous User instance with id and username from settings.
"""
User = get_user_model()
try:
User.objects.get(pk=guardian_settings.ANONYMOUS_USER_ID)
except User.DoesNotExist:
- User.objects.create(pk=guardian_settings.ANONYMOUS_USER_ID,
- username='AnonymousUser')
+ if django.VERSION >= (1, 5):
+ User.objects.create(pk=guardian_settings.ANONYMOUS_USER_ID,
+ **{User.USERNAME_FIELD: guardian_settings.ANONYMOUS_DEFAULT_USERNAME_VALUE})
+ else:
+ User.objects.create(pk=guardian_settings.ANONYMOUS_USER_ID,
+ username=guardian_settings.ANONYMOUS_DEFAULT_USERNAME_VALUE)
signals.post_syncdb.connect(create_anonymous_user, sender=guardian_app,
dispatch_uid="guardian.management.create_anonymous_user")
View
7 guardian/models.py
@@ -1,7 +1,11 @@
from __future__ import unicode_literals
+import django
from django.db import models
from django.core.exceptions import ValidationError
+from django.contrib.auth.models import Group
+from django.contrib.auth.models import Permission
+from django.contrib.auth.models import AnonymousUser
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.generic import GenericForeignKey
from django.utils.translation import ugettext_lazy as _
@@ -11,9 +15,9 @@
from guardian.compat import get_user_model
from guardian.compat import user_model_label
from guardian.compat import unicode
+from guardian.utils import get_anonymous_user
from guardian.managers import GroupObjectPermissionManager
from guardian.managers import UserObjectPermissionManager
-from guardian.utils import get_anonymous_user
class BaseObjectPermission(models.Model):
@@ -98,4 +102,3 @@ class Meta:
lambda self, perm, obj: GroupObjectPermission.objects.assign_perm(perm, self, obj))
setattr(Group, 'del_obj_perm',
lambda self, perm, obj: GroupObjectPermission.objects.remove_perm(perm, self, obj))
-
View
2 guardian/utils.py
@@ -24,6 +24,8 @@
from guardian.conf import settings as guardian_settings
from guardian.exceptions import NotUserNorGroup
+User = get_user_model()
+
logger = logging.getLogger(__name__)
abspath = lambda *p: os.path.abspath(os.path.join(*p))

0 comments on commit 29e1198

Please sign in to comment.