a number of updates

This commit is contained in:
Ross
2023-09-08 18:10:42 +01:00
parent 98a874f805
commit 297f8619b4
5 changed files with 198 additions and 105 deletions
+113 -66
View File
@@ -208,7 +208,6 @@ def examination_merge(request, pk):
if examination.merge_into(examination_to_merge_into):
success = True
else:
examination_to_merge_into = False
return render(
@@ -218,7 +217,7 @@ def examination_merge(request, pk):
"form": form,
"examination": examination,
"examination_to_merge_into": examination_to_merge_into,
"success": success
"success": success,
},
)
@@ -2474,83 +2473,131 @@ def users_bulk_delete_supervisors(request):
return HttpResponse("No users selected", content_type="text/plain")
class NoUsersSelected(Exception):
pass
def get_user_selection_from_request(request):
selected_users = request.POST.getlist("selection")
if not selected_users:
raise NoUsersSelected
user_models = User.objects.filter(id__in=selected_users)
return user_models
@user_is_cid_user_manager
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")
case "bulk-edit-grade-button":
user_grades = UserGrades.objects.all()
try:
match request.htmx.trigger:
case r if r is None:
html = "None"
case "bulk-deactivate-user-button":
user_models = get_user_selection_from_request(request)
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>"
)
# u: User
for u in user_models:
u.is_active = False
u.save()
return HttpResponse(html, content_type="text/html")
case r if r.startswith("add-grade"):
selected_users = request.POST.getlist("selection")
html = "Users deactivated"
if not selected_users:
return HttpResponse("No users selected", content_type="text/html")
user_models = User.objects.filter(id__in=selected_users)
case "bulk-edit-grade-button":
user_grades = UserGrades.objects.all()
# 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()
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"
_='on click put "" into #htmx-options'
>{grade.name}</button>"""
for grade in user_grades
]
)
# TODO: work out how to actually use hyperscript
html = (
html
+ "<button class='cancel-button' _='on click for el in .change-grade-button remove el end then remove me'>Cancel</button>"
)
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>"
)
case r if r.startswith("add-grade"):
user_models = get_user_selection_from_request(request)
return HttpResponse(html, content_type="text/html")
case r if r.startswith("add-group"):
selected_users = request.POST.getlist("selection")
# u: User
for u in user_models:
u.userprofile.grade_id = r.split("--")[-1]
u.save()
html = "Grades updated"
case "bulk-add-group-button":
user_groups = UserUserGroup.objects.all()
if not selected_users:
return HttpResponse("No users selected", content_type="text/html")
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"
_='on click put "" into #htmx-options'
>{group.name}</button>"""
for group in user_groups
]
)
# TODO: work out how to actually use hyperscript
html = (
html
+ "<button class='cancel-button'_='on click for el in .add-group-button remove el end then remove me'>Cancel</button>"
)
# 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")
case "bulk-remove-group-button":
user_groups = UserUserGroup.objects.all()
html = "".join(
[
f"""<button class='remove-group-button' id='remove-group--{group.id}'
hx-post="{reverse('generic:users_bulk_edit')}"
hx-include="[name='selection']"
hx-confirm="This will remove the group '{group.name}' from all selected users, are you sure you wish to continue?"
hx-target="#htmx-info"
_='on click put "" into #htmx-options'
>{group.name}</button>"""
for group in user_groups
]
)
# TODO: work out how to actually use hyperscript
html = (
html
+ "<button class='cancel-button'_='on click for el in .add-group-button remove el end then remove me'>Cancel</button>"
)
case r if r.startswith("add-group"):
user_models = get_user_selection_from_request(request)
# u: User
for u in user_models:
pass
u.user_groups.add(r.split("--")[-1])
u.save()
html = "Group added"
case r if r.startswith("remove-group"):
user_models = get_user_selection_from_request(request)
# u: User
for u in user_models:
group_id = r.split("--")[-1]
u.user_groups.remove(group_id)
html = "Group removed"
case _:
html = "None"
return HttpResponse(html, content_type="text/html")
except NoUsersSelected:
return HttpResponse("No users selected", content_type="text/html")
@user_is_cid_user_manager