-
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.
updated actions to allow ppl in those groups to access
- Loading branch information
1 parent
f0f28c1
commit 556a739
Showing
1 changed file
with
34 additions
and
13 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 |
---|---|---|
@@ -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 | ||
) |