summaryrefslogtreecommitdiff
path: root/ishtar_common/migrations/0233_default_mandatory_keys_import_permissions.py
blob: a20682e299cc941de8b216167c00e1d5b49f2c70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 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)
        if perm not in group.permissions.all():
            group.permissions.add(perm)
        if administrator:
            if group not in administrator.groups.all():
                administrator.groups.add(group)

    print(COLOR_WARNING + "** Verify import permissions in profiles **" + COLOR_ENDC)


class Migration(migrations.Migration):

    dependencies = [
        ('ishtar_common', '0232_auto_20231115_1616'),
    ]

    operations = [
        migrations.RunPython(migrate),
    ]