def test_fields_disabled_when_editors_disallowed_to_edit(self): evaluation = baker.make(Evaluation) form = EvaluationForm(instance=evaluation) self.assertFalse( all(form.fields[field].disabled for field in form.fields)) evaluation.allow_editors_to_edit = False evaluation.save() form = EvaluationForm(instance=evaluation) self.assertTrue( all(form.fields[field].disabled for field in form.fields))
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)
def evaluation_view(request, evaluation_id): user = request.user evaluation = get_object_or_404(Evaluation, id=evaluation_id) # check rights if (not evaluation.is_user_editor_or_delegate(user) or not Evaluation.State.PREPARED <= evaluation.state <= Evaluation.State.REVIEWED): raise PermissionDenied InlineContributionFormset = inlineformset_factory( Evaluation, Contribution, formset=ContributionFormset, form=EditorContributionForm, extra=0) form = EvaluationForm(request.POST or None, instance=evaluation) formset = InlineContributionFormset(request.POST or None, instance=evaluation) # make everything read-only for cform in formset.forms + [form]: for field in cform.fields.values(): field.disabled = True template_data = dict( form=form, formset=formset, evaluation=evaluation, editable=False, questionnaires_with_answers_per_contributor={}, ) return render(request, "contributor_evaluation_form.html", template_data)
def evaluation_view(request, evaluation_id): user = request.user evaluation = get_object_or_404(Evaluation, id=evaluation_id) # check rights if not evaluation.is_user_editor_or_delegate( user) or evaluation.state not in [ 'prepared', 'editor_approved', 'approved', 'in_evaluation', 'evaluated', 'reviewed' ]: raise PermissionDenied InlineContributionFormset = inlineformset_factory( Evaluation, Contribution, formset=ContributionFormSet, form=EditorContributionForm, extra=0) form = EvaluationForm(request.POST or None, instance=evaluation) formset = InlineContributionFormset(request.POST or None, instance=evaluation) # make everything read-only for cform in formset.forms + [form]: for field in cform.fields.values(): field.disabled = True template_data = dict(form=form, formset=formset, evaluation=evaluation, editable=False) return render(request, "contributor_evaluation_form.html", template_data)
def test_edit_participants(self): student = baker.make(UserProfile) evaluation = baker.make(Evaluation, course__degrees=[baker.make(Degree)], participants=[student]) evaluation.general_contribution.questionnaires.set( [baker.make(Questionnaire)]) form_data = get_form_data_from_instance(EvaluationForm, evaluation) form = EvaluationForm(form_data, instance=evaluation) self.assertEqual(len(form["participants"].initial), 1) form_data["participants"].remove(student.pk) EvaluationForm(form_data, instance=evaluation).save() self.assertEqual(evaluation.num_participants, 0) form_data["participants"].append(student.pk) EvaluationForm(form_data, instance=evaluation).save() del evaluation.num_participants # discard cached property self.assertEqual(evaluation.num_participants, 1)
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)
def test_locked_questionnaire(self): """ Asserts that locked (general) questionnaires cannot be changed. """ questionnaire = baker.make(Questionnaire, type=Questionnaire.Type.TOP, is_locked=False, visibility=Questionnaire.Visibility.EDITORS) locked_questionnaire = baker.make( Questionnaire, type=Questionnaire.Type.TOP, is_locked=True, visibility=Questionnaire.Visibility.EDITORS) evaluation = baker.make(Evaluation) evaluation.general_contribution.questionnaires.add(questionnaire) form_data = get_form_data_from_instance(EvaluationForm, evaluation) form_data["general_questionnaires"] = [ questionnaire.pk, locked_questionnaire.pk ] # add locked questionnaire form = EvaluationForm(form_data, instance=evaluation) # Assert form is valid, but locked questionnaire is not added form.save() self.assertEqual( {questionnaire}, set(evaluation.general_contribution.questionnaires.all())) evaluation.general_contribution.questionnaires.add( locked_questionnaire) form_data = get_form_data_from_instance(EvaluationForm, evaluation) form_data["general_questionnaires"] = [questionnaire.pk ] # remove locked questionnaire form = EvaluationForm(form_data, instance=evaluation) # Assert form is valid, but locked questionnaire is not removed form.save() self.assertEqual( {questionnaire, locked_questionnaire}, set(evaluation.general_contribution.questionnaires.all()))