Fix 0009_migrate_queuemembership.py

This commit is contained in:
tecbr 2023-11-09 21:22:13 -03:00 committed by GitHub
parent 9ed5ecf327
commit 5df7b8ff85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,6 +6,7 @@ from django.utils.translation import gettext_lazy as _
def create_and_assign_permissions(apps, schema_editor): def create_and_assign_permissions(apps, schema_editor):
db_alias = schema_editor.connection.alias
Permission = apps.get_model('auth', 'Permission') Permission = apps.get_model('auth', 'Permission')
ContentType = apps.get_model('contenttypes', 'ContentType') ContentType = apps.get_model('contenttypes', 'ContentType')
# Two steps: # Two steps:
@ -14,8 +15,7 @@ def create_and_assign_permissions(apps, schema_editor):
# First step: prepare the permission for each queue # First step: prepare the permission for each queue
Queue = apps.get_model('helpdesk', 'Queue') Queue = apps.get_model('helpdesk', 'Queue')
for q in Queue.objects.using(db_alias).all():
for q in Queue.objects.all():
if not q.permission_name: if not q.permission_name:
basename = "queue_access_%s" % q.slug basename = "queue_access_%s" % q.slug
q.permission_name = "helpdesk.%s" % basename q.permission_name = "helpdesk.%s" % basename
@ -24,9 +24,9 @@ def create_and_assign_permissions(apps, schema_editor):
basename = q.permission_name[9:] basename = q.permission_name[9:]
try: try:
Permission.objects.create( Permission.objects.using(db_alias).create(
name=_("Permission for queue: ") + q.title, name=_("Permission for queue: ") + q.title,
content_type=ContentType.objects.get(model="queue"), content_type=ContentType.objects.using(db_alias).get(model="queue"),
codename=basename, codename=basename,
) )
except IntegrityError: except IntegrityError:
@ -36,29 +36,30 @@ def create_and_assign_permissions(apps, schema_editor):
# Second step: map the permissions according to QueueMembership # Second step: map the permissions according to QueueMembership
QueueMembership = apps.get_model('helpdesk', 'QueueMembership') QueueMembership = apps.get_model('helpdesk', 'QueueMembership')
for qm in QueueMembership.objects.all(): for qm in QueueMembership.objects.using(db_alias).all():
user = qm.user user = qm.user
for q in qm.queues.all(): for q in qm.queues.all():
# Strip the `helpdesk.` prefix # Strip the `helpdesk.` prefix
p = Permission.objects.get(codename=q.permission_name[9:]) p = Permission.objects.using(db_alias).get(codename=q.permission_name[9:])
user.user_permissions.add(p) user.user_permissions.add(p)
qm.delete() qm.delete()
def revert_queue_membership(apps, schema_editor): def revert_queue_membership(apps, schema_editor):
db_alias = schema_editor.connection.alias
Permission = apps.get_model('auth', 'Permission') Permission = apps.get_model('auth', 'Permission')
Queue = apps.get_model('helpdesk', 'Queue') Queue = apps.get_model('helpdesk', 'Queue')
QueueMembership = apps.get_model('helpdesk', 'QueueMembership') QueueMembership = apps.get_model('helpdesk', 'QueueMembership')
for p in Permission.objects.all(): for p in Permission.objects.using(db_alias).all():
if p.codename.startswith("queue_access_"): if p.codename.startswith("queue_access_"):
slug = p.codename[13:] slug = p.codename[13:]
try: try:
q = Queue.objects.get(slug=slug) q = Queue.objects.using(db_alias).get(slug=slug)
except ObjectDoesNotExist: except ObjectDoesNotExist:
continue continue
for user in p.user_set.all(): for user in p.user_set.all():
qm, _ = QueueMembership.objects.get_or_create(user=user) qm, _ = QueueMembership.objects.using(db_alias).get_or_create(user=user)
qm.queues.add(q) qm.queues.add(q)
p.delete() p.delete()