Skip to content

Commit

Permalink
fix: .save being placed in the wrong model
Browse files Browse the repository at this point in the history
feat: auto-create groups
  • Loading branch information
JasonLovesDoggo committed Dec 2, 2023
1 parent 2765f0d commit ea491b8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
51 changes: 51 additions & 0 deletions core/management/commands/init.py
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")
12 changes: 6 additions & 6 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ea491b8

Please sign in to comment.