示例#1
0
def survey_detail(request, slug):
    """ When you load the survey, this view decides what to do. It displays
    the form, redirects to the results page, displays messages, or whatever
    makes sense based on the survey, the user, and the user's entries. """
    survey = _get_survey_or_404(slug, request)
    if not survey.is_open:
        return _survey_results_redirect(request, survey)
    # if not survey.is_open and survey.can_have_public_submissions():
    #     return _survey_results_redirect(request, survey)
    need_login = (survey.is_open
                  and survey.require_login
                  and not request.user.is_authenticated())
    if _can_show_form(request, survey):

        if request.method == 'POST':
            return _survey_submit(request, survey)
        else:
            #create submission with status opened, it will be filtered out in reports
            email = request.GET.get('email')  # contact email is in the querystring

            if email and Contact.objects.filter(email=email):
                contact = Contact.objects.filter(email=email)[0]
                already_submitted = Submission.objects.filter(survey=survey, contact=contact,
                                                              status=Submission.COMPLETED)
                if already_submitted.count() > 0:
                    if survey.allow_multiple_submissions:
                        #get the latest
                        submission_ = already_submitted.latest('submitted_at')
                        # submission_.status = Submission.OPENED
                    else:
                        #redirect to already submitted
                        return render_to_response('admin/survey/thanks_already_submitted.html',
                                                  {'survey': survey},
                                                  context_instance=RequestContext(request))
                else:
                    pending_sub = Submission.objects.filter(survey=survey, contact=contact, status=Submission.OPENED)
                    if len(pending_sub) > 0:
                        submission_ = pending_sub[0]
                    else:
                        submission_ = Submission()
                        submission_.contact = contact
                        submission_.survey = survey
                        submission.is_public = not survey.moderate_submissions
                        submission_.status = Submission.OPENED
                #it cannot be null. ip_address will be updated at submission completed
                submission_.ip_address = _get_remote_ip(request)
                submission_.save()
        forms = forms_for_survey(survey, request)
    elif need_login:
        messages.error(request, 'Per compilare il questionario è necessario autenticarsi.')
        return HttpResponseRedirect(_login_url(request))
    elif survey.can_have_public_submissions():
        return _survey_results_redirect(request, survey, thanks=True)
    else:  # Survey is closed with private results.
        forms = ()
    return _survey_show_form(request, survey, forms)
示例#2
0
def _submit_valid_forms(forms, request, survey):
    if not all(form.is_valid() for form in forms):
        return False

    email = request.GET.get('email')  # contact email is in the querystring
    survey_score = 0
    total_score = 0

    if email is not None:
        if Contact.objects.filter(email=email):
            contact = Contact.objects.filter(email=email)[0]
            submissions_ = Submission.objects.filter(contact=contact, survey=survey, status=Submission.OPENED)

            if len(submissions_) > 0:
                submission_ = submissions_[0]  # there is only one!
                submission_.ip_address = _get_remote_ip(request)
                submission_.contact = contact
                # delete old answers if any. This will avoid double counting for
                # users who redo surveys
                Answer.objects.filter(submission=submission_).delete()

                for form in forms[1:]:
                    answer = form.save(commit=False)
                    if isinstance(answer, (list, tuple)):
                        for a in answer:
                            a.submission = submission_
                            a.save()
                    elif answer:
                        answer.submission = submission_
                        answer.save()
                answs = Answer.objects.filter(submission=submission_)
                total_score, survey_score = get_scores(answs, contact)
                submission_.status = Submission.COMPLETED
                submission_.save()

                email_to_notify = survey.email
                subject = "{} {} ha completato il questionario {}".format(contact.surname,
                                                                          contact.name,
                                                                          survey.title)
                text = """
                Il contatto {} {} <{}> ha completato il questionario {}
                in data {}.
                Newsletter: {}.
                Punteggio: {}/{}.
                Campagna: {}""".format(contact.surname, contact.name, email,
                                       survey.title, submission_.submitted_at,
                                       survey.newsletter.name,
                                       total_score, survey_score,
                                       survey.newsletter.campaign.name)

                send_single_email(email_to_notify, subject, text)
    return _thanks(request, survey, total_score, survey_score)