allow adding users to group by email

This commit is contained in:
Ross
2024-12-16 10:28:51 +00:00
parent 8d62673f06
commit 6d20beebce
3 changed files with 79 additions and 15 deletions
@@ -38,6 +38,24 @@
They can be created (or added) <a href="{% url 'generic:manage_cids' %}">here</a>
{% endif %}
{% endif %}
<div class="border border-secondary">
<details>
<summary >Add users by email</summary>
<div >
<form method="post">
{% csrf_token %}
<textarea name="emails" placeholder="Email addresses (one per line)"></textarea>
<button type="submit"
hx-post="{% url 'generic:user_group_add_by_email_user' group.pk %}"
hx-target="#add-users-by-email-results"
>Add</button>
</form>
<div id="add-users-by-email-results"></div>
</div>
</details>
</div>
<h3>Exams</h3>
The group is currently associated with the following exams. This allows the users within the group to be easily added to the exam. It will not automatically give members of the group access to the exam.
{% if group_type == "cid" %}
@@ -52,7 +70,7 @@
<div>
{% for exam in value %}
<div class="d-flex">
<a href="{{exam.get_absolute_url}}" class="px-2">{{exam}}</a>
<a href="{{exam.get_absolute_url}}" class="px-2">{{exam}}</a>
{% if group_type == "cid" %}
<a href="{{exam.get_cid_edit_url}}" class="edit-link" px-2>Edit candidates</a>
{% comment %} <button>Add group candidates</button>
@@ -62,20 +80,20 @@
<form>
<input type="hidden" name="exam_id" value="{{exam.pk}}">
<input type="hidden" name="exam_type" value="{{exam.app_name}}">
<button
class="btn btn-sm px-2"
hx-post="{% url 'generic:user_group_add_candidates_to_exams' group.pk %}"
title="Add all group candidates to this exam"
name="action"
value="add"
>Add group candidates</button>
<button
hx-post="{% url 'generic:user_group_add_candidates_to_exams' group.pk %}"
title="Remove all group candidates to this exam"
class="btn btn-sm px-2"
name="action"
value="remove"
>Remove group candidates</button>
<button
class="btn btn-sm px-2"
hx-post="{% url 'generic:user_group_add_candidates_to_exams' group.pk %}"
title="Add all group candidates to this exam"
name="action"
value="add"
>Add group candidates</button>
<button
hx-post="{% url 'generic:user_group_add_candidates_to_exams' group.pk %}"
title="Remove all group candidates to this exam"
class="btn btn-sm px-2"
name="action"
value="remove"
>Remove group candidates</button>
</form>
{% endif %}
+5
View File
@@ -130,6 +130,11 @@ urlpatterns = [
views.user_group_view_detail,
name="user_group_detail",
),
path(
"user/group/<int:group_id>/add_by_email",
views.user_group_add_by_email_user,
name="user_group_add_by_email_user",
),
path(
"user/group/<int:group_id>/add",
views.user_group_add_candidates_to_exams,
+41
View File
@@ -2863,6 +2863,47 @@ def cid_group_view(request):
{"groups": groups},
)
@user_is_cid_user_manager
def user_group_add_by_email_user(request, group_id):
if request.htmx:
group = get_object_or_404(UserUserGroup, pk=group_id)
emails = request.POST.get("emails")
if not emails:
return HttpResponse("No emails provided", content_type="text/plain")
added = []
invalid_emails = []
for email in emails.split():
try:
validate_email(email)
try:
user = User.objects.get(email=email)
group.users.add(user)
added.append(user.username)
except User.DoesNotExist:
invalid_emails.append(email)
except ValidationError:
# Ignore invalid emails
pass
res = f"<div class='alert alert-primary'>Users added to {group.name} [{','.join(added)}]</div>"
if invalid_emails:
res += f"<br/><div class='alert alert-warning'>Invalid emails [{','.join(invalid_emails)}]</div>"
return HttpResponse(format_html(res), content_type="text/html")
raise Http404
@user_is_cid_user_manager
def cid_group_view_detail(request, group_id):