Esempio n. 1
0
def course_edit(request, course_id):
    user = request.user
    course = get_object_or_404(Course, id=course_id)

    # check rights
    if not (course.is_user_editor_or_delegate(user) and course.state == 'prepared'):
        raise PermissionDenied

    InlineContributionFormset = inlineformset_factory(Course, Contribution, formset=ContributionFormSet, form=EditorContributionForm, extra=1)

    course_form = CourseForm(request.POST or None, instance=course)
    formset = InlineContributionFormset(request.POST or None, instance=course, form_kwargs={'course': course})

    operation = request.POST.get('operation')

    if course_form.is_valid() and formset.is_valid():
        if operation not in ('save', 'approve'):
            raise SuspiciousOperation("Invalid POST operation")

        course_form.save(user=user)
        formset.save()

        if operation == 'approve':
            # approve course
            course.editor_approve()
            course.save()
            messages.success(request, _("Successfully updated and approved course."))
        else:
            messages.success(request, _("Successfully updated course."))

        return redirect('contributor:index')
    else:
        sort_formset(request, formset)
        template_data = dict(form=course_form, formset=formset, course=course, edit=True, responsible=course.responsible_contributor.username)
        return render(request, "contributor_course_form.html", template_data)
Esempio n. 2
0
def helper_course_edit(request, semester, course):
    InlineContributionFormset = inlineformset_factory(Course, Contribution, formset=ContributionFormSet, form=ContributionForm, extra=1)

    form = CourseForm(request.POST or None, instance=course)
    formset = InlineContributionFormset(request.POST or None, instance=course, form_kwargs={'course': course})
    editable = course.can_staff_edit

    operation = request.POST.get('operation')

    if form.is_valid() and formset.is_valid():
        if operation not in ('save', 'approve'):
            raise SuspiciousOperation("Invalid POST operation")

        if not course.can_staff_edit or course.is_archived:
            raise SuspiciousOperation("Modifying this course is not allowed.")

        if course.state in ['evaluated', 'reviewed'] and course.is_in_evaluation_period():
            course.reopen_evaluation()
        form.save(user=request.user)
        formset.save()

        if operation == 'approve':
            # approve course
            course.staff_approve()
            course.save()
            messages.success(request, _("Successfully updated and approved course."))
        else:
            messages.success(request, _("Successfully updated course."))

        return custom_redirect('staff:semester_view', semester.id)
    else:
        sort_formset(request, formset)
        template_data = dict(semester=semester, form=form, formset=formset, staff=True, state=course.state, editable=editable)
        return render(request, "staff_course_form.html", template_data)
Esempio n. 3
0
def evaluation_edit(request, evaluation_id):
    evaluation = get_object_or_404(Evaluation, id=evaluation_id)

    # check rights
    if not (evaluation.is_user_editor_or_delegate(request.user) and evaluation.state == Evaluation.State.PREPARED):
        raise PermissionDenied

    post_operation = request.POST.get("operation") if request.POST else None
    preview = post_operation == "preview"

    InlineContributionFormset = inlineformset_factory(
        Evaluation, Contribution, formset=ContributionFormset, form=EditorContributionForm, extra=1
    )
    evaluation_form = EvaluationForm(request.POST or None, instance=evaluation)
    formset = InlineContributionFormset(
        request.POST or None, instance=evaluation, form_kwargs={"evaluation": evaluation}
    )

    forms_are_valid = evaluation_form.is_valid() and formset.is_valid()
    if forms_are_valid and not preview:
        if post_operation not in ("save", "approve"):
            raise SuspiciousOperation("Invalid POST operation")

        form_has_changed = evaluation_form.has_changed() or formset.has_changed()

        evaluation_form.save()
        formset.save()

        if post_operation == "approve":
            evaluation.editor_approve()
            evaluation.save()
            if form_has_changed:
                messages.success(request, _("Successfully updated and approved evaluation."))
            else:
                messages.success(request, _("Successfully approved evaluation."))
        else:
            messages.success(request, _("Successfully updated evaluation."))

        return redirect("contributor:index")

    preview_html = None
    if preview and forms_are_valid:
        preview_html = render_preview(request, formset, evaluation_form, evaluation)

    if not forms_are_valid and (evaluation_form.errors or formset.errors):
        if preview:
            messages.error(request, _("The preview could not be rendered. Please resolve the errors shown below."))
        else:
            messages.error(request, _("The form was not saved. Please resolve the errors shown below."))

    sort_formset(request, formset)
    template_data = dict(
        form=evaluation_form,
        formset=formset,
        evaluation=evaluation,
        editable=True,
        preview_html=preview_html,
        questionnaires_with_answers_per_contributor={},
    )
    return render(request, "contributor_evaluation_form.html", template_data)
Esempio n. 4
0
def course_edit(request, course_id):
    user = request.user
    course = get_object_or_404(Course, id=course_id)

    # check rights
    if not (course.is_user_editor_or_delegate(user)
            and course.state == 'prepared'):
        raise PermissionDenied

    InlineContributionFormset = inlineformset_factory(
        Course,
        Contribution,
        formset=ContributionFormSet,
        form=EditorContributionForm,
        extra=1)

    course_form = CourseForm(request.POST or None, instance=course)
    formset = InlineContributionFormset(request.POST or None,
                                        instance=course,
                                        form_kwargs={'course': course})

    if course_form.is_valid() and formset.is_valid():
        operation = request.POST.get('operation')
        if operation not in ('save', 'approve'):
            raise SuspiciousOperation("Invalid POST operation")

        course_form.save(user=user)
        formset.save()

        if operation == 'approve':
            # approve course
            course.editor_approve()
            course.save()
            messages.success(request,
                             _("Successfully updated and approved course."))
        else:
            messages.success(request, _("Successfully updated course."))

        return redirect('contributor:index')
    else:
        if course_form.errors or formset.errors:
            messages.error(
                request,
                _("The form was not saved. Please resolve the errors shown below."
                  ))
        sort_formset(request, formset)
        template_data = dict(
            form=course_form,
            formset=formset,
            course=course,
            editable=True,
            responsible=course.responsible_contributor.username)
        return render(request, "contributor_course_form.html", template_data)
Esempio n. 5
0
def evaluation_edit(request, evaluation_id):
    evaluation = get_object_or_404(Evaluation, id=evaluation_id)

    # check rights
    if not (evaluation.is_user_editor_or_delegate(request.user) and evaluation.state == 'prepared'):
        raise PermissionDenied

    post_operation = request.POST.get('operation') if request.POST else None
    preview = post_operation == 'preview'

    InlineContributionFormset = inlineformset_factory(Evaluation, Contribution, formset=ContributionFormSet, form=EditorContributionForm, extra=1)
    evaluation_form = EvaluationForm(request.POST or None, instance=evaluation)
    formset = InlineContributionFormset(request.POST or None, instance=evaluation, form_kwargs={'evaluation': evaluation})

    forms_are_valid = evaluation_form.is_valid() and formset.is_valid()
    if forms_are_valid and not preview:
        if post_operation not in ('save', 'approve'):
            raise SuspiciousOperation("Invalid POST operation")

        form_has_changed = evaluation_form.has_changed() or formset.has_changed()

        if form_has_changed:
            evaluation.set_last_modified(request.user)
        evaluation_form.save()
        formset.save()

        if post_operation == 'approve':
            evaluation.editor_approve()
            evaluation.save()
            if form_has_changed:
                messages.success(request, _("Successfully updated and approved evaluation."))
            else:
                messages.success(request, _("Successfully approved evaluation."))
        else:
            messages.success(request, _("Successfully updated evaluation."))

        return redirect('contributor:index')
    else:
        preview_html = None
        if preview and forms_are_valid:
            preview_html = render_preview(request, formset, evaluation_form, evaluation)

        if not forms_are_valid and (evaluation_form.errors or formset.errors):
            if preview:
                messages.error(request, _("The preview could not be rendered. Please resolve the errors shown below."))
            else:
                messages.error(request, _("The form was not saved. Please resolve the errors shown below."))

        sort_formset(request, formset)
        template_data = dict(form=evaluation_form, formset=formset, evaluation=evaluation, editable=True, preview_html=preview_html)
        return render(request, "contributor_evaluation_form.html", template_data)
Esempio n. 6
0
def course_edit(request, course_id):
    user = request.user
    course = get_object_or_404(Course, id=course_id)

    # check rights
    if not (course.is_user_editor_or_delegate(user) and course.state == 'prepared'):
        raise PermissionDenied

    post_operation = request.POST.get('operation') if request.POST else None
    preview = post_operation == 'preview'

    InlineContributionFormset = inlineformset_factory(Course, Contribution, formset=ContributionFormSet, form=EditorContributionForm, extra=1)
    course_form = CourseForm(request.POST or None, instance=course)
    formset = InlineContributionFormset(request.POST or None, instance=course, form_kwargs={'course': course})

    forms_are_valid = course_form.is_valid() and formset.is_valid()

    if forms_are_valid and not preview:
        if post_operation not in ('save', 'approve'):
            raise SuspiciousOperation("Invalid POST operation")

        course_form.save(user=user)
        formset.save()

        if post_operation == 'approve':
            course.editor_approve()
            course.save()
            messages.success(request, _("Successfully updated and approved course."))
        else:
            messages.success(request, _("Successfully updated course."))

        return redirect('contributor:index')
    else:
        preview_html = None
        if preview and forms_are_valid:
            preview_html = render_preview(request, formset, course_form, course)

        if not forms_are_valid and (course_form.errors or formset.errors):
            if preview:
                messages.error(request, _("The preview could not be rendered. Please resolve the errors shown below."))
            else:
                messages.error(request, _("The form was not saved. Please resolve the errors shown below."))

        sort_formset(request, formset)
        template_data = dict(form=course_form, formset=formset, course=course, editable=True, preview_html=preview_html,
                             responsibles=[contributor.username for contributor in course.responsible_contributors])
        return render(request, "contributor_course_form.html", template_data)
Esempio n. 7
0
def helper_course_edit(request, semester, course):
    InlineContributionFormset = inlineformset_factory(
        Course,
        Contribution,
        formset=ContributionFormSet,
        form=ContributionForm,
        extra=1)

    form = CourseForm(request.POST or None, instance=course)
    formset = InlineContributionFormset(request.POST or None,
                                        instance=course,
                                        form_kwargs={'course': course})

    operation = request.POST.get('operation')

    if form.is_valid() and formset.is_valid():
        if operation not in ('save', 'approve'):
            raise SuspiciousOperation("Invalid POST operation")
        if course.state in ['evaluated', 'reviewed'
                            ] and course.is_in_evaluation_period:
            course.reopen_evaluation()
        form.save(user=request.user)
        formset.save()

        if operation == 'approve':
            # approve course
            course.staff_approve()
            course.save()
            messages.success(request,
                             _("Successfully updated and approved course."))
        else:
            messages.success(request, _("Successfully updated course."))

        return custom_redirect('staff:semester_view', semester.id)
    else:
        sort_formset(request, formset)
        template_data = dict(semester=semester,
                             course=course,
                             form=form,
                             formset=formset,
                             staff=True)
        return render(request, "staff_course_form.html", template_data)
Esempio n. 8
0
def helper_course_edit(request, semester, course):
    InlineContributionFormset = inlineformset_factory(
        Course, Contribution, formset=ContributionFormSet, form=ContributionForm, extra=1, exclude=("course",)
    )

    form = CourseForm(request.POST or None, instance=course)
    formset = InlineContributionFormset(
        request.POST or None, instance=course, queryset=course.contributions.exclude(contributor=None)
    )

    if form.is_valid() and formset.is_valid():
        if course.state in ["evaluated", "reviewed"] and course.is_in_evaluation_period:
            course.reopen_evaluation()
        form.save(user=request.user)
        formset.save()

        messages.success(request, _("Successfully updated course."))
        return custom_redirect("staff:semester_view", semester.id)
    else:
        sort_formset(request, formset)
        template_data = dict(semester=semester, course=course, form=form, formset=formset, staff=True)
        return render(request, "staff_course_form.html", template_data)
Esempio n. 9
0
def course_edit(request, course_id):
    course = get_object_or_404(Course, id=course_id)

    # check rights
    if not (course.is_user_editor_or_delegate(request.user)
            and course.state == 'prepared'):
        raise PermissionDenied

    post_operation = request.POST.get('operation') if request.POST else None
    preview = post_operation == 'preview'

    InlineContributionFormset = inlineformset_factory(
        Course,
        Contribution,
        formset=ContributionFormSet,
        form=EditorContributionForm,
        extra=1)
    course_form = CourseForm(request.POST or None, instance=course)
    formset = InlineContributionFormset(request.POST or None,
                                        instance=course,
                                        can_change_responsible=False,
                                        form_kwargs={'course': course})

    forms_are_valid = course_form.is_valid() and formset.is_valid()
    if forms_are_valid and not preview:
        if post_operation not in ('save', 'approve'):
            raise SuspiciousOperation("Invalid POST operation")

        form_has_changed = course_form.has_changed() or formset.has_changed()

        if form_has_changed:
            course.set_last_modified(request.user)
        course_form.save()
        formset.save()

        if post_operation == 'approve':
            course.editor_approve()
            course.save()
            if form_has_changed:
                messages.success(
                    request, _("Successfully updated and approved course."))
            else:
                messages.success(request, _("Successfully approved course."))
        else:
            messages.success(request, _("Successfully updated course."))

        return redirect('contributor:index')
    else:
        preview_html = None
        if preview and forms_are_valid:
            preview_html = render_preview(request, formset, course_form,
                                          course)

        if not forms_are_valid and (course_form.errors or formset.errors):
            if preview:
                messages.error(
                    request,
                    _("The preview could not be rendered. Please resolve the errors shown below."
                      ))
            else:
                messages.error(
                    request,
                    _("The form was not saved. Please resolve the errors shown below."
                      ))

        sort_formset(request, formset)
        template_data = dict(
            form=course_form,
            formset=formset,
            course=course,
            editable=True,
            preview_html=preview_html,
            responsibles=[
                contributor.username
                for contributor in course.responsible_contributors
            ])
        return render(request, "contributor_course_form.html", template_data)