diff --git a/helpdesk/migrations/0009_migrate_queuemembership.py b/helpdesk/migrations/0009_migrate_queuemembership.py index 318b24ab..0b88bdee 100644 --- a/helpdesk/migrations/0009_migrate_queuemembership.py +++ b/helpdesk/migrations/0009_migrate_queuemembership.py @@ -6,6 +6,7 @@ from django.utils.translation import gettext_lazy as _ def create_and_assign_permissions(apps, schema_editor): + db_alias = schema_editor.connection.alias Permission = apps.get_model('auth', 'Permission') ContentType = apps.get_model('contenttypes', 'ContentType') # Two steps: @@ -14,8 +15,7 @@ def create_and_assign_permissions(apps, schema_editor): # First step: prepare the permission for each queue Queue = apps.get_model('helpdesk', 'Queue') - - for q in Queue.objects.all(): + for q in Queue.objects.using(db_alias).all(): if not q.permission_name: basename = "queue_access_%s" % q.slug q.permission_name = "helpdesk.%s" % basename @@ -24,9 +24,9 @@ def create_and_assign_permissions(apps, schema_editor): basename = q.permission_name[9:] try: - Permission.objects.create( + Permission.objects.using(db_alias).create( 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, ) except IntegrityError: @@ -36,29 +36,30 @@ def create_and_assign_permissions(apps, schema_editor): # Second step: map the permissions according to 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 for q in qm.queues.all(): # 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) qm.delete() def revert_queue_membership(apps, schema_editor): + db_alias = schema_editor.connection.alias Permission = apps.get_model('auth', 'Permission') Queue = apps.get_model('helpdesk', 'Queue') 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_"): slug = p.codename[13:] try: - q = Queue.objects.get(slug=slug) + q = Queue.objects.using(db_alias).get(slug=slug) except ObjectDoesNotExist: continue 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) p.delete()