Skip to content

Commit

Permalink
updated actions to allow ppl in those groups to access
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLovesDoggo committed Dec 7, 2023
1 parent f0f28c1 commit 556a739
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions core/utils/actions.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,68 @@
from django.conf import settings
from django.contrib import admin
from django.contrib import admin, messages
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


def set_group_membership(
modeladmin, request, queryset: QuerySet[User], group_name, add=True
):
try:
group = Group.objects.get(name=group_name)
except Group.DoesNotExist:
messages.error(request, f"Group '{group_name}' does not exist.")
return

for user in queryset:
if add:
user.is_staff = True
user.save()
user.groups.add(group)
else:
user.groups.remove(group)
if user.groups.count() == 0:
user.is_staff = False
user.save()


@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)
)
set_group_membership(
modeladmin, request, queryset, settings.LOGIC_PUZZLE_SETTERS_NAME, add=True
)


@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))
set_group_membership(
modeladmin, request, queryset, settings.LOGIC_PUZZLE_SETTERS_NAME, add=False
)


@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)
)
set_group_membership(
modeladmin, request, queryset, settings.LOCATION_SETTERS_NAME, add=True
)


@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))
set_group_membership(
modeladmin, request, queryset, settings.LOCATION_SETTERS_NAME, add=False
)

0 comments on commit 556a739

Please sign in to comment.