def test_single_result_can_be_published(self): """ Regression test for #1238 """ responsible = mommy.make(UserProfile) single_result = mommy.make(Course, semester=mommy.make(Semester), is_single_result=True, _participant_count=5, _voter_count=5) contribution = mommy.make( Contribution, course=single_result, contributor=responsible, responsible=True, can_edit=True, comment_visibility=Contribution.ALL_COMMENTS, questionnaires=[Questionnaire.single_result_questionnaire()]) mommy.make(RatingAnswerCounter, answer=1, count=1, question=Questionnaire.single_result_questionnaire(). questions.first(), contribution=contribution) single_result.single_result_created() single_result.publish() # used to crash
def test_single_result_can_be_deleted_only_in_reviewed(self): responsible = mommy.make(UserProfile) evaluation = mommy.make(Evaluation, is_single_result=True) contribution = mommy.make( Contribution, evaluation=evaluation, contributor=responsible, can_edit=True, textanswer_visibility=Contribution.GENERAL_TEXTANSWERS, questionnaires=[Questionnaire.single_result_questionnaire()]) mommy.make(RatingAnswerCounter, answer=1, count=1, question=Questionnaire.single_result_questionnaire(). questions.first(), contribution=contribution) evaluation.single_result_created() evaluation.publish() evaluation.save() self.assertTrue(Evaluation.objects.filter(pk=evaluation.pk).exists()) self.assertFalse(evaluation.can_manager_delete) evaluation.unpublish() self.assertTrue(evaluation.can_manager_delete) RatingAnswerCounter.objects.filter( contribution__evaluation=evaluation).delete() evaluation.delete() self.assertFalse(Evaluation.objects.filter(pk=evaluation.pk).exists())
def test_single_result_can_be_deleted_only_in_reviewed(self): responsible = baker.make(UserProfile) evaluation = baker.make(Evaluation, is_single_result=True) contribution = baker.make( Contribution, evaluation=evaluation, contributor=responsible, questionnaires=[Questionnaire.single_result_questionnaire()], role=Contribution.Role.EDITOR, textanswer_visibility=Contribution.TextAnswerVisibility.GENERAL_TEXTANSWERS, ) make_rating_answer_counters(Questionnaire.single_result_questionnaire().questions.first(), contribution) evaluation.skip_review_single_result() evaluation.publish() evaluation.save() self.assertTrue(Evaluation.objects.filter(pk=evaluation.pk).exists()) self.assertFalse(evaluation.can_be_deleted_by_manager) evaluation.unpublish() self.assertTrue(evaluation.can_be_deleted_by_manager) RatingAnswerCounter.objects.filter(contribution__evaluation=evaluation).delete() evaluation.delete() self.assertFalse(Evaluation.objects.filter(pk=evaluation.pk).exists())
def test_single_result_can_be_deleted_only_in_reviewed(self): responsible = mommy.make(UserProfile) course = mommy.make(Course, semester=mommy.make(Semester), is_single_result=True) contribution = mommy.make( Contribution, course=course, contributor=responsible, responsible=True, can_edit=True, comment_visibility=Contribution.ALL_COMMENTS, questionnaires=[Questionnaire.single_result_questionnaire()]) mommy.make(RatingAnswerCounter, answer=1, count=1, question=Questionnaire.single_result_questionnaire(). questions.first(), contribution=contribution) course.single_result_created() course.publish() course.save() self.assertTrue(Course.objects.filter(pk=course.pk).exists()) self.assertFalse(course.can_manager_delete) course.unpublish() self.assertTrue(course.can_manager_delete) RatingAnswerCounter.objects.filter( contribution__course=course).delete() course.delete() self.assertFalse(Course.objects.filter(pk=course.pk).exists())
def test_single_result_can_be_published(): """ Regression test for #1238 """ responsible = baker.make(UserProfile) single_result = baker.make(Evaluation, is_single_result=True, _participant_count=5, _voter_count=5) contribution = baker.make( Contribution, evaluation=single_result, contributor=responsible, questionnaires=[Questionnaire.single_result_questionnaire()], role=Contribution.Role.EDITOR, textanswer_visibility=Contribution.TextAnswerVisibility. GENERAL_TEXTANSWERS, ) baker.make(RatingAnswerCounter, answer=1, count=1, question=Questionnaire.single_result_questionnaire(). questions.first(), contribution=contribution) single_result.single_result_created() single_result.publish() # used to crash
def save(self, *args, **kw): user = kw.pop("user") self.instance.last_modified_user = user event_date = self.cleaned_data['event_date'] self.instance.vote_start_datetime = date_to_datetime(event_date) self.instance.vote_end_date = event_date self.instance.is_graded = False self.instance.is_single_result = True super().save(*args, **kw) single_result_questionnaire = Questionnaire.single_result_questionnaire() single_result_question = single_result_questionnaire.question_set.first() contribution, created = Contribution.objects.get_or_create(course=self.instance, responsible=True, can_edit=True, comment_visibility=Contribution.ALL_COMMENTS) contribution.contributor = self.cleaned_data['responsible'] if created: contribution.questionnaires.add(single_result_questionnaire) contribution.save() # set answers contribution = Contribution.objects.get(course=self.instance, responsible=True) total_votes = 0 for i in range(1, 6): count = self.cleaned_data['answer_' + str(i)] total_votes += count RatingAnswerCounter.objects.update_or_create(contribution=contribution, question=single_result_question, answer=i, defaults={'count': count}) self.instance._participant_count = total_votes self.instance._voter_count = total_votes # change state to "reviewed" # works only for single_results so the course and its contribution must be saved first self.instance.single_result_created() self.instance.save()
def test_single_result_anonymization(self): questionnaire = Questionnaire.single_result_questionnaire() single_result = baker.make(Evaluation, is_single_result=True, course=self.course) single_result.general_contribution.questionnaires.set([questionnaire]) question = Question.objects.get(questionnaire=questionnaire) answer_count_before = 0 choices = [ choice for choice in CHOICES[question.type].values if choice != NO_ANSWER ] answer_counts = [random.randint(50, 100) for answer in choices] answer_count_before = sum(answer_counts) make_rating_answer_counters(question, single_result.general_contribution, answer_counts) management.call_command("anonymize", stdout=StringIO()) self.assertLessEqual(RatingAnswerCounter.objects.count(), len(choices)) self.assertEqual( RatingAnswerCounter.objects.aggregate(Sum("count"))["count__sum"], answer_count_before)
def test_calculate_average_course_distribution(self): baker.make(RatingAnswerCounter, question=self.question_grade, contribution=self.contribution1, answer=1, count=2) course = self.evaluation.course single_result = baker.make( Evaluation, name_de="Single Result", name_en="Single Result", course=course, weight=3, is_single_result=True, vote_start_datetime=datetime.now(), vote_end_date=datetime.now().date(), state="published", ) single_result_questionnaire = Questionnaire.single_result_questionnaire() single_result_question = single_result_questionnaire.questions.first() contribution = baker.make(Contribution, evaluation=single_result, contributor=None, questionnaires=[single_result_questionnaire]) baker.make(RatingAnswerCounter, question=single_result_question, contribution=contribution, answer=2, count=1) baker.make(RatingAnswerCounter, question=single_result_question, contribution=contribution, answer=3, count=1) cache_results(single_result) cache_results(self.evaluation) distribution = calculate_average_course_distribution(course) self.assertEqual(distribution[0], 0.25) self.assertEqual(distribution[1], 0.375) self.assertEqual(distribution[2], 0.375) self.assertEqual(distribution[3], 0) self.assertEqual(distribution[4], 0)
def test_calculate_average_course_distribution(self): mommy.make(RatingAnswerCounter, question=self.question_grade, contribution=self.contribution1, answer=1, count=2) course = self.evaluation.course single_result = mommy.make( Evaluation, name_de="Single Result", name_en="Single Result", course=course, weight=3, is_single_result=True, vote_start_datetime=datetime.now(), vote_end_date=datetime.now().date(), state="published", ) single_result_questionnaire = Questionnaire.single_result_questionnaire() single_result_question = single_result_questionnaire.questions.first() contribution = mommy.make(Contribution, evaluation=single_result, contributor=None, questionnaires=[single_result_questionnaire]) mommy.make(RatingAnswerCounter, question=single_result_question, contribution=contribution, answer=2, count=1) mommy.make(RatingAnswerCounter, question=single_result_question, contribution=contribution, answer=3, count=1) distribution = calculate_average_course_distribution(course) self.assertEqual(distribution[0], 0.25) self.assertEqual(distribution[1], 0.375) self.assertEqual(distribution[2], 0.375) self.assertEqual(distribution[3], 0) self.assertEqual(distribution[4], 0)
def save(self, *args, **kw): user = kw.pop("user") self.instance.last_modified_user = user self.instance.vote_start_date = self.cleaned_data['event_date'] self.instance.vote_end_date = self.cleaned_data['event_date'] self.instance.is_graded = False super().save(*args, **kw) single_result_questionnaire = Questionnaire.get_single_result_questionnaire() single_result_question = single_result_questionnaire.question_set.first() if not Contribution.objects.filter(course=self.instance, responsible=True).exists(): contribution = Contribution(course=self.instance, contributor=self.cleaned_data['responsible'], responsible=True) contribution.save() contribution.questionnaires.add(single_result_questionnaire) # set answers contribution = Contribution.objects.get(course=self.instance, responsible=True) total_votes = 0 for i in range(1,6): count = self.cleaned_data['answer_'+str(i)] total_votes += count RatingAnswerCounter.objects.update_or_create(contribution=contribution, question=single_result_question, answer=i, defaults={'count': count}) self.instance._participant_count = total_votes self.instance._voter_count = total_votes # change state to "reviewed" # works only for single_results so the course and its contribution must be saved first self.instance.single_result_created() self.instance.save()
def save(self, *args, **kw): user = kw.pop("user") self.instance.last_modified_user = user self.instance.vote_start_date = self.cleaned_data['event_date'] self.instance.vote_end_date = self.cleaned_data['event_date'] self.instance.is_graded = False super().save(*args, **kw) if not Contribution.objects.filter(course=self.instance, responsible=True).exists(): contribution = Contribution( course=self.instance, contributor=self.cleaned_data['responsible'], responsible=True) contribution.save() contribution.questionnaires.add( Questionnaire.get_single_result_questionnaire()) # set answers contribution = Contribution.objects.get(course=self.instance, responsible=True) for i in range(1, 6): count = {'count': self.cleaned_data['answer_' + str(i)]} answer_counter, created = RatingAnswerCounter.objects.update_or_create( contribution=contribution, question=contribution.questionnaires.first( ).question_set.first(), answer=i, defaults=count) # change state to "reviewed" # works only for single_results so the course and its contribution must be saved first self.instance.single_result_created() self.instance.save()
def questionnaire_create(request): questionnaire = Questionnaire() InlineQuestionFormset = inlineformset_factory(Questionnaire, Question, formset=AtLeastOneFormSet, form=QuestionForm, extra=1, exclude=('questionnaire', )) form = QuestionnaireForm(request.POST or None, instance=questionnaire) formset = InlineQuestionFormset(request.POST or None, instance=questionnaire) if form.is_valid() and formset.is_valid(): newQuestionnaire = form.save(commit=False) # set index according to existing questionnaires newQuestionnaire.index = Questionnaire.objects.all().aggregate( Max('index'))['index__max'] + 1 newQuestionnaire.save() form.save_m2m() formset.save() messages.success(request, _("Successfully created questionnaire.")) return redirect('staff:questionnaire_index') else: return render(request, "staff_questionnaire_form.html", dict(form=form, formset=formset))
def test_single_result_anonymization(self): questionnaire = Questionnaire.single_result_questionnaire() single_result = mommy.make(Evaluation, is_single_result=True, course=self.course) single_result.general_contribution.questionnaires.set([questionnaire]) question = Question.objects.get(questionnaire=questionnaire) answer_count_before = 0 choices = [ choice for choice in CHOICES[question.type].values if choice != NO_ANSWER ] random.seed(0) for answer in choices: count = random.randint(50, 100) mommy.make(RatingAnswerCounter, question=question, contribution=single_result.general_contribution, count=count, answer=answer) answer_count_before += count management.call_command('anonymize', stdout=StringIO()) self.assertLessEqual(RatingAnswerCounter.objects.count(), len(choices)) self.assertEqual( RatingAnswerCounter.objects.aggregate(Sum('count'))["count__sum"], answer_count_before)
def test_calculate_average_course_distribution(self): make_rating_answer_counters(self.question_grade, self.contribution1, [2, 0, 0, 0, 0]) course = self.evaluation.course single_result = baker.make( Evaluation, name_de="Single Result", name_en="Single Result", course=course, weight=3, is_single_result=True, vote_start_datetime=datetime.now(), vote_end_date=datetime.now().date(), state=Evaluation.State.PUBLISHED, ) single_result_questionnaire = Questionnaire.single_result_questionnaire( ) single_result_question = single_result_questionnaire.questions.first() contribution = baker.make(Contribution, evaluation=single_result, contributor=None, questionnaires=[single_result_questionnaire]) make_rating_answer_counters(single_result_question, contribution, [0, 1, 1, 0, 0]) cache_results(single_result) cache_results(self.evaluation) distribution = calculate_average_course_distribution(course) self.assertEqual(distribution[0], 0.25) self.assertEqual(distribution[1], 0.375) self.assertEqual(distribution[2], 0.375) self.assertEqual(distribution[3], 0) self.assertEqual(distribution[4], 0)
def test_archiving_participations_doesnt_change_single_results_participant_count(self): responsible = mommy.make(UserProfile) evaluation = mommy.make(Evaluation, state="published", is_single_result=True, _participant_count=5, _voter_count=5) contribution = mommy.make(Contribution, evaluation=evaluation, contributor=responsible, can_edit=True, textanswer_visibility=Contribution.GENERAL_TEXTANSWERS) contribution.questionnaires.add(Questionnaire.single_result_questionnaire()) evaluation.course.semester.archive_participations() evaluation = Evaluation.objects.get(pk=evaluation.pk) self.assertEqual(evaluation._participant_count, 5) self.assertEqual(evaluation._voter_count, 5)
def questionnaire_copy(request, questionnaire_id): if request.method == "POST": questionnaire = Questionnaire() QuestionFormset = inlineformset_factory(Questionnaire, Question, formset=AtLeastOneFormSet, form=QuestionForm, extra=1, exclude=('questionnaire', )) form = QuestionnaireForm(request.POST, instance=questionnaire) formset = QuestionFormset(request.POST.copy(), instance=questionnaire, save_as_new=True) if form.is_valid() and formset.is_valid(): form.save() formset.save() messages.add_message(request, messages.INFO, _("Successfully created questionnaire.")) return redirect('evap.fsr.views.questionnaire_index') else: return render_to_response("fsr_questionnaire_form.html", dict(form=form, formset=formset), context_instance=RequestContext(request)) else: questionnaire = get_object_or_404(Questionnaire, id=questionnaire_id) QuestionFormset = inlineformset_factory(Questionnaire, Question, formset=IdLessQuestionFormSet, form=QuestionForm, extra=1, exclude=('questionnaire', )) form = QuestionnaireForm(instance=questionnaire) formset = QuestionFormset(instance=Questionnaire(), queryset=questionnaire.question_set.all()) return render_to_response("fsr_questionnaire_form.html", dict(form=form, formset=formset), context_instance=RequestContext(request))
def test_archiving_participations_doesnt_change_single_results_participant_count(self): responsible = mommy.make(UserProfile) course = mommy.make(Course, state="published", is_single_result=True) contribution = mommy.make(Contribution, course=course, contributor=responsible, responsible=True, can_edit=True, comment_visibility=Contribution.ALL_COMMENTS) contribution.questionnaires.add(Questionnaire.single_result_questionnaire()) course._participant_count = 5 course._voter_count = 5 course.save() course.semester.archive_participations() self.assertEqual(course._participant_count, 5) self.assertEqual(course._voter_count, 5)
def test_archiving_doesnt_change_single_results_participant_count(self): responsible = mommy.make(UserProfile) course = mommy.make(Course, state="published") contribution = mommy.make(Contribution, course=course, contributor=responsible, responsible=True, can_edit=True, comment_visibility=Contribution.ALL_COMMENTS) contribution.questionnaires.add(Questionnaire.get_single_result_questionnaire()) self.assertTrue(course.is_single_result) course._participant_count = 5 course._voter_count = 5 course.save() course._archive() self.assertEqual(course._participant_count, 5) self.assertEqual(course._voter_count, 5)
def questionnaire_create(request): questionnaire = Questionnaire() QuestionFormset = inlineformset_factory(Questionnaire, Question, formset=AtLeastOneFormSet, form=QuestionForm, extra=1, exclude=('questionnaire',)) form = QuestionnaireForm(request.POST or None, instance=questionnaire) formset = QuestionFormset(request.POST or None, instance=questionnaire) if form.is_valid() and formset.is_valid(): form.save() formset.save() messages.add_message(request, messages.INFO, _("Successfully created questionnaire.")) return redirect('evap.fsr.views.questionnaire_index') else: return render_to_response("fsr_questionnaire_form.html", dict(form=form, formset=formset), context_instance=RequestContext(request))
def save(self, *args, **kw): user = kw.pop("user") self.instance.last_modified_user = user event_date = self.cleaned_data['event_date'] self.instance.vote_start_datetime = date_to_datetime(event_date) self.instance.vote_end_date = event_date self.instance.is_single_result = True evaluation = super().save(*args, **kw) single_result_questionnaire = Questionnaire.single_result_questionnaire( ) single_result_question = single_result_questionnaire.questions.first() contribution, created = Contribution.objects.get_or_create( evaluation=evaluation, contributor=None) if created: contribution.questionnaires.add(single_result_questionnaire) contribution.save() # set answers contribution = Contribution.objects.get(evaluation=evaluation) total_votes = 0 for i in range(1, 6): count = self.cleaned_data['answer_' + str(i)] total_votes += count RatingAnswerCounter.objects.update_or_create( contribution=contribution, question=single_result_question, answer=i, defaults={'count': count}) evaluation._participant_count = total_votes evaluation._voter_count = total_votes # change state to "reviewed" # works only for single_results so the evaluation and its contribution must be saved first evaluation.single_result_created() evaluation.save() if hasattr(self.instance, 'old_course'): if self.instance.old_course != evaluation.course: update_template_cache_of_published_evaluations_in_course( self.instance.old_course) update_template_cache_of_published_evaluations_in_course( evaluation.course) return evaluation
def test_archiving_participations_doesnt_change_single_results_participant_count(self): responsible = baker.make(UserProfile) evaluation = baker.make( Evaluation, state=Evaluation.State.PUBLISHED, is_single_result=True, _participant_count=5, _voter_count=5 ) contribution = baker.make( Contribution, evaluation=evaluation, contributor=responsible, role=Contribution.Role.EDITOR, textanswer_visibility=Contribution.TextAnswerVisibility.GENERAL_TEXTANSWERS, ) contribution.questionnaires.add(Questionnaire.single_result_questionnaire()) evaluation.course.semester.archive() evaluation = Evaluation.objects.get(pk=evaluation.pk) self.assertEqual(evaluation._participant_count, 5) self.assertEqual(evaluation._voter_count, 5)
def save(self, *args, **kw): self.instance.course.is_graded = False self.instance.course.name_de = self.cleaned_data['name_de'] self.instance.course.name_en = self.cleaned_data['name_en'] self.instance.course.type = self.cleaned_data['type'] self.instance.course.save() self.instance.course.responsibles.set([self.cleaned_data['responsible']]) self.instance.course.degrees.set(self.cleaned_data['degrees']) self.instance.course.save() self.instance.course = Course.objects.get(id=self.instance.course.id) user = kw.pop("user") self.instance.last_modified_user = user event_date = self.cleaned_data['event_date'] self.instance.vote_start_datetime = date_to_datetime(event_date) self.instance.vote_end_date = event_date self.instance.is_single_result = True super().save(*args, **kw) single_result_questionnaire = Questionnaire.single_result_questionnaire() single_result_question = single_result_questionnaire.questions.first() contribution, created = Contribution.objects.get_or_create(evaluation=self.instance, contributor=None) if created: contribution.questionnaires.add(single_result_questionnaire) contribution.save() # set answers contribution = Contribution.objects.get(evaluation=self.instance) total_votes = 0 for i in range(1, 6): count = self.cleaned_data['answer_' + str(i)] total_votes += count RatingAnswerCounter.objects.update_or_create(contribution=contribution, question=single_result_question, answer=i, defaults={'count': count}) self.instance._participant_count = total_votes self.instance._voter_count = total_votes # change state to "reviewed" # works only for single_results so the evaluation and its contribution must be saved first self.instance.single_result_created() self.instance.save()
def questionnaire_copy(request, questionnaire_id): if request.method == "POST": questionnaire = Questionnaire() InlineQuestionFormset = inlineformset_factory( Questionnaire, Question, formset=AtLeastOneFormSet, form=QuestionForm, extra=1, exclude=('questionnaire', )) form = QuestionnaireForm(request.POST, instance=questionnaire) formset = InlineQuestionFormset(request.POST.copy(), instance=questionnaire, save_as_new=True) if form.is_valid() and formset.is_valid(): form.save() formset.save() messages.success(request, _("Successfully created questionnaire.")) return redirect('staff:questionnaire_index') else: return render(request, "staff_questionnaire_form.html", dict(form=form, formset=formset)) else: questionnaire = get_object_or_404(Questionnaire, id=questionnaire_id) InlineQuestionFormset = inlineformset_factory( Questionnaire, Question, formset=AtLeastOneFormSet, form=QuestionForm, extra=1, exclude=('questionnaire', )) form = QuestionnaireForm(instance=questionnaire) formset = InlineQuestionFormset( instance=questionnaire, queryset=questionnaire.question_set.all()) return render(request, "staff_questionnaire_form.html", dict(form=form, formset=formset))
def save(self, *args, **kw): user = kw.pop("user") self.instance.last_modified_user = user event_date = self.cleaned_data['event_date'] self.instance.vote_start_datetime = date_to_datetime(event_date) self.instance.vote_end_date = event_date self.instance.is_single_result = True evaluation = super().save(*args, **kw) single_result_questionnaire = Questionnaire.single_result_questionnaire() single_result_question = single_result_questionnaire.questions.first() contribution, created = Contribution.objects.get_or_create(evaluation=evaluation, contributor=None) if created: contribution.questionnaires.add(single_result_questionnaire) contribution.save() # set answers contribution = Contribution.objects.get(evaluation=evaluation) total_votes = 0 for i in range(1, 6): count = self.cleaned_data['answer_' + str(i)] total_votes += count RatingAnswerCounter.objects.update_or_create(contribution=contribution, question=single_result_question, answer=i, defaults={'count': count}) evaluation._participant_count = total_votes evaluation._voter_count = total_votes # change state to "reviewed" # works only for single_results so the evaluation and its contribution must be saved first evaluation.single_result_created() evaluation.save() if hasattr(self.instance, 'old_course'): if self.instance.old_course != evaluation.course: update_template_cache_of_published_evaluations_in_course(self.instance.old_course) update_template_cache_of_published_evaluations_in_course(evaluation.course) return evaluation
def test_get_single_result_rating_result(self): single_result_evaluation = baker.make(Evaluation, state=Evaluation.State.PUBLISHED, is_single_result=True) questionnaire = Questionnaire.single_result_questionnaire() contribution = baker.make( Contribution, contributor=baker.make(UserProfile), evaluation=single_result_evaluation, questionnaires=[questionnaire], role=Contribution.Role.EDITOR, textanswer_visibility=Contribution.TextAnswerVisibility. GENERAL_TEXTANSWERS, ) make_rating_answer_counters(questionnaire.questions.first(), contribution, [1, 0, 0, 1, 0]) cache_results(single_result_evaluation) distribution = calculate_average_distribution(single_result_evaluation) self.assertEqual(distribution, (0.5, 0, 0, 0.5, 0)) rating_result = get_single_result_rating_result( single_result_evaluation) self.assertEqual(rating_result.counts, (1, 0, 0, 1, 0))