diff --git a/core/management/commands/init.py b/core/management/commands/init.py new file mode 100644 index 0000000..5f7ccd7 --- /dev/null +++ b/core/management/commands/init.py @@ -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") diff --git a/core/models.py b/core/models.py index 1539b51..a09c74c 100644 --- a/core/models.py +++ b/core/models.py @@ -44,7 +44,7 @@ class QrCode(models.Model): help_text="Location of the QR code. Be specific—it's internal", ) notes = models.TextField(help_text="Internal notes", blank=True) - key = models.CharField(max_length=64, unique=True, default=generate_hint_key) + key = models.CharField(max_length=64, unique=True, default=generate_hint_key, help_text="Key to access the hint, used in the QR code ") image_url = models.URLField( help_text="A URL to an image of where the QR code is located (try imgur)", blank=True, @@ -93,11 +93,6 @@ def hint(self, team: "Team"): r.random() return Hint.objects.get(id=r.choice(pks)["pk"]) - def save(self, *args, **kwargs): - if self._state.adding: # only generate key on creation not on update - Invite.objects.create(team=self, code=generate_invite_code()).save() - return super().save(*args, **kwargs) - class Hint(models.Model): qr_code = models.ForeignKey(QrCode, related_name="hints", on_delete=models.CASCADE) @@ -157,6 +152,11 @@ def qr_len(self): def __str__(self): return str(self.name) + def save(self, *args, **kwargs): + if self._state.adding: # only generate key on creation not on update + Invite.objects.create(team=self, code=generate_invite_code()).save() + return super().save(*args, **kwargs) + class Invite(models.Model): invites = models.IntegerField(default=0)