more modular management of permission codenames

This commit is contained in:
Alex Barcelo 2015-12-16 17:05:25 +01:00
parent 830fcbe796
commit 92d8ca3eff
2 changed files with 35 additions and 17 deletions

View File

@ -2,6 +2,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import migrations from django.db import migrations
from django.conf import settings from django.conf import settings
from django.db.utils import IntegrityError
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -24,14 +25,20 @@ def create_and_assign_permissions(apps, schema_editor):
Queue = apps.get_model('helpdesk', 'Queue') Queue = apps.get_model('helpdesk', 'Queue')
for q in Queue.objects.all(): for q in Queue.objects.all():
# Prepare the permission associated to this Queue if not q.permission_name:
basename = "queue_access_%s" % q.slug basename = q.prepare_permission_name
q.permission_name = "helpdesk.%s" % basename else:
basename = q.permission_name[9:]
try:
Permission.objects.create( Permission.objects.create(
name=_("Permission for queue: ") + q.title, name=_("Permission for queue: ") + q.title,
content_type=ContentType.objects.get(model="queue"), content_type=ContentType.objects.get(model="queue"),
codename=basename, codename=basename,
) )
except IntegrityError:
# Seems that it already existed, safely ignore it
pass
q.save() q.save()
# Second step: map the permissions according to QueueMembership # Second step: map the permissions according to QueueMembership

View File

@ -240,6 +240,15 @@ class Queue(models.Model):
return u'%s <%s>' % (self.title, self.email_address) return u'%s <%s>' % (self.title, self.email_address)
from_address = property(_from_address) from_address = property(_from_address)
def prepare_permission_name(self):
"""Prepare internally the codename for the permission and store it in permission_name.
:return: The codename that can be used to create a new Permission object.
"""
# Prepare the permission associated to this Queue
basename = "queue_access_%s" % self.slug
self.permission_name = "helpdesk.%s" % basename
return basename
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
if self.email_box_type == 'imap' and not self.email_box_imap_folder: if self.email_box_type == 'imap' and not self.email_box_imap_folder:
self.email_box_imap_folder = 'INBOX' self.email_box_imap_folder = 'INBOX'
@ -263,10 +272,12 @@ class Queue(models.Model):
elif self.email_box_type == 'pop3' and not self.email_box_ssl: elif self.email_box_type == 'pop3' and not self.email_box_ssl:
self.email_box_port = 110 self.email_box_port = 110
if not self.id and helpdesk_settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_PERMISSION: if not self.id:
# Prepare the permission associated to this Queue # Always prepare the permission codename
basename = "queue_access_%s" % self.slug basename = self.prepare_permission_name()
self.permission_name = "helpdesk.%s" % basename
# Create the permission only if the flag is active
if helpdesk_settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_PERMISSION:
Permission.objects.create( Permission.objects.create(
name=_("Permission for queue: ") + self.title, name=_("Permission for queue: ") + self.title,
content_type=ContentType.objects.get(model="queue"), content_type=ContentType.objects.get(model="queue"),