django-authority/authority/models.py
Jannis Leidel 71eee85b85 Moved authority app from src/ to root directory
--HG--
rename : src/authority/__init__.py => authority/__init__.py
rename : src/authority/admin.py => authority/admin.py
rename : src/authority/decorators.py => authority/decorators.py
rename : src/authority/exceptions.py => authority/exceptions.py
rename : src/authority/fixtures/tests.json => authority/fixtures/tests.json
rename : src/authority/forms.py => authority/forms.py
rename : src/authority/managers.py => authority/managers.py
rename : src/authority/models.py => authority/models.py
rename : src/authority/permissions.py => authority/permissions.py
rename : src/authority/sites.py => authority/sites.py
rename : src/authority/templates/admin/edit_inline/action_tabular.html => authority/templates/admin/edit_inline/action_tabular.html
rename : src/authority/templates/admin/permission_change_form.html => authority/templates/admin/permission_change_form.html
rename : src/authority/templates/authority/403.html => authority/templates/authority/403.html
rename : src/authority/templates/authority/permission_delete_link.html => authority/templates/authority/permission_delete_link.html
rename : src/authority/templates/authority/permission_form.html => authority/templates/authority/permission_form.html
rename : src/authority/templates/authority/permission_request_approve_link.html => authority/templates/authority/permission_request_approve_link.html
rename : src/authority/templates/authority/permission_request_delete_link.html => authority/templates/authority/permission_request_delete_link.html
rename : src/authority/templatetags/__init__.py => authority/templatetags/__init__.py
rename : src/authority/templatetags/permissions.py => authority/templatetags/permissions.py
rename : src/authority/tests.py => authority/tests.py
rename : src/authority/urls.py => authority/urls.py
rename : src/authority/views.py => authority/views.py
rename : src/authority/widgets.py => authority/widgets.py
2010-01-07 18:00:41 +01:00

58 lines
2.5 KiB
Python

from datetime import datetime
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
class Permission(models.Model):
"""
A granular permission model, per-object permission in other words.
This kind of permission is associated with a user/group and an object
of any content type.
"""
codename = models.CharField(_('codename'), max_length=100)
content_type = models.ForeignKey(ContentType, related_name="row_permissions")
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
user = models.ForeignKey(User, null=True, blank=True, related_name='granted_permissions')
group = models.ForeignKey(Group, null=True, blank=True)
creator = models.ForeignKey(User, null=True, blank=True, related_name='created_permissions')
approved = models.BooleanField(_('approved'), default=False, help_text=_("Designates whether the permission has been approved and treated as active. Unselect this instead of deleting permissions."))
date_requested = models.DateTimeField(_('date requested'), default=datetime.now)
date_approved = models.DateTimeField(_('date approved'), blank=True, null=True)
objects = PermissionManager()
def __unicode__(self):
return self.codename
class Meta:
unique_together = ("codename", "object_id", "content_type", "user", "group")
verbose_name = _('permission')
verbose_name_plural = _('permissions')
permissions = (
('change_foreign_permissions', 'Can change foreign permissions'),
('delete_foreign_permissions', 'Can delete foreign permissions'),
('approve_permission_requests', 'Can approve permission requests'),
)
def save(self, *args, **kwargs):
# Make sure the approval date is always set
if self.approved and not self.date_approved:
self.date_approved = datetime.now()
super(Permission, self).save(*args, **kwargs)
def approve(self, creator):
"""
Approve granular permission request setting a Permission entry as
approved=True for a specific action from an user on an object instance.
"""
self.approved = True
self.creator = creator
self.save()