2015-12-16 15:06:06 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-12-17 18:44:10 +01:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2015-12-16 15:06:06 +01:00
|
|
|
from django.db import migrations
|
2015-12-16 17:05:25 +01:00
|
|
|
from django.db.utils import IntegrityError
|
2022-03-17 03:29:09 +01:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2015-12-16 15:06:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
def create_and_assign_permissions(apps, schema_editor):
|
2023-11-10 01:22:13 +01:00
|
|
|
db_alias = schema_editor.connection.alias
|
2015-12-16 15:06:06 +01:00
|
|
|
Permission = apps.get_model('auth', 'Permission')
|
|
|
|
ContentType = apps.get_model('contenttypes', 'ContentType')
|
2015-12-18 14:35:50 +01:00
|
|
|
# Two steps:
|
2015-12-16 15:06:06 +01:00
|
|
|
# 1. Create the permission for existing Queues
|
|
|
|
# 2. Assign the permission to user according to QueueMembership objects
|
|
|
|
|
|
|
|
# First step: prepare the permission for each queue
|
|
|
|
Queue = apps.get_model('helpdesk', 'Queue')
|
2023-11-10 01:22:13 +01:00
|
|
|
for q in Queue.objects.using(db_alias).all():
|
2015-12-16 17:05:25 +01:00
|
|
|
if not q.permission_name:
|
2015-12-17 18:11:02 +01:00
|
|
|
basename = "queue_access_%s" % q.slug
|
|
|
|
q.permission_name = "helpdesk.%s" % basename
|
2015-12-16 17:05:25 +01:00
|
|
|
else:
|
2015-12-17 18:11:02 +01:00
|
|
|
# Strip the `helpdesk.` prefix
|
2015-12-16 17:05:25 +01:00
|
|
|
basename = q.permission_name[9:]
|
|
|
|
|
|
|
|
try:
|
2023-11-10 01:22:13 +01:00
|
|
|
Permission.objects.using(db_alias).create(
|
2015-12-16 17:05:25 +01:00
|
|
|
name=_("Permission for queue: ") + q.title,
|
2023-11-10 01:22:13 +01:00
|
|
|
content_type=ContentType.objects.using(db_alias).get(model="queue"),
|
2015-12-16 17:05:25 +01:00
|
|
|
codename=basename,
|
|
|
|
)
|
|
|
|
except IntegrityError:
|
|
|
|
# Seems that it already existed, safely ignore it
|
|
|
|
pass
|
2015-12-16 15:06:06 +01:00
|
|
|
q.save()
|
|
|
|
|
|
|
|
# Second step: map the permissions according to QueueMembership
|
|
|
|
QueueMembership = apps.get_model('helpdesk', 'QueueMembership')
|
2023-11-10 01:22:13 +01:00
|
|
|
for qm in QueueMembership.objects.using(db_alias).all():
|
2015-12-16 15:06:06 +01:00
|
|
|
user = qm.user
|
|
|
|
for q in qm.queues.all():
|
|
|
|
# Strip the `helpdesk.` prefix
|
2023-11-10 01:22:13 +01:00
|
|
|
p = Permission.objects.using(db_alias).get(codename=q.permission_name[9:])
|
2015-12-16 15:06:06 +01:00
|
|
|
user.user_permissions.add(p)
|
|
|
|
qm.delete()
|
|
|
|
|
|
|
|
|
2015-12-17 18:44:10 +01:00
|
|
|
def revert_queue_membership(apps, schema_editor):
|
2023-11-10 01:22:13 +01:00
|
|
|
db_alias = schema_editor.connection.alias
|
2015-12-17 18:44:10 +01:00
|
|
|
Permission = apps.get_model('auth', 'Permission')
|
|
|
|
Queue = apps.get_model('helpdesk', 'Queue')
|
|
|
|
QueueMembership = apps.get_model('helpdesk', 'QueueMembership')
|
2023-11-10 01:22:13 +01:00
|
|
|
for p in Permission.objects.using(db_alias).all():
|
2015-12-17 18:44:10 +01:00
|
|
|
if p.codename.startswith("queue_access_"):
|
|
|
|
slug = p.codename[13:]
|
|
|
|
try:
|
2023-11-10 01:22:13 +01:00
|
|
|
q = Queue.objects.using(db_alias).get(slug=slug)
|
2015-12-17 18:44:10 +01:00
|
|
|
except ObjectDoesNotExist:
|
|
|
|
continue
|
|
|
|
|
|
|
|
for user in p.user_set.all():
|
2023-11-10 01:22:13 +01:00
|
|
|
qm, _ = QueueMembership.objects.using(db_alias).get_or_create(user=user)
|
2015-12-17 18:44:10 +01:00
|
|
|
qm.queues.add(q)
|
|
|
|
|
|
|
|
p.delete()
|
|
|
|
|
|
|
|
|
2015-12-16 15:06:06 +01:00
|
|
|
class Migration(migrations.Migration):
|
|
|
|
|
|
|
|
dependencies = [
|
|
|
|
('helpdesk', '0008_extra_for_permissions'),
|
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
2015-12-17 18:44:10 +01:00
|
|
|
migrations.RunPython(create_and_assign_permissions,
|
|
|
|
revert_queue_membership)
|
2015-12-16 15:06:06 +01:00
|
|
|
]
|