mirror of
https://gitea.mueller.network/extern/django-helpdesk.git
synced 2024-11-21 23:43:11 +01:00
Merge pull request #1133 from tecbr/main
Fix migrations for multiple databases
This commit is contained in:
commit
d6764cd4fb
@ -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()
|
||||
|
@ -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é',
|
||||
|
Loading…
Reference in New Issue
Block a user