|
| 1 | +""" |
| 2 | +Add the ``regroup_sessions_deployment`` custom permission to ``Project`` and |
| 3 | +grant it to existing ``ProjectManager`` role groups. |
| 4 | +
|
| 5 | +The new ``POST /api/v2/deployments/<pk>/regroup-sessions/`` action runs through |
| 6 | +``BaseModel.check_custom_permission``, which builds the codename as |
| 7 | +``{action}_{model_name}`` — for the ``regroup_sessions`` action on a |
| 8 | +``Deployment`` viewset that resolves project permission via the parent project, |
| 9 | +the perm needed is ``regroup_sessions_deployment``. Mirrors how |
| 10 | +``sync_deployment`` is granted in ``ami.users.roles.ProjectManager``. |
| 11 | +""" |
| 12 | + |
| 13 | +from django.db import migrations |
| 14 | +from django.db.models import Q |
| 15 | + |
| 16 | + |
| 17 | +def grant_regroup_sessions_to_project_managers(apps, schema_editor): |
| 18 | + Group = apps.get_model("auth", "Group") |
| 19 | + Permission = apps.get_model("auth", "Permission") |
| 20 | + ContentType = apps.get_model("contenttypes", "ContentType") |
| 21 | + |
| 22 | + try: |
| 23 | + project_ct = ContentType.objects.get(app_label="main", model="project") |
| 24 | + except ContentType.DoesNotExist: |
| 25 | + return |
| 26 | + |
| 27 | + perm, _ = Permission.objects.get_or_create( |
| 28 | + codename="regroup_sessions_deployment", |
| 29 | + content_type=project_ct, |
| 30 | + defaults={"name": "Can regroup deployment captures into sessions"}, |
| 31 | + ) |
| 32 | + |
| 33 | + role_groups = Group.objects.filter(Q(name__endswith="_ProjectManager")) |
| 34 | + for group in role_groups: |
| 35 | + group.permissions.add(perm) |
| 36 | + |
| 37 | + |
| 38 | +def revoke_regroup_sessions_from_project_managers(apps, schema_editor): |
| 39 | + Group = apps.get_model("auth", "Group") |
| 40 | + Permission = apps.get_model("auth", "Permission") |
| 41 | + ContentType = apps.get_model("contenttypes", "ContentType") |
| 42 | + GroupObjectPermission = apps.get_model("guardian", "GroupObjectPermission") |
| 43 | + |
| 44 | + try: |
| 45 | + project_ct = ContentType.objects.get(app_label="main", model="project") |
| 46 | + except ContentType.DoesNotExist: |
| 47 | + return |
| 48 | + try: |
| 49 | + perm = Permission.objects.get(codename="regroup_sessions_deployment", content_type=project_ct) |
| 50 | + except Permission.DoesNotExist: |
| 51 | + return |
| 52 | + |
| 53 | + role_groups = Group.objects.filter(Q(name__endswith="_ProjectManager")) |
| 54 | + for group in role_groups: |
| 55 | + group.permissions.remove(perm) |
| 56 | + |
| 57 | + GroupObjectPermission.objects.filter( |
| 58 | + permission=perm, |
| 59 | + content_type=project_ct, |
| 60 | + group__in=role_groups, |
| 61 | + ).delete() |
| 62 | + |
| 63 | + |
| 64 | +class Migration(migrations.Migration): |
| 65 | + dependencies = [ |
| 66 | + ("main", "0084_revoke_delete_job_from_roles"), |
| 67 | + ("guardian", "0002_generic_permissions_index"), |
| 68 | + ] |
| 69 | + |
| 70 | + operations = [ |
| 71 | + migrations.AlterModelOptions( |
| 72 | + name="project", |
| 73 | + options={ |
| 74 | + "ordering": ["-priority", "created_at"], |
| 75 | + "permissions": [ |
| 76 | + ("create_identification", "Can create identifications"), |
| 77 | + ("update_identification", "Can update identifications"), |
| 78 | + ("delete_identification", "Can delete identifications"), |
| 79 | + ("create_job", "Can create a job"), |
| 80 | + ("update_job", "Can update a job"), |
| 81 | + ("run_ml_job", "Can run/retry/cancel ML jobs"), |
| 82 | + ("run_populate_captures_collection_job", "Can run/retry/cancel Populate Collection jobs"), |
| 83 | + ("run_data_storage_sync_job", "Can run/retry/cancel Data Storage Sync jobs"), |
| 84 | + ("run_data_export_job", "Can run/retry/cancel Data Export jobs"), |
| 85 | + ("run_single_image_ml_job", "Can process a single capture"), |
| 86 | + ("run_post_processing_job", "Can run/retry/cancel Post-Processing jobs"), |
| 87 | + ("delete_job", "Can delete a job"), |
| 88 | + ("create_deployment", "Can create a deployment"), |
| 89 | + ("delete_deployment", "Can delete a deployment"), |
| 90 | + ("update_deployment", "Can update a deployment"), |
| 91 | + ("sync_deployment", "Can sync images to a deployment"), |
| 92 | + ("regroup_sessions_deployment", "Can regroup deployment captures into sessions"), |
| 93 | + ("create_sourceimagecollection", "Can create a collection"), |
| 94 | + ("update_sourceimagecollection", "Can update a collection"), |
| 95 | + ("delete_sourceimagecollection", "Can delete a collection"), |
| 96 | + ("populate_sourceimagecollection", "Can populate a collection"), |
| 97 | + ("create_sourceimage", "Can create a source image"), |
| 98 | + ("update_sourceimage", "Can update a source image"), |
| 99 | + ("delete_sourceimage", "Can delete a source image"), |
| 100 | + ("star_sourceimage", "Can star a source image"), |
| 101 | + ("create_sourceimageupload", "Can create a source image upload"), |
| 102 | + ("update_sourceimageupload", "Can update a source image upload"), |
| 103 | + ("delete_sourceimageupload", "Can delete a source image upload"), |
| 104 | + ("create_s3storagesource", "Can create storage"), |
| 105 | + ("delete_s3storagesource", "Can delete storage"), |
| 106 | + ("update_s3storagesource", "Can update storage"), |
| 107 | + ("test_s3storagesource", "Can test storage connection"), |
| 108 | + ("create_site", "Can create a site"), |
| 109 | + ("delete_site", "Can delete a site"), |
| 110 | + ("update_site", "Can update a site"), |
| 111 | + ("create_device", "Can create a device"), |
| 112 | + ("delete_device", "Can delete a device"), |
| 113 | + ("update_device", "Can update a device"), |
| 114 | + ("view_userprojectmembership", "Can view project members"), |
| 115 | + ("create_userprojectmembership", "Can add a user to the project"), |
| 116 | + ("update_userprojectmembership", "Can update a user's project membership and role in the project"), |
| 117 | + ("delete_userprojectmembership", "Can remove a user from the project"), |
| 118 | + ("create_dataexport", "Can create a data export"), |
| 119 | + ("update_dataexport", "Can update a data export"), |
| 120 | + ("delete_dataexport", "Can delete a data export"), |
| 121 | + ("create_projectpipelineconfig", "Can register pipelines for the project"), |
| 122 | + ("update_projectpipelineconfig", "Can update pipeline configurations"), |
| 123 | + ("delete_projectpipelineconfig", "Can remove pipelines from the project"), |
| 124 | + ("create_taxalist", "Can create a taxa list"), |
| 125 | + ("update_taxalist", "Can update a taxa list"), |
| 126 | + ("delete_taxalist", "Can delete a taxa list"), |
| 127 | + ("view_private_data", "Can view private data"), |
| 128 | + ], |
| 129 | + }, |
| 130 | + ), |
| 131 | + migrations.RunPython( |
| 132 | + grant_regroup_sessions_to_project_managers, |
| 133 | + revoke_regroup_sessions_from_project_managers, |
| 134 | + ), |
| 135 | + ] |
0 commit comments