allow adding users to group by email
This commit is contained in:
@@ -38,6 +38,24 @@
|
|||||||
They can be created (or added) <a href="{% url 'generic:manage_cids' %}">here</a>
|
They can be created (or added) <a href="{% url 'generic:manage_cids' %}">here</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% 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>
|
<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.
|
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" %}
|
{% if group_type == "cid" %}
|
||||||
@@ -62,20 +80,20 @@
|
|||||||
<form>
|
<form>
|
||||||
<input type="hidden" name="exam_id" value="{{exam.pk}}">
|
<input type="hidden" name="exam_id" value="{{exam.pk}}">
|
||||||
<input type="hidden" name="exam_type" value="{{exam.app_name}}">
|
<input type="hidden" name="exam_type" value="{{exam.app_name}}">
|
||||||
<button
|
<button
|
||||||
class="btn btn-sm px-2"
|
class="btn btn-sm px-2"
|
||||||
hx-post="{% url 'generic:user_group_add_candidates_to_exams' group.pk %}"
|
hx-post="{% url 'generic:user_group_add_candidates_to_exams' group.pk %}"
|
||||||
title="Add all group candidates to this exam"
|
title="Add all group candidates to this exam"
|
||||||
name="action"
|
name="action"
|
||||||
value="add"
|
value="add"
|
||||||
>Add group candidates</button>
|
>Add group candidates</button>
|
||||||
<button
|
<button
|
||||||
hx-post="{% url 'generic:user_group_add_candidates_to_exams' group.pk %}"
|
hx-post="{% url 'generic:user_group_add_candidates_to_exams' group.pk %}"
|
||||||
title="Remove all group candidates to this exam"
|
title="Remove all group candidates to this exam"
|
||||||
class="btn btn-sm px-2"
|
class="btn btn-sm px-2"
|
||||||
name="action"
|
name="action"
|
||||||
value="remove"
|
value="remove"
|
||||||
>Remove group candidates</button>
|
>Remove group candidates</button>
|
||||||
</form>
|
</form>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
|||||||
@@ -130,6 +130,11 @@ urlpatterns = [
|
|||||||
views.user_group_view_detail,
|
views.user_group_view_detail,
|
||||||
name="user_group_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(
|
path(
|
||||||
"user/group/<int:group_id>/add",
|
"user/group/<int:group_id>/add",
|
||||||
views.user_group_add_candidates_to_exams,
|
views.user_group_add_candidates_to_exams,
|
||||||
|
|||||||
@@ -2863,6 +2863,47 @@ def cid_group_view(request):
|
|||||||
{"groups": groups},
|
{"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
|
@user_is_cid_user_manager
|
||||||
def cid_group_view_detail(request, group_id):
|
def cid_group_view_detail(request, group_id):
|
||||||
|
|||||||
Reference in New Issue
Block a user