|
| 1 | +# Generated by Django 3.1.14 on 2022-02-24 15:14 |
| 2 | + |
| 3 | +from django.db import migrations |
| 4 | + |
| 5 | + |
| 6 | +def apply_migration(apps, schema_editor): |
| 7 | + db_alias = schema_editor.connection.alias |
| 8 | + |
| 9 | + Group = apps.get_model("auth", "Group") |
| 10 | + Group.objects.using(db_alias).bulk_create( |
| 11 | + [ |
| 12 | + Group(name="core/participation"), |
| 13 | + Group(name="core/areamanagement"), |
| 14 | + Group(name="default"), |
| 15 | + ] |
| 16 | + ) |
| 17 | + |
| 18 | + permissions = ['view_workspace','view_annotation','view_category', 'view_usergroup'] |
| 19 | + |
| 20 | + default = Group.objects.using(db_alias).get(name="default") |
| 21 | + core_p_group = Group.objects.using(db_alias).get(name="core/participation") |
| 22 | + core_a_group = Group.objects.using(db_alias).get(name="core/areamanagement") |
| 23 | + |
| 24 | + default.permissions.set(permissions) |
| 25 | + core_p_group.permissions.set(permissions) |
| 26 | + core_a_group.permissions.set(permissions) |
| 27 | + |
| 28 | + # Add all users to new groups |
| 29 | + User = apps.get_model("gsuser", "User") |
| 30 | + users = User.objects.using(db_alias) |
| 31 | + default.user_set.add(*users) |
| 32 | + core_p_group.user_set.add(*users) |
| 33 | + core_a_group.user_set.add(*users) |
| 34 | + |
| 35 | + # todo: Add non-existent standard categories and usergroups then assign to core groups |
| 36 | + |
| 37 | + # todo: Add default group to objects without group |
| 38 | + |
| 39 | + |
| 40 | +def revert_migration(apps, schema_editor): |
| 41 | + Group = apps.get_model("auth", "Group") |
| 42 | + Group.objects.filter(name__in=["default", "core/participation", "core/areamanagement"]).delete() |
| 43 | + |
| 44 | +class Migration(migrations.Migration): |
| 45 | + |
| 46 | + dependencies = [ |
| 47 | + ('gsmap', '0042_auto_20220223_2331'), |
| 48 | + ] |
| 49 | + |
| 50 | + operations = [ |
| 51 | + migrations.RunPython(apply_migration, revert_migration) |
| 52 | + ] |
0 commit comments