Example #1
0
def question_update(request, survey_id, question_id, template_name="questions/question_form.html"):

    survey = get_object_or_404(Survey, pk=survey_id)
    question = get_object_or_404(Question, pk=question_id)

    form = QuestionForm(request.POST or None, instance=question)
    if form.is_valid():
        form.save()

        choices = request.POST.getlist("choice")

        for choice in choices:
            choice_obj = Choice()
            choice_obj.question = question
            choice_obj.choice = choice
            choice_obj.save()

        if "add_another" in request.POST:
            return redirect("question_new", survey_id)
        else:
            return redirect("survey_view", survey_id)
    return render(request, template_name, {"form": form, "survey": survey})
Example #2
0
def question_create(request, survey_id, template_name="questions/question_form.html"):
    survey = get_object_or_404(Survey, pk=survey_id)
    form = QuestionForm(request.POST or None)
    if form.is_valid():
        question = form.save(commit=False)
        question.survey = survey
        question.save()
        # save anwers
        # print(request.POST)
        choices = request.POST.getlist("choice")

        for choice in choices:
            choice_obj = Choice()
            choice_obj.question = question
            choice_obj.choice = choice
            choice_obj.save()

        # form.save()
        if "add_another" in request.POST:
            return redirect("question_new", survey_id)
        else:
            return redirect("survey_view", survey_id)
    return render(request, template_name, {"form": form, "survey": survey})
Example #3
0
def edit(request, survey_id):
    s = get_object_or_404(Survey, pk=survey_id)
    # only admins and survey owner can view this page
    if request.user.is_staff or (request.user.is_authenticated()
                                 and s.owner == request.user):
        if request.method == "POST":
            # editing is only allowed when survey status is 'ACTIVE'
            if s.status == s.STATUS_ACTIVE:
                errors = {}
                for k, v in request.POST.items():
                    if k.startswith("surveyTitle"):
                        s.title = v
                        s.save()
                    elif k.startswith("questionName"):
                        k, pk = k.split('-')
                        q = s.question_set.get(pk=pk)
                        q.question = v
                        q.save()
                    elif k.startswith("questionType"):
                        k, pk = k.split('-')
                        q = s.question_set.get(pk=pk)
                        q.type = int(v)
                        q.save()
                    elif k.startswith("choice"):
                        k, pk = k.split('-')
                        c = Choice.objects.get(pk=pk)
                        if v and v != "Add new choice":
                            c.choice = v
                            c.save()
                        else:
                            c.delete()
                    elif k.startswith("newChoice"):
                        k, pk = k.split('-')
                        if v and v != "Add new choice" and v != "":
                            q = s.question_set.get(pk=pk)
                            c = Choice()
                            c.question = q
                            c.choice = v
                            c.votes = 0
                            c.save()
                    elif k == "newQuestion" and v and v != "Enter a new question":
                        q = Question()
                        q.survey = s
                        q.question = v
                        if request.POST["newQuestionType"]:
                            q.type = int(request.POST["newQuestionType"])
                        else:
                            q.type = 1
                        q.save()
                if 'publish' in request.POST:
                    if s.question_set.all().count() == 0:
                        errors[
                            'publishError'] = "You cannot publish an empty survey. Create some questions first."
                        return render_to_response(
                            'surveys/edit/edit.html', {
                                'survey':
                                s,
                                'today':
                                datetime.datetime.now(),
                                'tomorrow':
                                datetime.datetime.now() +
                                datetime.timedelta(days=1),
                                'errors':
                                errors
                            },
                            context_instance=RequestContext(request))
                    if 'resultDisplay' in request.POST and 'endtime' in request.POST:
                        if not s.publish(
                                resultDisplay=request.POST["resultDisplay"],
                                endtime=dateutil.parser.parse(
                                    request.POST["endtime"], dayfirst=True)):
                            return HttpResponseRedirect(
                                reverse('surveys.views.edit', args=(s.id, )))
            # actions for published surveys
            elif s.status == s.STATUS_PUBLISHED:
                if "end" in request.POST:
                    ACTION_CONFIRM = 0
                    ACTION_END = 1
                    if "confirm" in request.POST:
                        s.end()
                        return render_to_response(
                            'surveys/edit/end.html', {
                                'survey': s,
                                'action': ACTION_END
                            },
                            context_instance=RequestContext(request))
                    if "negative" in request.POST:
                        return HttpResponseRedirect(
                            reverse('surveys.views.edit', args=(s.id, )))
                    else:
                        return render_to_response(
                            'surveys/edit/end.html', {
                                'survey': s,
                                'action': ACTION_CONFIRM
                            },
                            context_instance=RequestContext(request))
                if "unpublish" in request.POST:
                    if request.user.is_staff:
                        s.status = s.STATUS_ACTIVE
                        s.save()
            elif s.status == s.STATUS_INACTIVE:
                if "undelete" in request.POST:
                    if request.user.is_staff:
                        s.status = s.STATUS_ACTIVE
                        s.save()
            if not s.status == s.STATUS_INACTIVE:
                if "delete" in request.POST:
                    if request.user.is_staff:
                        s.status = s.STATUS_INACTIVE
                        s.save()

            if "ban" in request.POST:
                if request.user.is_staff:
                    s.owner.is_active = False
                    s.owner.save()

            elif "unban" in request.POST:
                if request.user.is_staff:
                    s.owner.is_active = True
                    s.owner.save()

            return HttpResponseRedirect(
                reverse('surveys.views.edit', args=(s.id, )))
        return render_to_response(
            'surveys/edit/edit.html', {
                'survey': s,
                'today': datetime.datetime.now(),
                'tomorrow':
                datetime.datetime.now() + datetime.timedelta(days=1)
            },
            context_instance=RequestContext(request))
    else:
        return render_to_response('surveys/edit/error.html', {
            'survey': s,
        })
Example #4
0
def edit(request, survey_id):
    s = get_object_or_404(Survey, pk=survey_id)
    # only admins and survey owner can view this page
    if request.user.is_staff or (request.user.is_authenticated() and s.owner == request.user):
        if request.method == "POST":
            # editing is only allowed when survey status is 'ACTIVE'
            if s.status == s.STATUS_ACTIVE:
                errors = {}
                for k, v in request.POST.items():
                    if k.startswith("surveyTitle"):
                        s.title = v
                        s.save()
                    elif k.startswith("questionName"):
                        k, pk = k.split('-')
                        q = s.question_set.get(pk=pk)
                        q.question = v
                        q.save()
                    elif k.startswith("questionType"):
                        k, pk = k.split('-')
                        q = s.question_set.get(pk=pk)
                        q.type = int(v)
                        q.save()
                    elif k.startswith("choice"):
                        k, pk = k.split('-')
                        c = Choice.objects.get(pk=pk)
                        if v and v != "Add new choice":
                            c.choice = v
                            c.save()
                        else:
                            c.delete()
                    elif k.startswith("newChoice"):
                        k, pk = k.split('-')
                        if v and v != "Add new choice" and v != "":
                            q = s.question_set.get(pk=pk)
                            c = Choice()
                            c.question = q
                            c.choice = v
                            c.votes = 0
                            c.save()
                    elif k == "newQuestion" and v and v != "Enter a new question":
                        q = Question()
                        q.survey = s
                        q.question = v
                        if request.POST["newQuestionType"]:
                            q.type = int(request.POST["newQuestionType"])
                        else:
                            q.type = 1
                        q.save()
                if 'publish' in request.POST:
                    if s.question_set.all().count() == 0:
                        errors['publishError'] = "You cannot publish an empty survey. Create some questions first."
                        return render_to_response('surveys/edit/edit.html', {'survey': s, 'today':datetime.datetime.now(), 'tomorrow': datetime.datetime.now() + datetime.timedelta(days=1), 'errors': errors}, context_instance=RequestContext(request))
                    if 'resultDisplay' in request.POST and 'endtime' in request.POST:
                        if not s.publish(resultDisplay=request.POST["resultDisplay"], endtime=dateutil.parser.parse(request.POST["endtime"], dayfirst=True)):
                            return HttpResponseRedirect(reverse('surveys.views.edit', args=(s.id,)))
            # actions for published surveys
            elif s.status == s.STATUS_PUBLISHED:
                if "end" in request.POST:
                    ACTION_CONFIRM = 0
                    ACTION_END = 1
                    if "confirm" in request.POST:
                        s.end()
                        return render_to_response('surveys/edit/end.html', {'survey': s, 'action': ACTION_END}, context_instance=RequestContext(request))
                    if "negative" in request.POST:
                        return HttpResponseRedirect(reverse('surveys.views.edit', args=(s.id,)))
                    else:
                        return render_to_response('surveys/edit/end.html', {'survey': s, 'action': ACTION_CONFIRM}, context_instance=RequestContext(request))
                if "unpublish" in request.POST:
                    if request.user.is_staff:
                        s.status = s.STATUS_ACTIVE
                        s.save()
            elif s.status == s.STATUS_INACTIVE:
                if "undelete" in request.POST:
                    if request.user.is_staff:
                        s.status = s.STATUS_ACTIVE
                        s.save()
            if not s.status == s.STATUS_INACTIVE:
                if "delete" in request.POST:
                    if request.user.is_staff:
                        s.status = s.STATUS_INACTIVE
                        s.save()

            if "ban" in request.POST:
                if request.user.is_staff:
                    s.owner.is_active = False
                    s.owner.save()

            elif "unban" in request.POST:
                if request.user.is_staff:
                    s.owner.is_active = True
                    s.owner.save()


            return HttpResponseRedirect(reverse('surveys.views.edit', args=(s.id,)))
        return render_to_response('surveys/edit/edit.html', {'survey': s, 'today':datetime.datetime.now(), 'tomorrow': datetime.datetime.now() + datetime.timedelta(days=1)}, context_instance=RequestContext(request))
    else:
        return render_to_response('surveys/edit/error.html', {'survey': s,})