diff --git a/course/enrollment.py b/course/enrollment.py index c0c230247..7dc910e7a 100644 --- a/course/enrollment.py +++ b/course/enrollment.py @@ -1096,4 +1096,100 @@ def edit_participation( # }}} + +# {{{ edit_participation_tag + +class ParticipationTagForm(StyledModelForm): + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self.helper.add_input(Submit("submit", _("Save"))) + + class Meta: + model = ParticipationTag + fields = ("name", "shown_to_participant") + + +@course_view +def edit_participation_tag( + pctx: CoursePageContext, tag_id: int) -> http.HttpResponse: + if not pctx.has_permission(PPerm.edit_participation): + raise PermissionDenied() + + request = pctx.request + + num_tag_id = int(tag_id) + + if num_tag_id == -1: + tag = ParticipationTag(course=pctx.course) + add_new = True + else: + tag = get_object_or_404(ParticipationTag, id=num_tag_id) + add_new = False + + if tag.course.id != pctx.course.id: + raise SuspiciousOperation( + "may not edit participation tag in different course") + + if request.method == "POST": + form = ParticipationTagForm(request.POST, instance=tag) + if form.is_valid(): + tag = form.save(commit=False) + tag.course = pctx.course + tag.save() + messages.add_message(request, messages.SUCCESS, + _("Changes saved.")) + return redirect( + "relate-list_participation_tags", + pctx.course.identifier) + else: + form = ParticipationTagForm(instance=tag) + + return render_course_page(pctx, "course/generic-course-form.html", { + "form_description": ( + _("Add Participation Tag") if add_new + else _("Edit Participation Tag")), + "form": form, + }) + + +@course_view +def delete_participation_tag( + pctx: CoursePageContext, tag_id: int) -> http.HttpResponse: + if not pctx.has_permission(PPerm.edit_participation): + raise PermissionDenied() + + request = pctx.request + + tag = get_object_or_404( + ParticipationTag, id=int(tag_id), course=pctx.course) + + if request.method == "POST": + tag.delete() + messages.add_message(request, messages.SUCCESS, + _("Participation tag deleted.")) + return redirect( + "relate-list_participation_tags", + pctx.course.identifier) + + return render_course_page( + pctx, "course/confirm-delete-participation-tag.html", { + "participation_tag": tag, + }) + + +@course_view +def list_participation_tags(pctx: CoursePageContext) -> http.HttpResponse: + if not pctx.has_permission(PPerm.edit_participation): + raise PermissionDenied() + + tags = ParticipationTag.objects.filter(course=pctx.course).order_by("name") + + return render_course_page( + pctx, "course/participation-tags.html", { + "participation_tags": tags, + }) + +# }}} + # vim: foldmethod=marker diff --git a/course/templates/course/confirm-delete-participation-tag.html b/course/templates/course/confirm-delete-participation-tag.html new file mode 100644 index 000000000..7043cdd7c --- /dev/null +++ b/course/templates/course/confirm-delete-participation-tag.html @@ -0,0 +1,18 @@ +{% extends "course/course-base.html" %} +{% load i18n %} + +{% block title %} + {% trans "Delete Participation Tag" %} - {{ relate_site_name }} +{% endblock %} + +{% block content %} +
{% blocktrans with name=participation_tag.name %}Are you sure you want to delete the participation tag "{{ name }}"?{% endblocktrans %}
+ + +{% endblock %} diff --git a/course/templates/course/course-base.html b/course/templates/course/course-base.html index 116f182d9..70371eea5 100644 --- a/course/templates/course/course-base.html +++ b/course/templates/course/course-base.html @@ -144,7 +144,7 @@ {% endif %} - {% if pperm.query_participation or pperm.manage_instant_flow_requests or pperm.preapprove_participation %} + {% if pperm.query_participation or pperm.manage_instant_flow_requests or pperm.preapprove_participation or pperm.edit_participation %} {% if not pperm.view_participant_masked_profile %}| {% trans "Name" %} | +{% trans "Shown to participant" %} | +{% trans "Actions" %} | +
|---|---|---|
| {{ tag.name }} | +{{ tag.shown_to_participant|yesno }} | ++ {% trans "Edit" %} + {% trans "Delete" %} + | +
{% trans "No participation tags defined." %}
+ {% endif %} +{% endblock %} diff --git a/relate/urls.py b/relate/urls.py index 390285e28..4436ec364 100644 --- a/relate/urls.py +++ b/relate/urls.py @@ -302,6 +302,26 @@ "/$", course.enrollment.edit_participation, name="relate-edit_participation"), + re_path(r"^course" + "/" + COURSE_ID_REGEX + + "/participation-tags" + "/$", + course.enrollment.list_participation_tags, + name="relate-list_participation_tags"), + re_path(r"^course" + "/" + COURSE_ID_REGEX + + "/edit-participation-tag" + "/(?P