Skip to content

Commit

Permalink
fix/feat: updated perms system
Browse files Browse the repository at this point in the history
feat: updates credits sys

refactor: moved actions to actions.py

+ fmt
  • Loading branch information
JasonLovesDoggo committed Dec 7, 2023
1 parent f4e46cf commit f0f28c1
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 34 deletions.
32 changes: 8 additions & 24 deletions core/admin.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
from django.contrib import admin, messages
from django.contrib import messages
from django.contrib.auth.admin import UserAdmin as UserAdmin_
from django.contrib.auth.models import Group
from django.db.models import QuerySet
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _

from .forms import *


@admin.action(
permissions=["change"],
description=_("Set selected users as a Logic Puzzle Setter"),
)
def set_as_logic_setter(modeladmin, request, queryset: QuerySet[User]):
for user in queryset:
user.is_staff = True
# set their group to location setter
user.groups.add(Group.objects.get_or_create(name="Logic Logic Puzzle Setters"))


@admin.action(
permissions=["change"],
description=_("Remove selected users as a Logic Puzzle Setter"),
)
def remove_as_logic_setter(modeladmin, request, queryset: QuerySet[User]):
for user in queryset:
user.groups.remove(Group.objects.get(name="Logic Logic Puzzle Setters"))
from .utils.actions import *


class HintsInLine(admin.StackedInline):
Expand Down Expand Up @@ -123,7 +102,12 @@ class UserAdmin(UserAdmin_):
"last_name",
"email",
)
actions = [set_as_logic_setter, remove_as_logic_setter]
actions = [
set_as_logic_setter,
remove_as_logic_setter,
set_as_location_setter,
remove_as_location_setter,
]
admin_field = list(UserAdmin_.fieldsets)
admin_field[0][1]["fields"] = (
"username",
Expand Down
7 changes: 5 additions & 2 deletions core/management/commands/init.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
from django.conf import settings
from django.contrib.auth.models import Group, Permission
from django.core.management import BaseCommand

from core import models

GROUPS_PERMISSIONS = {
"Location Setter": {
settings.LOCATION_SETTERS_NAME: {
models.QrCode: ["add", "change", "delete"],
models.Hint: ["add", "change", "delete", "view"],
models.LogicPuzzleHint: ["view"],
models.Hunt: ["view"],
},
"Logic Puzzle Setters": {
settings.LOGIC_PUZZLE_SETTERS_NAME: {
models.QrCode: ["view"],
models.Hint: ["view"],
models.LogicPuzzleHint: ["add", "change", "delete", "view"],
models.Hunt: ["view"],
},
}

Expand Down
1 change: 1 addition & 0 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class Hint(models.Model):
def __str__(self):
return self.hint


class Team(models.Model):
# owner = models.ForeignKey(User, on_delete=models.PROTECT, related_name="teams_ownership") potentially add this later
id = models.AutoField(primary_key=True)
Expand Down
7 changes: 6 additions & 1 deletion core/templates/core/credits.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ <h2>{% translate 'hints' %}</h2>
<li>{{ hs.get_full_name }}</li>
{% endfor %}
</ul>

<h2>{% translate 'logic puzzle' %}</h2>
<ul class="nodot">
{% for ls in logicsetters %}
<li>{{ ls.get_full_name }}</li>
{% endfor %}
</ul>
<h2>{% translate 'support' %}</h2>
<ul class="nodot">
<li><a href="https://maclyonsden.com">project metropolis</a></li>
Expand Down
47 changes: 47 additions & 0 deletions core/utils/actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from django.conf import settings
from django.contrib import admin
from django.contrib.auth.models import Group
from django.db.models import QuerySet
from django.utils.translation import gettext_lazy as _

from core.models import User


@admin.action(
permissions=["change"],
description=_("Set selected users as a Logic Puzzle Setter"),
)
def set_as_logic_setter(modeladmin, request, queryset: QuerySet[User]):
for user in queryset:
user.groups.add(
Group.objects.get_or_create(name=settings.LOGIC_PUZZLE_SETTERS_NAME)
)


@admin.action(
permissions=["change"],
description=_("Remove selected users as a Logic Puzzle Setter"),
)
def remove_as_logic_setter(modeladmin, request, queryset: QuerySet[User]):
for user in queryset:
user.groups.remove(Group.objects.get(name=settings.LOGIC_PUZZLE_SETTERS_NAME))


@admin.action(
permissions=["change"],
description=_("Set selected users as a Qr Code Setter"),
)
def set_as_location_setter(modeladmin, request, queryset: QuerySet[User]):
for user in queryset:
user.groups.add(
Group.objects.get_or_create(name=settings.LOCATION_SETTERS_NAME)
)


@admin.action(
permissions=["change"],
description=_("Remove selected users as a Qr Code Setter"),
)
def remove_as_location_setter(modeladmin, request, queryset: QuerySet[User]):
for user in queryset:
user.groups.remove(Group.objects.get(name=settings.LOCATION_SETTERS_NAME))
11 changes: 9 additions & 2 deletions core/views/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ def credits(q):
try:
hintsetters = User.objects.filter(
groups__in=[
Group.objects.get(id=settings.HINTS_GROUP_PK),
Group.objects.get(name=settings.HINT_SETTERS_NAME),
]
).all()
logicsetters = User.objects.filter(
groups__in=[
Group.objects.get(name=settings.LOGIC_PUZZLE_SETTERS_NAME),
]
).all()
except Group.DoesNotExist:
hintsetters = []
return render(q, "core/credits.html", dict(hintsetters=hintsetters))
return render(
q, "core/credits.html", dict(hintsetters=hintsetters, logicsetters=logicsetters)
)
7 changes: 2 additions & 5 deletions wlmac-scavenger/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,8 @@
me_url=f"{base_url}/api/me/internal",
scope="me_meta internal",
)

HINTS_GROUP_PK: Final[
int
] = 1 # the pk of the hints group (defined in commands/init.py)

LOCATION_SETTERS_NAME: Final[str] = "Location Setter"
LOGIC_PUZZLE_SETTERS_NAME: Final[str] = "Logic Puzzle Setters"
try:
with open(os.path.join(os.path.dirname(__file__), "local_settings.py")) as f:
exec(f.read(), globals())
Expand Down

0 comments on commit f0f28c1

Please sign in to comment.