Esempio n. 1
0
def vote(request, course_id):
    # retrieve course and make sure that the user is allowed to vote
    course = get_object_or_404(Course, id=course_id)
    if not course.can_user_vote(request.user):
        raise PermissionDenied

    # build forms
    forms = SortedDict()
    for questionnaire, contribution in questionnaires_and_contributions(
            course):
        form = QuestionsForm(request.POST or None,
                             contribution=contribution,
                             questionnaire=questionnaire)
        forms[(contribution, questionnaire)] = form

    if all(form.is_valid() for form in forms.values()):
        # begin vote operation
        with transaction.commit_on_success():
            for (contribution, questionnaire), form in forms.items():
                for question in questionnaire.question_set.all():
                    identifier = make_form_identifier(contribution,
                                                      questionnaire, question)
                    value = form.cleaned_data.get(identifier)

                    if type(value) in [str, unicode]:
                        value = value.strip()

                    if value == 6:  #no answer
                        value = None

                    # store the answer if one was given
                    if value:
                        question.answer_class.objects.create(
                            contribution=contribution,
                            question=question,
                            answer=value)

            # remember that the user voted already
            course.voters.add(request.user)

        messages.add_message(request, messages.INFO,
                             _("Your vote was recorded."))
        return redirect('evap.student.views.index')
    else:
        return render_to_response("student_vote.html",
                                  dict(forms=forms.values(), course=course),
                                  context_instance=RequestContext(request))
Esempio n. 2
0
File: views.py Progetto: maikz/EvaP
def helper_create_voting_form_groups(request, contributions):
    form_groups = OrderedDict()
    for contribution in contributions:
        questionnaires = contribution.questionnaires.all()
        if not questionnaires.exists():
            continue
        form_groups[contribution] = [QuestionsForm(request.POST or None, contribution=contribution, questionnaire=questionnaire) for questionnaire in questionnaires]
    return form_groups
Esempio n. 3
0
def questionnaire_view(request, questionnaire_id):
    questionnaire = get_object_or_404(Questionnaire, id=questionnaire_id)

    # build forms
    contribution = Contribution(contributor=request.user)
    form = QuestionsForm(request.POST or None, contribution=contribution, questionnaire=questionnaire)

    return render_to_response("fsr_questionnaire_view.html", dict(forms=[form], questionnaire=questionnaire), context_instance=RequestContext(request))
Esempio n. 4
0
File: views.py Progetto: Bartzi/EvaP
def vote(request, course_id):
    # retrieve course and make sure that the user is allowed to vote
    course = get_object_or_404(Course, id=course_id)
    if not course.can_user_vote(request.user):
        raise PermissionDenied

    # build forms
    forms = SortedDict()
    for questionnaire, contribution in questionnaires_and_contributions(course):
        form = QuestionsForm(request.POST or None, contribution=contribution, questionnaire=questionnaire)
        forms[(contribution, questionnaire)] = form

    if all(form.is_valid() for form in forms.values()):
        # begin vote operation
        with transaction.commit_on_success():
            for (contribution, questionnaire), form in forms.items():
                for question in questionnaire.question_set.all():
                    identifier = make_form_identifier(contribution, questionnaire, question)
                    value = form.cleaned_data.get(identifier)

                    if type(value) in [str, unicode]:
                        value = value.strip()

                    if value == 6: #no answer
                        value = None

                    # store the answer if one was given
                    if value:
                        question.answer_class.objects.create(
                            contribution=contribution,
                            question=question,
                            answer=value)

            # remember that the user voted already
            course.voters.add(request.user)

        messages.add_message(request, messages.INFO, _("Your vote was recorded."))
        return redirect('evap.student.views.index')
    else:
        return render_to_response(
            "student_vote.html",
            dict(forms=forms.values(),
                 course=course),
            context_instance=RequestContext(request))
Esempio n. 5
0
def course_preview(request, semester_id, course_id):
    semester = get_object_or_404(Semester, id=semester_id)
    course = get_object_or_404(Course, id=course_id)

    # build forms
    forms = SortedDict()
    for questionnaire, contribution in questionnaires_and_contributions(course):
        form = QuestionsForm(request.POST or None, contribution=contribution, questionnaire=questionnaire)
        forms[(contribution, questionnaire)] = form
    return render_to_response("fsr_course_preview.html", dict(forms=forms.values(), course=course, semester=semester), context_instance=RequestContext(request))
Esempio n. 6
0
def questionnaire_view(request, questionnaire_id):
    questionnaire = get_object_or_404(Questionnaire, id=questionnaire_id)

    # build forms
    contribution = Contribution(contributor=request.user)
    form = QuestionsForm(request.POST or None,
                         contribution=contribution,
                         questionnaire=questionnaire)

    return render(request, "staff_questionnaire_view.html",
                  dict(forms=[form], questionnaire=questionnaire))
Esempio n. 7
0
File: views.py Progetto: cyroxx/EvaP
def vote(request, course_id):
    # retrieve course and make sure that the user is allowed to vote
    course = get_object_or_404(Course, id=course_id)
    if not course.can_user_vote(request.user):
        raise PermissionDenied

    # build forms
    form_group = {}
    contributors_with_questionnaires = questionnaires_and_contributions_by_contributor(course)
    for contributor in contributors_with_questionnaires:
        for questionnaire, contribution in contributors_with_questionnaires[contributor]:
            form = QuestionsForm(request.POST or None, contribution=contribution, questionnaire=questionnaire)
            if form.contribution.contributor == request.user:
                continue # users shall not vote about themselves
            if not contributor in form_group:
                form_group[contributor] = SortedDict()
            form_group[contributor][(contribution, questionnaire)] = form

    for contributor in form_group:
        for (contribution, questionnaire), form in form_group[contributor].items():
            for question in questionnaire.question_set.all():
                identifier = make_form_identifier(contribution, questionnaire, question)

    for contributor in form_group:
        for form in form_group[contributor].values():
            if form.is_valid():
                continue
            course_forms = []
            contributor_questionnaires = {}
            errors = []

            for form in form_group[None].values():
                course_forms.append(form)

            for contributor in form_group:
                if contributor == None:
                    continue
                user_profile = UserProfile.get_for_user(contributor)
                contributor_questionnaires[user_profile] = form_group[contributor].values()
                for form in form_group[contributor].values():
                    if form.errors:
                        errors.append(contributor.id)

            return render_to_response(
                "student_vote.html",
                dict(course_forms=course_forms,
                     contributor_questionnaires=contributor_questionnaires,
                     errors=errors,
                     course=course),
                context_instance=RequestContext(request))

    # all forms are valid
    # begin vote operation
    with transaction.commit_on_success():
        for contributor in form_group:
            for (contribution, questionnaire), form in form_group[contributor].items():
                for question in questionnaire.question_set.all():
                    identifier = make_form_identifier(contribution, questionnaire, question)
                    value = form.cleaned_data.get(identifier)

                    if type(value) in [str, unicode]:
                        value = value.strip()

                    if value == 6: #no answer
                        value = None

                    # store the answer if one was given
                    if value:
                        question.answer_class.objects.create(
                            contribution=contribution,
                            question=question,
                            answer=value)

        # remember that the user voted already
        course.voters.add(request.user)

    messages.add_message(request, messages.INFO, _("Your vote was recorded."))
    return redirect('evap.student.views.index')
Esempio n. 8
0
def helper_create_form_group(request, contribution):
    return list(
        QuestionsForm(request.POST or None,
                      contribution=contribution,
                      questionnaire=questionnaire)
        for questionnaire in contribution.questionnaires.all())