mirror of https://github.com/sipwise/repoapi.git
Change-Id: Ifbffb02c5bc833cc4295745c7729e34fa8e0486apull/7/head
parent
10ce513d20
commit
1ad5cce788
@ -0,0 +1,86 @@
|
||||
# Generated by Django 3.2.13 on 2022-06-14 16:12
|
||||
from django.contrib.auth.management import create_permissions
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def add_permissions(apps, schema_editor):
|
||||
"""ContentType table is populated after all the migrations applied"""
|
||||
for app_config in apps.get_app_configs():
|
||||
app_config.models_module = True
|
||||
create_permissions(app_config, verbosity=0)
|
||||
app_config.models_module = None
|
||||
|
||||
|
||||
def reverse_func(apps, schema_editor):
|
||||
add_permissions(apps, schema_editor)
|
||||
Group = apps.get_model("auth", "Group")
|
||||
Permission = apps.get_model("auth", "Permission")
|
||||
ContentType = apps.get_model("contenttypes", "ContentType")
|
||||
db_alias = schema_editor.connection.alias
|
||||
|
||||
dev_grp = Group.objects.using(db_alias).get(name="dev")
|
||||
devops_grp = Group.objects.using(db_alias).get(name="devops")
|
||||
|
||||
BuildRelease = apps.get_model("build", "BuildRelease")
|
||||
ct = ContentType.objects.get_for_model(BuildRelease)
|
||||
|
||||
# these are the wrong ones!!
|
||||
devops_grp.permissions.set(
|
||||
[
|
||||
Permission.objects.using(db_alias).get(
|
||||
content_type=ct, codename="can_trigger_hotfix"
|
||||
),
|
||||
]
|
||||
)
|
||||
dev_grp.permissions.set(
|
||||
[
|
||||
Permission.objects.using(db_alias).get(
|
||||
content_type=ct, codename="can_trigger"
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def forwards_func(apps, schema_editor):
|
||||
add_permissions(apps, schema_editor)
|
||||
Group = apps.get_model("auth", "Group")
|
||||
Permission = apps.get_model("auth", "Permission")
|
||||
ContentType = apps.get_model("contenttypes", "ContentType")
|
||||
db_alias = schema_editor.connection.alias
|
||||
|
||||
dev_grp = Group.objects.using(db_alias).get(name="dev")
|
||||
devops_grp = Group.objects.using(db_alias).get(name="devops")
|
||||
|
||||
BuildRelease = apps.get_model("build", "BuildRelease")
|
||||
ct = ContentType.objects.get_for_model(BuildRelease)
|
||||
|
||||
perm_codenames = ["can_trigger", "can_trigger_hotfix"]
|
||||
for perm in ("add", "change", "delete", "view"):
|
||||
perm_codenames.append(f"{perm}_buildrelease")
|
||||
|
||||
perms = []
|
||||
for codename in perm_codenames:
|
||||
perm = Permission.objects.using(db_alias).get(
|
||||
content_type=ct, codename=codename
|
||||
)
|
||||
perms.append(perm)
|
||||
devops_grp.permissions.set(perms)
|
||||
|
||||
dev_grp.permissions.set(
|
||||
[
|
||||
Permission.objects.using(db_alias).get(
|
||||
content_type=ct, codename="can_trigger_hotfix"
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("repoapi", "0011_ldap_groups"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(forwards_func, reverse_func),
|
||||
]
|
Loading…
Reference in new issue