forked from w-a-r-m-inventory-system/Food-Pantry-Inventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_warm_groups.py
executable file
·167 lines (136 loc) · 5.08 KB
/
setup_warm_groups.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
"""
setup_warm_groups.py - Establish the groups and permissions for WARM users.
Overview and Group Definitions
------------------------------
This program sets up the group and their permissions for the WARM
database. WARM is expected to have three levels occess to use this system.
Volunteer
Most users will have this level. It allows the user to
checkin and out boxes of product into the system.
Staff
Staff users can create and manage volunteer and staff users, as well as
manage the application data such as product, category, location,
etc. They also have all the privileges of a volunteer.
Admin
Admin users are Django superusers. They can manage any table in the
database directly via the Django admin web interface. In addition to
having all the privileges of a staff user, the application provides the
ability to add and manage other admins.
Requirements
------------
This program will not work as expected until the migration
0029_add_model_permissions.py has been applied to the database.
In turn, this program must be run before the application will work properly.
"""
from typing import Dict, List
from sys import stderr
# ---------------------------------------------------------
if __name__ == '__main__':
# Boilerplate for stand-alone Django scripts
from django import setup
setup()
from django.contrib.auth.models import Group, Permission
GROUPS_AND_PERMISSIONS: Dict[str, Dict[str, Dict[str, List[str]]]] = {
'Volunteer': {
'fpiweb': {
'box': [
'add_box',
'check_in_box',
'check_out_box',
'move_box',
'view_box'
],
'pallet': [
'build_pallet',
'move_pallet',
'view_pallet',
],
},
},
'Staff': {
'fpiweb': {
'activity': [
'view_activity',
],
'box': [
'print_labels_box',
],
'constraints': [
'add_constraints',
'change_constraints',
'delete_constraints',
'view_constraints',
],
'locbin': [
'add_locbin',
'change_locbin',
'delete_locbin',
'view_locbin',
],
'locrow': [
'add_locrow',
'change_locrow',
'delete_locrow',
'view_locrow',
],
'loctier': [
'add_loctier',
'change_loctier',
'delete_loctier',
'view_loctier',
],
'profile': [
'view_system_maintenance',
],
},
},
}
def iterate_permissions(permissions):
"""
Generator to pick up the needed foreign keys for a given permisison.
:param permissions: attributes associated with this permission
:return:
"""
for app_label, models in permissions.items():
for model, model_permissions in models.items():
for permission in model_permissions:
yield app_label, model, permission
def setup_group_permissions(group, permissions):
"""
For a given group, create or replace the permisisons for it.
:param group: group to be created or replaced
:param permissions: list of permissions to be applied to this group
:return:
"""
print(f"Clearing {group.name} permissions.")
group.permissions.clear()
for app_label, model, codename in iterate_permissions(permissions):
try:
permission = Permission.objects.get(
content_type__app_label=app_label,
content_type__model=model,
codename=codename
)
except Permission.DoesNotExist:
print(f"Permission {model} {codename} not found", file=stderr)
continue
print(f"Granting {model} {codename} to {group.name}")
group.permissions.add(permission)
def setup_groups_and_permissions(groups_and_permissions):
"""
Create or replace groups and add permissions.
:param groups_and_permissions: table of groups and associated permissions
:return:
"""
for group_name, permissions in groups_and_permissions.items():
group, created = Group.objects.get_or_create(name=group_name)
if created:
print(f"Created {group_name} group.")
else:
print(f"Found {group_name} group.")
setup_group_permissions(group, permissions)
print()
# run application
setup_groups_and_permissions(GROUPS_AND_PERMISSIONS)
# EOF