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.' ))
0 commit comments