Permalink
Browse files

compatibility with custom user models

  • Loading branch information...
1 parent 4cbfea6 commit da83ee611d8608c2c60e0935230e9b008517d67a @craigds craigds committed Oct 31, 2012
Showing with 46 additions and 44 deletions.
  1. +25 −25 cms/models/moderatormodels.py
  2. +21 −19 cms/models/permissionmodels.py
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
import sys
+from django.conf import settings
from django.db import models
-from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from cms.models.managers import PageModeratorStateManager
@@ -13,7 +13,7 @@
ACCESS_PAGE = 1
ACCESS_CHILDREN = 2 # just immediate children (1 level)
ACCESS_PAGE_AND_CHILDREN = 3 # just immediate children (1 level)
-ACCESS_DESCENDANTS = 4
+ACCESS_DESCENDANTS = 4
ACCESS_PAGE_AND_DESCENDANTS = 5
# binary masks for ACCESS permissions
@@ -41,85 +41,85 @@ class PageModerator(models.Model):
moderation depth he requires.
"""
MAX_MODERATION_LEVEL = sys.maxint # just an number
-
- page = models.ForeignKey(Page, verbose_name=_('Page'))
- user = models.ForeignKey(User, verbose_name=_('User'))
-
+
+ page = models.ForeignKey(Page, verbose_name=_('Page'))
+ user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('User'))
+
# TODO: permission stuff could be changed to this structure also, this gives
# better querying performance
moderate_page = models.BooleanField(_('Moderate page'), blank=True)
moderate_children = models.BooleanField(_('Moderate children'), blank=True)
moderate_descendants = models.BooleanField(_('Moderate descendants'), blank=True)
-
+
class Meta:
verbose_name=_('PageModerator')
verbose_name_plural=_('PageModerator')
app_label = 'cms'
-
+
def set_decimal(self, state):
"""Converts and sets binary state to local attributes
"""
self.moderate_page = bool(state & MASK_PAGE)
moderate_children = bool(state & MASK_CHILDREN)
moderate_descendants = bool(state & MASK_DESCENDANTS)
-
+
if moderate_descendants:
moderate_children = True
-
+
self.moderate_children = moderate_children
self.moderate_descendants = moderate_descendants
-
+
def get_decimal(self):
return self.moderate_page * MASK_PAGE + \
self.moderate_children * MASK_CHILDREN + \
self.moderate_descendants * MASK_DESCENDANTS
-
+
def __unicode__(self):
return u"%s on %s mod: %d" % (self.user, self.page, self.get_decimal())
class PageModeratorState(models.Model):
"""PageModeratorState memories all actions made on page.
- Page can be in only one advanced state.
+ Page can be in only one advanced state.
"""
ACTION_ADD = "ADD"
ACTION_CHANGED = "CHA"
-
+
ACTION_PUBLISH = "PUB"
ACTION_UNPUBLISH = "UNP"
ACTION_MOVE = "MOV"
-
+
# advanced states
ACTION_DELETE = "DEL"
-
+
# approve state
ACTION_APPROVE = "APP"
-
+
_action_choices = (
- (ACTION_ADD, _('created')),
- (ACTION_CHANGED, _('changed')),
+ (ACTION_ADD, _('created')),
+ (ACTION_CHANGED, _('changed')),
(ACTION_DELETE, _('delete req.')),
(ACTION_MOVE, _('move req.')),
(ACTION_PUBLISH, _('publish req.')),
(ACTION_UNPUBLISH, _('unpublish req.')),
(ACTION_APPROVE, _('approved')), # Approved by somebody in approvement process
)
-
+
page = models.ForeignKey(Page)
- user = models.ForeignKey(User, null=True)
+ user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True)
created = models.DateTimeField(auto_now_add=True)
action = models.CharField(max_length=3, choices=_action_choices, null=True, blank=True)
message = models.TextField(max_length=1000, blank=True, default="")
-
+
objects = PageModeratorStateManager()
-
+
class Meta:
verbose_name=_('Page moderator state')
verbose_name_plural=_('Page moderator states')
ordering = ('page', 'action', '-created') # newer first
app_label = 'cms'
-
+
css_class = lambda self: self.action.lower()
-
+
def __unicode__(self):
return u"%s: %s" % (self.page, self.get_action_display())
@@ -2,7 +2,9 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
-from django.contrib.auth.models import User, Group
+from django.conf import settings
+from django.contrib.auth import get_user_model
+from django.contrib.auth.models import Group
from django.contrib.sites.models import Site
from cms.models import Page, ACCESS_CHOICES, ACCESS_PAGE_AND_DESCENDANTS
@@ -13,9 +15,9 @@ class AbstractPagePermission(models.Model):
"""Abstract page permissions
"""
# who:
- user = models.ForeignKey(User, verbose_name=_("user"), blank=True, null=True)
+ user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_("user"), blank=True, null=True)
group = models.ForeignKey(Group, verbose_name=_("group"), blank=True, null=True)
-
+
# what:
can_change = models.BooleanField(_("can edit"), default=True)
can_add = models.BooleanField(_("can add"), default=True)
@@ -26,18 +28,18 @@ class AbstractPagePermission(models.Model):
can_move_page = models.BooleanField(_("can move"), default=True)
can_moderate = models.BooleanField(_("can moderate"), default=True)
can_view = models.BooleanField(_("view restricted"), default=False, help_text=_("frontend view restriction"))
-
+
class Meta:
abstract = True
app_label = 'cms'
-
+
@property
def audience(self):
"""Return audience by priority, so: All or User, Group
"""
targets = filter(lambda item: item, (self.user, self.group,))
return ", ".join([unicode(t) for t in targets]) or 'No one'
-
+
def save(self, *args, **kwargs):
if not self.user and not self.group:
# don't allow `empty` objects
@@ -50,52 +52,52 @@ class GlobalPagePermission(AbstractPagePermission):
"""
can_recover_page = models.BooleanField(_("can recover pages"), default=True, help_text=_("can recover any deleted page"))
sites = models.ManyToManyField(Site, null=True, blank=True, help_text=_('If none selected, user haves granted permissions to all sites.'), verbose_name=_('sites'))
-
+
objects = BasicPagePermissionManager()
-
+
class Meta:
verbose_name = _('Page global permission')
verbose_name_plural = _('Pages global permissions')
app_label = 'cms'
-
+
def __unicode__(self):
return "%s :: GLOBAL" % self.audience
class PagePermission(AbstractPagePermission):
"""Page permissions for single page
- """
+ """
grant_on = models.IntegerField(_("Grant on"), choices=ACCESS_CHOICES, default=ACCESS_PAGE_AND_DESCENDANTS)
page = models.ForeignKey(Page, null=True, blank=True, verbose_name=_("page"))
-
+
objects = PagePermissionManager()
-
+
class Meta:
verbose_name = _('Page permission')
verbose_name_plural = _('Page permissions')
app_label = 'cms'
-
+
def __unicode__(self):
page = self.page_id and unicode(self.page) or "None"
return "%s :: %s has: %s" % (page, self.audience, unicode(dict(ACCESS_CHOICES)[self.grant_on]))
-class PageUser(User):
+class PageUser(get_user_model()):
"""Cms specific user data, required for permission system
"""
- created_by = models.ForeignKey(User, related_name="created_users")
-
+ created_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="created_users")
+
class Meta:
verbose_name = _('User (page)')
verbose_name_plural = _('Users (page)')
app_label = 'cms'
class PageUserGroup(Group):
- """Cms specific group data, required for permission system
+ """Cms specific group data, required for permission system
"""
- created_by = models.ForeignKey(User, related_name="created_usergroups")
-
+ created_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="created_usergroups")
+
class Meta:
verbose_name = _('User group (page)')
verbose_name_plural = _('User groups (page)')

0 comments on commit da83ee6

Please sign in to comment.