allow adding groups to users in bulk
This commit is contained in:
@@ -14,4 +14,8 @@
|
||||
|
||||
# Future
|
||||
- Clean up user results / score pages
|
||||
- Remove external javascript dependencies
|
||||
- Remove external javascript dependencies
|
||||
|
||||
- Viva cases
|
||||
|
||||
- Update rapids for new style
|
||||
@@ -41,11 +41,17 @@
|
||||
hx-target="#htmx-info"
|
||||
>Delete supervisors</button>
|
||||
<button id="bulk-edit-grade-button"
|
||||
hx-post="{% url 'generic:users_bulk_edit_grade' %}"
|
||||
hx-post="{% url 'generic:users_bulk_edit' %}"
|
||||
hx-include="[name='selection']"
|
||||
hx-target="#htmx-options"
|
||||
|
||||
>Edit grade</button>
|
||||
<button id="bulk-add-group-button"
|
||||
hx-post="{% url 'generic:users_bulk_edit' %}"
|
||||
hx-include="[name='selection']"
|
||||
hx-target="#htmx-options"
|
||||
|
||||
>Add group</button>
|
||||
</details>
|
||||
|
||||
{% endblock %}
|
||||
|
||||
+3
-3
@@ -94,9 +94,9 @@ urlpatterns = [
|
||||
name="users_bulk_delete_supervisors",
|
||||
),
|
||||
path(
|
||||
"user/bulk_edit_grade/",
|
||||
views.users_bulk_edit_grade,
|
||||
name="users_bulk_edit_grade",
|
||||
"user/bulk_edit/",
|
||||
views.users_bulk_edit,
|
||||
name="users_bulk_edit",
|
||||
),
|
||||
path("user/group/", views.user_group_view, name="user_group_view"),
|
||||
path("user/group/all", views.user_group_view_all, name="user_group_view_all"),
|
||||
|
||||
+67
-48
@@ -2475,63 +2475,82 @@ def users_bulk_delete_supervisors(request):
|
||||
|
||||
|
||||
@user_is_cid_user_manager
|
||||
def users_bulk_edit_grade(request):
|
||||
if request.htmx.trigger == "bulk-edit-grade-button":
|
||||
user_grades = UserGrades.objects.all()
|
||||
def users_bulk_edit(request):
|
||||
print(request.htmx.trigger)
|
||||
print(request.htmx.trigger_name)
|
||||
match request.htmx.trigger:
|
||||
case r if r is None:
|
||||
return HttpResponse("None", content_type="text/html")
|
||||
|
||||
html = "".join(
|
||||
[
|
||||
f"""<button class='change-grade-button' name='add-grade--{grade.id}'
|
||||
hx-post="{reverse('generic:users_bulk_edit_grade')}"
|
||||
hx-include="[name='selection']"
|
||||
hx-confirm="This will modify grades of all selected users, are you sure you wish to continue?"
|
||||
hx-target="#htmx-info"
|
||||
>{grade.name}</button>"""
|
||||
for grade in user_grades
|
||||
]
|
||||
)
|
||||
# TODO: work out how to actually use hyperscript
|
||||
html = (
|
||||
html
|
||||
+ "<button _='on click for el in .change-grade-button remove el end then remove me'>Cancel</button>"
|
||||
)
|
||||
case "bulk-edit-grade-button":
|
||||
user_grades = UserGrades.objects.all()
|
||||
|
||||
return HttpResponse(html, content_type="text/html")
|
||||
elif request.htmx.trigger_name.startswith("add-grade"):
|
||||
selected_users = request.POST.getlist("selection")
|
||||
html = "".join(
|
||||
[
|
||||
f"""<button class='change-grade-button' id='add-grade--{grade.id}'
|
||||
hx-post="{reverse('generic:users_bulk_edit')}"
|
||||
hx-include="[name='selection']"
|
||||
hx-confirm="This will modify grades of all selected users, are you sure you wish to continue?"
|
||||
hx-target="#htmx-info"
|
||||
>{grade.name}</button>"""
|
||||
for grade in user_grades
|
||||
]
|
||||
)
|
||||
# TODO: work out how to actually use hyperscript
|
||||
html = (
|
||||
html
|
||||
+ "<button _='on click for el in .change-grade-button remove el end then remove me'>Cancel</button>"
|
||||
)
|
||||
|
||||
if not selected_users:
|
||||
return HttpResponse("No users selected", content_type="text/html")
|
||||
user_models = User.objects.filter(id__in=selected_users)
|
||||
return HttpResponse(html, content_type="text/html")
|
||||
case r if r.startswith("add-grade"):
|
||||
selected_users = request.POST.getlist("selection")
|
||||
|
||||
# u: User
|
||||
for u in user_models:
|
||||
u.userprofile.grade_id = request.htmx.trigger_name.split("--")[-1]
|
||||
u.save()
|
||||
return HttpResponse("Grades update", content_type="text/html")
|
||||
else:
|
||||
return HttpResponse("Error", content_type="text/html")
|
||||
if not selected_users:
|
||||
return HttpResponse("No users selected", content_type="text/html")
|
||||
user_models = User.objects.filter(id__in=selected_users)
|
||||
|
||||
# u: User
|
||||
for u in user_models:
|
||||
u.userprofile.grade_id = r.split("--")[-1]
|
||||
u.save()
|
||||
return HttpResponse("Grades update", content_type="text/html")
|
||||
case "bulk-add-group-button":
|
||||
user_groups = UserUserGroup.objects.all()
|
||||
|
||||
@user_is_cid_user_manager
|
||||
def users_bulk_delete_supervisors(request):
|
||||
if "selection" in request.POST:
|
||||
selected_users = request.POST.getlist("selection")
|
||||
user_models = User.objects.filter(id__in=selected_users)
|
||||
html = "".join(
|
||||
[
|
||||
f"""<button class='add-group-button' id='add-group--{group.id}'
|
||||
hx-post="{reverse('generic:users_bulk_edit')}"
|
||||
hx-include="[name='selection']"
|
||||
hx-confirm="This will add the group '{group.name}' to all selected users, are you sure you wish to continue?"
|
||||
hx-target="#htmx-info"
|
||||
>{group.name}</button>"""
|
||||
for group in user_groups
|
||||
]
|
||||
)
|
||||
# TODO: work out how to actually use hyperscript
|
||||
html = (
|
||||
html
|
||||
+ "<button _='on click for el in .add-group-button remove el end then remove me'>Cancel</button>"
|
||||
)
|
||||
|
||||
for u in user_models:
|
||||
u.userprofile.supervisor = None
|
||||
u.save()
|
||||
return HttpResponse(html, content_type="text/html")
|
||||
case r if r.startswith("add-group"):
|
||||
selected_users = request.POST.getlist("selection")
|
||||
|
||||
modified_users = ",".join([user.username for user in user_models])
|
||||
if not selected_users:
|
||||
return HttpResponse("No users selected", content_type="text/html")
|
||||
user_models = User.objects.filter(id__in=selected_users)
|
||||
|
||||
return HttpResponse(
|
||||
f"Supervisors removed from {modified_users}. Refresh page to see update",
|
||||
content_type="text/plain",
|
||||
)
|
||||
|
||||
else:
|
||||
return HttpResponse("No users selected", content_type="text/plain")
|
||||
# u: User
|
||||
for u in user_models:
|
||||
pass
|
||||
u.user_groups.add(r.split("--")[-1])
|
||||
u.save()
|
||||
return HttpResponse("Group added", content_type="text/html")
|
||||
case _:
|
||||
return HttpResponse("Error", content_type="text/html")
|
||||
|
||||
|
||||
@user_is_cid_user_manager
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import secrets
|
||||
from typing import Optional
|
||||
from django.conf import settings
|
||||
|
||||
from django_tables2 import SingleTableMixin
|
||||
@@ -542,6 +543,12 @@ class UserListTableView(CidManagerRequiredMixin, SingleTableMixin, FilterView):
|
||||
|
||||
filterset_class = UserUserFilter
|
||||
|
||||
#table_pagination = {"per_page": 5}
|
||||
|
||||
def get_paginate_by(self, table_data) -> int | None:
|
||||
return 2
|
||||
#return super().get_paginate_by(table_data)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
# user = self.request.user
|
||||
|
||||
Reference in New Issue
Block a user