-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: .save being placed in the wrong model
feat: auto-create groups
- Loading branch information
1 parent
2765f0d
commit ea491b8
Showing
2 changed files
with
57 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from django.core.management import BaseCommand | ||
from django.contrib.auth.models import Group, Permission | ||
|
||
from core import models | ||
|
||
GROUPS_PERMISSIONS = { | ||
'Problem Setters': { | ||
models.QrCode: ['add', 'change', 'delete'], | ||
models.Hint: ['add', 'change', 'delete', 'view'], | ||
models.LogicPuzzleHint: ['view'], | ||
}, | ||
'Logic Puzzle Setters': { | ||
models.QrCode: ['view'], | ||
models.Hint: ['view'], | ||
models.LogicPuzzleHint: ['add', 'change', 'delete', 'view'], | ||
}, | ||
} | ||
|
||
class Command(BaseCommand): | ||
def __init__(self, *args, **kwargs): | ||
super(Command, self).__init__(*args, **kwargs) | ||
|
||
help = "Create the groups necessary for the scavenger hunt and assign permissions to those groups" | ||
|
||
def handle(self, *args, **options): | ||
# Loop groups | ||
for group_name in GROUPS_PERMISSIONS: | ||
|
||
# Get or create group | ||
group, created = Group.objects.get_or_create(name=group_name) | ||
|
||
# Loop models in group | ||
for model_cls in GROUPS_PERMISSIONS[group_name]: | ||
|
||
# Loop permissions in group/model | ||
for perm_index, perm_name in \ | ||
enumerate(GROUPS_PERMISSIONS[group_name][model_cls]): | ||
|
||
# Generate permission name as Django would generate it | ||
codename = perm_name + "_" + model_cls._meta.model_name | ||
|
||
try: | ||
# Find permission object and add to group | ||
perm = Permission.objects.get(codename=codename) | ||
group.permissions.add(perm) | ||
self.stdout.write("Adding " | ||
+ codename | ||
+ " to group " | ||
+ group.__str__()) | ||
except Permission.DoesNotExist: | ||
self.stdout.write(codename + " not found") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters