summaryrefslogtreecommitdiff
path: root/ishtar_common/migrations
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2023-10-26 17:03:41 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2024-04-16 16:38:32 +0200
commitf4f482cd4074898f5344a3a078e27800bbd060fd (patch)
tree46a317f0f5de7b0177206ac5b965be794ff2b2af /ishtar_common/migrations
parente008dd87b2eafd88cec3d75d0b3b4c92ce891f23 (diff)
downloadIshtar-f4f482cd4074898f5344a3a078e27800bbd060fd.tar.bz2
Ishtar-f4f482cd4074898f5344a3a078e27800bbd060fd.zip
✨ refactoring import permissions
Diffstat (limited to 'ishtar_common/migrations')
-rw-r--r--ishtar_common/migrations/0231_default_mandatory_keys.py38
-rw-r--r--ishtar_common/migrations/0231_default_mandatory_keys_import_permissions.py116
2 files changed, 116 insertions, 38 deletions
diff --git a/ishtar_common/migrations/0231_default_mandatory_keys.py b/ishtar_common/migrations/0231_default_mandatory_keys.py
deleted file mode 100644
index cd245322a..000000000
--- a/ishtar_common/migrations/0231_default_mandatory_keys.py
+++ /dev/null
@@ -1,38 +0,0 @@
-# Generated by Django 2.2.24 on 2023-09-18 17:05
-
-from django.db import migrations
-
-EXCLUDE_LIST = [
- "-",
- "auto_external_id",
- "spatial_reference_system",
- "public_domain",
-]
-
-FULL_COPY_LIST = [
- "scientist__attached_to",
-]
-
-
-def migrate(apps, __):
- ImporterDefault = apps.get_model('ishtar_common', 'ImporterDefault')
- for default in ImporterDefault.objects.all():
- if default.target not in EXCLUDE_LIST:
- req = default.target
- if req not in FULL_COPY_LIST:
- req = req.split("__")[0]
- if req.endswith("_type") or req.endswith("_types"):
- continue
- default.required_fields = req
- default.save()
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('ishtar_common', '0230_auto_20231024_1045'),
- ]
-
- operations = [
- migrations.RunPython(migrate),
- ]
diff --git a/ishtar_common/migrations/0231_default_mandatory_keys_import_permissions.py b/ishtar_common/migrations/0231_default_mandatory_keys_import_permissions.py
new file mode 100644
index 000000000..120711f09
--- /dev/null
+++ b/ishtar_common/migrations/0231_default_mandatory_keys_import_permissions.py
@@ -0,0 +1,116 @@
+# Generated by Django 2.2.24 on 2023-09-18 17:05
+
+from django.db import migrations
+
+COLOR_WARNING = "\033[93m"
+COLOR_ENDC = "\033[0m"
+
+EXCLUDE_LIST = [
+ "-",
+ "auto_external_id",
+ "spatial_reference_system",
+ "public_domain",
+]
+
+FULL_COPY_LIST = [
+ "scientist__attached_to",
+]
+
+GROUPS = [
+ [
+ "Imports : lecture",
+ "view_import",
+ "ishtar_common",
+ "import"
+ ],
+ [
+ "Imports rattachés : lecture",
+ "view_own_import",
+ "ishtar_common",
+ "import"
+ ],
+ [
+ "Imports : modification",
+ "change_import",
+ "ishtar_common",
+ "import"
+ ],
+ [
+ "Imports rattachés : modification",
+ "change_own_import",
+ "ishtar_common",
+ "import"
+ ],
+ [
+ "Imports : suppression",
+ "delete_import",
+ "ishtar_common",
+ "import"
+ ],
+ [
+ "Imports rattachés : suppression",
+ "delete_own_import",
+ "ishtar_common",
+ "import"
+ ],
+ [
+ "Imports : ajout",
+ "add_import",
+ "ishtar_common",
+ "import"
+ ],
+ [
+ "Imports rattachés : ajout",
+ "add_own_import",
+ "ishtar_common",
+ "import"
+ ],
+]
+
+
+def migrate(apps, __):
+ ImporterDefault = apps.get_model('ishtar_common', 'ImporterDefault')
+ for default in ImporterDefault.objects.all():
+ if default.target not in EXCLUDE_LIST:
+ req = default.target
+ if req not in FULL_COPY_LIST:
+ req = req.split("__")[0]
+ if req.endswith("_type") or req.endswith("_types"):
+ continue
+ default.required_fields = req
+ default.save()
+
+ print("")
+ ProfileType = apps.get_model('ishtar_common', 'ProfileType')
+ q = ProfileType.objects.filter(txt_idx="administrator")
+ administrator = None
+ if not q.count():
+ print(COLOR_WARNING + "** No administrator profile found. **" + COLOR_ENDC)
+ else:
+ administrator = q.all()[0]
+
+ Permission = apps.get_model("auth", "Permission")
+ Group = apps.get_model("auth", "Group")
+ ContentType = apps.get_model("contenttypes", "ContentType")
+ for name, codename, app, model in GROUPS:
+ ct, __ = ContentType.objects.get_or_create(app_label=app, model=model)
+ perm, __ = Permission.objects.get_or_create(
+ codename=codename, defaults={"name": name, "content_type": ct}
+ )
+ group, __ = Group.objects.get_or_create(name=name)
+ group.permissions.add(perm)
+ if administrator:
+ administrator.groups.add(group)
+
+ print(COLOR_WARNING + "** Verify import permissions in profiles **" + COLOR_ENDC)
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('ishtar_common', '0230_auto_20231024_1045'),
+ ]
+
+ operations = [
+ migrations.RunPython(migrate),
+ ]