From 5df7b8ff85bb90305b1d87643c311ca5bbf04326 Mon Sep 17 00:00:00 2001 From: tecbr <17889776+tecbr@users.noreply.github.com> Date: Thu, 9 Nov 2023 21:22:13 -0300 Subject: [PATCH 1/2] Fix 0009_migrate_queuemembership.py --- .../0009_migrate_queuemembership.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) 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() From f1d530db2ab8d5ac82afe260de9bd319d90f359d Mon Sep 17 00:00:00 2001 From: tecbr <17889776+tecbr@users.noreply.github.com> Date: Thu, 9 Nov 2023 21:22:56 -0300 Subject: [PATCH 2/2] Fix 0034_create_email_template_for_merged.py --- helpdesk/migrations/0034_create_email_template_for_merged.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/helpdesk/migrations/0034_create_email_template_for_merged.py b/helpdesk/migrations/0034_create_email_template_for_merged.py index 42abe198..018faf57 100644 --- a/helpdesk/migrations/0034_create_email_template_for_merged.py +++ b/helpdesk/migrations/0034_create_email_template_for_merged.py @@ -7,7 +7,7 @@ def forwards_func(apps, schema_editor): EmailTemplate = apps.get_model("helpdesk", "EmailTemplate") db_alias = schema_editor.connection.alias EmailTemplate.objects.using(db_alias).create( - id=EmailTemplate.objects.order_by('-id').first().id + 1, # because PG sequences are not reset + id=EmailTemplate.objects.using(db_alias).order_by('-id').first().id + 1 if EmailTemplate.objects.using(db_alias).first() else 1, # because PG sequences are not reset template_name='merged', subject='(Merged)', heading='Ticket merged', @@ -24,7 +24,7 @@ From now on, please answer on this ticket, or you can include the tag {{ ticket. locale='en' ) EmailTemplate.objects.using(db_alias).create( - id=EmailTemplate.objects.order_by('-id').first().id + 1, # because PG sequences are not reset + id=EmailTemplate.objects.using(db_alias).order_by('-id').first().id + 1, # because PG sequences are not reset template_name='merged', subject='(Fusionné)', heading='Ticket Fusionné',