Ejemplo n.º 1
0
def questions(request, survey_id, question_id=None):
    survey = get_object_or_404(Survey, id=survey_id)
    if request.method == 'GET':
        qs = Question.objects.filter(survey__id=survey_id).order_by('order', 'id')
        return api_utils.response(request, qs, QuestionGrid)
    elif request.method == 'POST':
        form = api_utils.load_form(request, QuestionForm)
        if form.is_valid():
            question = form.save(commit=False)
            question.survey = survey
            if question.order is None:
                res = Question.objects.filter(survey__id=survey_id).aggregate(Max('order'))
                max_order = res['order__max']
                if max_order:
                    question.order = max_order + 1
                else:
                    question.order = 0
            question.save()
            qs = Question.objects.filter(id=question.id)
            return api_utils.response(request, qs, QuestionGrid)
        else:
            return api_utils.response_error(request, form)
    elif request.method == 'DELETE':
        question = get_object_or_404(Question, id=question_id)
        question.delete()
        return api_utils.success()
    raise Http404
Ejemplo n.º 2
0
def choices(request, survey_id, question_id, choice_id=None):
    question = get_object_or_404(Question, id=question_id)
    if request.method == 'GET':
        qs = Choice.objects.filter(question__id=question_id).order_by('order', 'id')
        return api_utils.response(request, qs, ChoiceGrid)
    elif request.method == 'POST':
        form = api_utils.load_form(request, ChoiceForm)
        if form.is_valid():
            choice = form.save(commit=False)
            choice.question = question
            if choice.order is None:
                res = Choice.objects.filter(question__id=question_id).aggregate(Max('order'))
                max_order = res['order__max']
                if max_order:
                    choice.order = max_order + 1
                else:
                    choice.order = 0
            choice.save()
            qs = Choice.objects.filter(id=choice.id)
            return api_utils.response(request, qs, ChoiceGrid)
        else:
            return api_utils.response_error(request, form)
    elif request.method == 'PUT':
        api_utils.coerce_put_post(request)

        choice = get_object_or_404(Choice, id=choice_id)
        json_data = request.REQUEST.get('data', [])
        decoded_data = json.loads(json_data)
        if 'text' in decoded_data:
            choice.text = decoded_data['text']
        if 'order' in decoded_data:
            choice.order = int(decoded_data['order'])
        choice.save()
        return api_utils.success()
    elif request.method == 'DELETE':
        choice = get_object_or_404(Choice, id=choice_id)
        choice.delete()
        return api_utils.success()
    raise Http404
Ejemplo n.º 3
0
def surveys(request, survey_id=None):
    if request.method == 'GET':
        qs = Survey.objects.all()
        return api_utils.response(request, qs, SurveyGrid)
    elif request.method == 'POST':
        form = api_utils.load_form(request, SurveyForm)
        if form.is_valid():
            survey = form.save(commit=False)
            survey.created_by = request.user
            survey.editable_by = request.user
            survey.visible = True
            survey.public = False
            survey.allows_multiple_interviews = False
            survey.save()
            qs = Survey.objects.filter(id=survey.id)
            return api_utils.response(request, qs, SurveyGrid)
        else:
            return api_utils.response_error(request, form)
    elif request.method == 'DELETE':
        survey = get_object_or_404(Survey, id=survey_id)
        #survey.delete()
        return api_utils.success()
    raise Http404