Skip to content

Commit 606dfd8

Browse files
committed
Create permission groups
1 parent 1fa6faf commit 606dfd8

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

core/admin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class UserAdmin(BaseUserAdmin):
1616
'is_active',
1717
'is_staff',
1818
'is_superuser',
19+
'groups',
1920
)
2021
}
2122
),
@@ -33,6 +34,7 @@ class UserAdmin(BaseUserAdmin):
3334
'is_active',
3435
'is_staff',
3536
'is_superuser',
37+
'groups',
3638

3739
),
3840
}),
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from django.core.management.base import BaseCommand
2+
from django.contrib.auth.models import Group, Permission
3+
from django.contrib.contenttypes.models import ContentType
4+
from core.models import User
5+
from inventory.models import InventoryItem, Product, QuantitativeUnit
6+
from recipes.models import Recipe
7+
8+
class Command(BaseCommand):
9+
help = 'Create user groups and set permissions for Administrator, Moderator, and Contributor'
10+
11+
content_type = ContentType.objects.get_for_model(InventoryItem)
12+
inventory_item_permission = Permission.objects.filter(content_type=content_type)
13+
print([perm.codename for perm in inventory_item_permission])
14+
15+
def handle(self, *args, **kwargs):
16+
groups_permissions = {
17+
'Administrator': ['add_user', 'change_user', 'delete_user', 'view_user', 'add_inventoryitem', 'change_inventoryitem', 'delete_inventoryitem', 'view_inventoryitem', 'add_product', 'change_product', 'delete_product', 'view_product', 'add_recipe', 'change_recipe', 'delete_recipe', 'view_recipe', 'add_quantitativeunit', 'change_quantitativeunit', 'delete_quantitativeunit', 'view_quantitativeunit'],
18+
'Moderator': ['view_user', 'add_inventoryitem', 'change_inventoryitem', 'delete_inventoryitem', 'view_inventoryitem', 'add_product', 'change_product', 'delete_product', 'view_product', 'add_recipe', 'change_recipe', 'delete_recipe', 'view_recipe', 'add_quantitativeunit', 'change_quantitativeunit', 'delete_quantitativeunit', 'view_quantitativeunit'],
19+
'Contributor': ['add_inventoryitem', 'change_inventoryitem', 'delete_inventoryitem', 'view_inventoryitem', 'add_product', 'change_product', 'delete_product', 'view_product', 'add_recipe', 'change_recipe', 'delete_recipe', 'view_recipe', 'add_quantitativeunit', 'change_quantitativeunit', 'delete_quantitativeunit', 'view_quantitativeunit']
20+
}
21+
22+
model_content_types = {
23+
'user': ContentType.objects.get_for_model(User),
24+
'inventoryitem': ContentType.objects.get_for_model(InventoryItem),
25+
'product': ContentType.objects.get_for_model(Product),
26+
'quantitativeunit': ContentType.objects.get_for_model(QuantitativeUnit),
27+
'recipe': ContentType.objects.get_for_model(Recipe),
28+
}
29+
30+
for group_name, permissions_list in groups_permissions.items():
31+
# Create the group
32+
group, created = Group.objects.get_or_create(name=group_name)
33+
if created:
34+
self.stdout.write(f'Group "{group_name}" created')
35+
else:
36+
self.stdout.write(f'Group "{group_name}" already exists')
37+
38+
# Set permissions
39+
for perm_codename in permissions_list:
40+
# Determine model and content type
41+
model_name = perm_codename.split('_')[1]
42+
content_type = model_content_types.get(model_name)
43+
44+
if not content_type:
45+
self.stdout.write(f'No content type found for model "{model_name}"')
46+
continue
47+
48+
try:
49+
# Fetch the permission based on the codename and content type
50+
permission = Permission.objects.get(codename=perm_codename, content_type=content_type)
51+
group.permissions.add(permission)
52+
except Permission.DoesNotExist:
53+
self.stdout.write(f'Permission "{perm_codename}" not found for group "{group_name}"')
54+
55+
self.stdout.write(f'Permissions set for group "{group_name}"')
56+
57+
self.stdout.write(self.style.SUCCESS('Groups and permissions setup complete.'))

entrypoint.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ python manage.py makemigrations
2121
python manage.py migrate
2222
python manage.py migrate --fake contenttypes
2323
python manage.py spectacular
24+
python manage.py create_groups
2425
gunicorn --config gunicorn_config.py cms.wsgi:application
2526

2627
exec "$@"

0 commit comments

Comments
 (0)