Exemple #1
0
def new_subquestion(request, question_id, batch_id=None):
    parent_question = Question.objects.get(pk=question_id)
    questionform = QuestionForm(parent_question=parent_question)
    response = None
    if request.method == 'POST':
        questionform = QuestionForm(request.POST,
                                    parent_question=parent_question)
        response = __process_sub_question_form(request, questionform,
                                               parent_question, 'added',
                                               batch_id)
    context = {
        'questionform': questionform,
        'button_label': 'Create',
        'id': 'add-sub_question-form',
        'cancel_url': '/questions/',
        'parent_question': parent_question,
        'class': 'question-form',
        'heading': 'Add SubQuestion'
    }

    template_name = 'questions/new.html'
    if request.is_ajax():
        template_name = 'questions/_add_question.html'

    return response or render(request, template_name, context)
Exemple #2
0
def _process_question_form(request, batch, response, instance=None):
    question_form = QuestionForm(batch, data=request.POST, instance=instance)

    action_str = 'edit' if instance else 'add'
    if question_form.is_valid():
        question = question_form.save(**request.POST)
        if request.POST.has_key('add_to_lib_button'):
            qt = QuestionTemplate.objects.create(identifier=question.identifier,
                                            group=question.group,
                                            text=question.text,
                                            answer_type=question.answer_type,
                                            module=question.module)
            options = question.options.all()
            if options:
                topts = []
                for option in options:
                    topts.append(TemplateOption(question=qt, text=option.text, order=option.order))
                TemplateOption.objects.bulk_create(topts)
            messages.success(request, 'Question successfully %sed. to library' % action_str)
        messages.success(request, 'Question successfully %sed.' % action_str)
        response = HttpResponseRedirect(reverse('batch_questions_page', args=(batch.pk, )))
    else:
        messages.error(request, 'Question was not %sed.' % action_str)
#         options = dict(request.POST).get('options', None)
    return response, question_form
Exemple #3
0
def _save_subquestion(request, batch_id, instance=None):
    #possible subquestions are questions not bound to any interviewer yet
    batch = get_object_or_404(Batch, pk=batch_id)
    questionform = QuestionForm(batch, instance=instance)
    if request.method == 'POST':
        questionform = QuestionForm(batch, data=request.POST, instance=instance)
        if questionform.is_valid():
            if instance:
                zombify = False
            else:
                zombify = True
            question = questionform.save(zombie=zombify)
            if request.is_ajax():
                return HttpResponse(json.dumps({'id' : question.pk, 'text' : question.text,
                                                'identifier': question.identifier}), mimetype='application/json')
            messages.info(request, 'Sub Question saved')
    if instance:
        heading = 'Edit Subquestion'
    else:
        heading = 'New Subquestion'
    context = {'questionform': questionform, 'button_label': 'Create', 'id': 'add-sub_question-form',
               'save_url' : reverse('add_batch_subquestion_page', args=(batch.pk, )),
               'cancel_url': reverse('batch_questions_page', args=(batch.pk, )), 'class': 'question-form',
               'heading': heading}
    request.breadcrumbs([
        ('Surveys', reverse('survey_list_page')),
        (batch.survey.name, reverse('batch_index_page', args=(batch.survey.pk, ))),
        (batch.name, reverse('batch_questions_page', args=(batch.pk, ))),
    ])
    template_name = 'questions/new.html'
    if request.is_ajax():
        template_name = 'questions/_add_question.html'
        return render(request, template_name, context)
    else:
        return HttpResponseRedirect(reverse('batch_questions_page', args=(batch.pk, )))
Exemple #4
0
def _process_question_form(request, batch, response, instance=None):
    question_form = QuestionForm(batch, data=request.POST, instance=instance)

    action_str = 'edit' if instance else 'add'
    if question_form.is_valid():
        question = question_form.save(**request.POST)
        if request.POST.has_key('add_to_lib_button'):
            qt = QuestionTemplate.objects.create(identifier=question.identifier,
                                            group=question.group,
                                            text=question.text,
                                            answer_type=question.answer_type,
                                            module=question.module)
            options = question.options.all()
            if options:
                topts = []
                for option in options:
                    topts.append(TemplateOption(question=qt, text=option.text, order=option.order))
                TemplateOption.objects.bulk_create(topts)
            messages.success(request, 'Question successfully %sed. to library' % action_str)
        messages.success(request, 'Question successfully %sed.' % action_str)
        response = HttpResponseRedirect(reverse('batch_questions_page', args=(batch.pk, )))
    else:
        messages.error(request, 'Question was not %sed.' % action_str)
#         options = dict(request.POST).get('options', None)
    return response, question_form
def _render_question_view(request, batch, instance=None, prev_question=None):
    if instance is None and prev_question is None:
        prev_question = batch.last_question_inline()
    elif prev_question is None:
        try:
            prev_inlines = instance.previous_inlines()
            if prev_inlines:
                prev_question = prev_inlines[-1]
        except ValidationError:
            pass
    button_label = 'Create'
    options = None
    response = None
    QuestionForm = get_question_form(batch.question_model())
    if instance:
        button_label = 'Save'
        options = instance.options.all().order_by('order')
        # options = [option.text.strip()\
        #for option in options] if options else None
    if request.method == 'POST':
        question_form = QuestionForm(batch,
                                     data=request.POST,
                                     instance=instance,
                                     prev_question=prev_question)
        response, question_form = _process_question_form(
            request, batch, response, question_form)
    else:
        question_form = QuestionForm(batch,
                                     instance=instance,
                                     prev_question=prev_question)
    context = {
        'button_label': button_label,
        'id': 'add-question-form',
        'instance': instance,
        'request': request,
        'class': 'question-form',
        'USSD_MAX_CHARS': settings.USSD_MAX_CHARS,
        'batch': batch,
        'prev_question': prev_question,
        # 'prev_question': prev_question,
        'cancel_url': reverse('qset_questions_page', args=(batch.pk, )),
        'questionform': question_form,
        'response_validation_form': ResponseValidationForm(),
        'model_name': batch.__class__.__name__
    }

    if options:
        #options = filter(lambda text: text.strip(),
        #list(OrderedDict.fromkeys(options)))
        # options = map(lambda option: re.sub("[%s]" % \
        #settings.USSD_IGNORED_CHARACTERS, '', option), options)
        # map(lambda option: re.sub("  ", ' ', option), options)
        context['options'] = options
    breadcrumbs = Question.edit_breadcrumbs(qset=batch)
    if breadcrumbs:
        request.breadcrumbs(breadcrumbs)
    return response, context
Exemple #6
0
def _process_question_form(request, options, response, instance=None):
    question_form = QuestionForm(data=request.POST, instance=instance)
    action_str = 'edit' if instance else 'add'
    if question_form.is_valid():
        question_form.save(**request.POST)
        messages.success(request, 'Question successfully %sed.' % action_str)
        response = HttpResponseRedirect('/questions/')
    else:
        messages.error(request, 'Question was not %sed.' % action_str)
        options = dict(request.POST).get('options', None)
    return response, options, question_form
Exemple #7
0
def _process_question_form(request, options, response, instance=None):
    question_form = QuestionForm(data=request.POST, instance=instance)
    action_str = "edit" if instance else "add"
    if question_form.is_valid():
        question_form.save(**request.POST)
        messages.success(request, "Question successfully %sed." % action_str)
        response = HttpResponseRedirect("/questions/")
    else:
        messages.error(request, "Question was not %sed." % action_str)
        options = dict(request.POST).get("options", None)
    return response, options, question_form
Exemple #8
0
def add_logic(request, batch_id, question_id):
    question = Question.objects.get(id=question_id)
    batch = Batch.objects.get(id=batch_id)
    logic_form = LogicForm(question=question, batch=batch)
    response = None
    question_rules_for_batch = {}
    question_rules_for_batch[question] = question.rules_for_batch(batch)

    if request.method == "POST":
        logic_form = LogicForm(data=request.POST,
                               question=question,
                               batch=batch)
        if logic_form.is_valid():
            AnswerRule.objects.create(question=question,
                                      batch=batch,
                                      **_get_post_values(request.POST))
            messages.success(request, 'Logic successfully added.')
            response = HttpResponseRedirect('/batches/%s/questions/' %
                                            batch_id)

    context = {
        'logic_form': logic_form,
        'button_label': 'Save',
        'question': question,
        'rules_for_batch': question_rules_for_batch,
        'questionform': QuestionForm(parent_question=question),
        'modal_action': '/questions/%s/sub_questions/new/' % question.id,
        'class': 'question-form',
        'batch_id': batch_id,
        'batch': batch,
        'cancel_url': '/batches/%s/questions/' % batch_id
    }
    return response or render(request, "questions/logic.html", context)
Exemple #9
0
def _render_question_view(request, instance=None):
    question_form = QuestionForm(instance=instance)
    button_label = 'Create'
    options = None
    response = None
    if instance:
        button_label = 'Save'
        options = instance.options.all()
        options = [option.text for option in options] if options else None

    if request.method == 'POST':
        response, options, question_form = _process_question_form(
            request, options, response, instance)

    context = {
        'button_label': button_label,
        'id': 'add-question-form',
        'request': request,
        'class': 'question-form',
        'cancel_url': '/questions/',
        'questionform': question_form
    }

    if options:
        options = filter(lambda text: text.strip(),
                         list(OrderedDict.fromkeys(options)))
        options = map(
            lambda option: re.sub("[%s]" % Question.IGNORED_CHARACTERS, '',
                                  option), options)
        context['options'] = map(lambda option: re.sub("  ", ' ', option),
                                 options)

    return response, context
def add_logic(request, qset_id, question_id):
    question = Question.get(id=question_id)
    batch = QuestionSet.get(id=qset_id)
    QuestionForm = get_question_form(batch.question_model())
    response = None
    cancel_url = '../'
    logic_form = LogicForm(question)
    question_rules_for_batch = {}
    #     question_rules_for_batch[question] = question.rules_for_batch(batch)
    if request.method == "POST":
        logic_form = LogicForm(question, data=request.POST)
        if logic_form.is_valid():
            logic_form.save()
            messages.success(request, 'Logic successfully added.')
            response = HttpResponseRedirect(
                reverse('qset_questions_page', args=(batch.pk, )))
    breadcrumbs = Question.edit_breadcrumbs(qset=batch)
    if breadcrumbs:
        request.breadcrumbs(breadcrumbs)
        cancel_url = breadcrumbs[-1][1]
    context = {
        'logic_form': logic_form,
        'button_label': 'Save',
        'question': question,
        'USSD_MAX_CHARS': settings.USSD_MAX_CHARS,
        'rules_for_batch': question_rules_for_batch,
        'questionform': QuestionForm(batch, parent_question=question),
        'modal_action': reverse('add_qset_subquestion_page',
                                args=(batch.pk, )),
        'class': 'question-form',
        'batch_id': qset_id,
        'batch': batch,
        'cancel_url': cancel_url
    }
    return response or render(request, "set_questions/logic.html", context)
Exemple #11
0
def _render_question_view(request, batch, instance=None):
    question_form = QuestionForm(batch, instance=instance)
    button_label = 'Create'
    options = None
    response = None
    if instance:
        button_label = 'Save'
        options = instance.options.all().order_by('order')
        # import pdb; pdb.set_trace()
        # options = [option.text.strip() for option in options] if options else None

    if request.method == 'POST':
        response, question_form = _process_question_form(request, batch, response, instance)
    context = {'button_label': button_label,
               'id': 'add-question-form',
               'request': request,
               'class': 'question-form',
               'cancel_url': reverse('batch_questions_page', args=(batch.pk, )),
               'questionform': question_form}

    if options:
        #options = filter(lambda text: text.strip(), list(OrderedDict.fromkeys(options)))
        # options = map(lambda option: re.sub("[%s]" % settings.USSD_IGNORED_CHARACTERS, '', option), options)
        context['options'] = options #map(lambda option: re.sub("  ", ' ', option), options)
    request.breadcrumbs([
        ('Surveys', reverse('survey_list_page')),
        (batch.survey.name, reverse('batch_index_page', args=(batch.survey.pk, ))),
        (batch.name, reverse('batch_questions_page', args=(batch.pk, ))),
    ])
    return response, context
Exemple #12
0
def index(request, batch_id):
    batch = get_object_or_404(Batch, pk=batch_id)
    questions = batch.questions_inline()
    max_per_page = None
    if request.method == 'GET':
        question_filter_form = QuestionFilterForm(data=request.GET, batch=batch)
        batch_questions = batch.batch_questions.all()
        search_fields = ['identifier', 'group__name', 'text', ]
        if request.GET.has_key('q'):
            questions = get_filterset(batch_questions, request.GET['q'], search_fields)
        relevant_questions = question_filter_form.filter(batch_questions)
        questions = [q for q in questions if q in relevant_questions]
        #now maintain same inline other exclusing questions in
        max_per_page = _max_number_of_question_per_page(request.GET.get('number_of_questions_per_page', 0))
    else:
        question_filter_form = QuestionFilterForm(batch=batch)
    #question_library =  question_filter_form.filter(QuestionTemplate.objects.all())

    question_form = QuestionForm(batch)

    request.breadcrumbs([
        ('Surveys', reverse('survey_list_page')),
        (batch.survey.name, reverse('batch_index_page', args=(batch.survey.pk, ))),
    ])
    context = {'questions': questions, 'request': request, 'batch': batch, 'max_question_per_page':max_per_page,
               'question_filter_form': question_filter_form,
               'placeholder': 'identifier, group name, text',
               }
    return render(request, 'questions/index.html', context)
Exemple #13
0
def add_logic(request, batch_id, question_id):
    question = Question.objects.get(id=question_id)
    batch = Batch.objects.get(id=batch_id)
    response = None
    logic_form = LogicForm(question=question)
    question_rules_for_batch = {}
#     question_rules_for_batch[question] = question.rules_for_batch(batch)
    if request.method == "POST":
        logic_form = LogicForm(data=request.POST, question=question)
        if logic_form.is_valid():
            logic_form.save()
            messages.success(request, 'Logic successfully added.')
            response = HttpResponseRedirect('/batches/%s/questions/' % batch_id)
    request.breadcrumbs([
        ('Surveys', reverse('survey_list_page')),
        (batch.survey.name, reverse('batch_index_page', args=(batch.survey.pk, ))),
        (batch.name, reverse('batch_questions_page', args=(batch.pk, ))),
    ])

    context = {'logic_form': logic_form, 'button_label': 'Save', 'question': question,
               'rules_for_batch': question_rules_for_batch,
               'questionform': QuestionForm(parent_question=question, batch=batch),
               'modal_action': reverse('add_batch_subquestion_page', args=(batch.pk, )),
               'class': 'question-form', 'batch_id': batch_id, 'batch': batch,
               'cancel_url': '/batches/%s/questions/' % batch_id}
    return response or render(request, "questions/logic.html", context)
Exemple #14
0
def _save_subquestion(request, batch_id, instance=None):
    #possible subquestions are questions not bound to any interviewer yet
    batch = get_object_or_404(Batch, pk=batch_id)
    questionform = QuestionForm(batch, instance=instance)
    if request.method == 'POST':
        questionform = QuestionForm(batch, data=request.POST, instance=instance)
        if questionform.is_valid():
            if instance:
                zombify = False
            else:
                zombify = True
            question = questionform.save(zombie=zombify)
            if request.is_ajax():
                return HttpResponse(json.dumps({'id' : question.pk, 'text' : question.text,
                                                'identifier': question.identifier}), content_type='application/json')
            messages.info(request, 'Sub Question saved')
    if instance:
        heading = 'Edit Subquestion'
    else:
        heading = 'New Subquestion'
    context = {'questionform': questionform, 'button_label': 'Create', 'id': 'add-sub_question-form',
               'save_url' : reverse('add_batch_subquestion_page', args=(batch.pk, )),
               'cancel_url': reverse('batch_questions_page', args=(batch.pk, )), 'class': 'question-form',
               'heading': heading}
    request.breadcrumbs([
        ('Surveys', reverse('survey_list_page')),
        (batch.survey.name, reverse('batch_index_page', args=(batch.survey.pk, ))),
        (batch.name, reverse('batch_questions_page', args=(batch.pk, ))),
    ])
    template_name = 'questions/new.html'
    if request.is_ajax():
        template_name = 'questions/_add_question.html'
        return render(request, template_name, context)
    else:
        return HttpResponseRedirect(reverse('batch_questions_page', args=(batch.pk, )))
Exemple #15
0
def edit_subquestion(request, question_id, batch_id=None):
    question = Question.objects.get(pk=question_id)
    questionform = QuestionForm(instance=question)
    response = None
    if request.method == 'POST':
        questionform = QuestionForm(request.POST, instance=question)
        response = __process_sub_question_form(request, questionform,
                                               question.parent, 'edited',
                                               batch_id)
    context = {
        'questionform': questionform,
        'button_label': 'Save',
        'id': 'add-sub_question-form',
        'cancel_url': '/questions/',
        'parent_question': question.parent,
        'class': 'question-form',
        'heading': 'Edit Subquestion'
    }

    template_name = 'questions/new.html'

    return response or render(request, template_name, context)
Exemple #16
0
def _index(request, batch_id):
    batch = get_object_or_404(Batch, pk=batch_id)
    data = dict(request.GET)
    question_filter_form = QuestionFilterForm(data=data, batch=batch)
    question_library =  question_filter_form.filter(QuestionTemplate.objects.all())
    question_form = QuestionForm(batch)
    question_flow_form = None#QuestionFlowForm()
    question_tree = None
    if batch.start_question:
        question_tree = batch.batch_questions.all()
    context = {'batch': batch, 'batch_question_tree' : question_tree, 'question_form' : question_form, 'button_label' : 'Add',
               'id' : 'question_form', 'action': '#',
               'question_library' : question_library, 'question_filter_form' : question_filter_form,
               'question_flow_form' : question_flow_form}
    return render(request, 'questions/batch_question.html', context)
def _save_subquestion(request, batch_id, instance=None):
    # possible subquestions are questions not bound to any interviewer yet
    batch = QuestionSet.get(pk=batch_id)
    QuestionForm = get_question_form(batch.question_model())
    questionform = QuestionForm(batch, instance=instance)
    if request.method == 'POST':
        questionform = QuestionForm(batch,
                                    data=request.POST,
                                    instance=instance)
        if questionform.is_valid():
            if instance:
                zombify = False
            else:
                zombify = True
            question = questionform.save(zombie=zombify)
            if request.is_ajax():
                return HttpResponse(json.dumps({
                    'id':
                    question.pk,
                    'text':
                    question.text,
                    'identifier':
                    question.identifier
                }),
                                    content_type='application/json')
            messages.info(request, 'Sub Question saved')
    if instance:
        heading = 'Edit Subquestion'
    else:
        heading = 'New Subquestion'
    context = {
        'questionform': questionform,
        'button_label': 'Create',
        'id': 'add-sub_question-form',
        'USSD_MAX_CHARS': settings.USSD_MAX_CHARS,
        'save_url': reverse('%s_home' % batch.resolve_tag()),
        'cancel_url': reverse('qset_questions_page', args=(batch.pk, )),
        'class': 'question-form',
        'heading': heading
    }
    breadcrumbs = Question.edit_breadcrumbs(qset=batch)
    if breadcrumbs:
        request.breadcrumbs(breadcrumbs)
    template_name = 'set_questions/new.html'
    if request.is_ajax():
        template_name = 'set_questions/_add_question.html'
        return render(request, template_name, context)
    else:
        return HttpResponseRedirect(
            reverse('qset_questions_page', args=(batch.pk, )))
def assign(request, qset_id):
    batch = QuestionSet.get(id=qset_id)
    if batch.interviews.count():
        error_message = "Questions cannot be assigned \
            interviews has already been conducted: %s."                                                        % \
                        batch.name.capitalize()
        messages.error(request, error_message)
        return HttpResponseRedirect(
            reverse('qset_questions_page', args=(batch.pk, )))
    if request.method == 'POST':
        data = dict(request.POST)
        last_question = batch.last_question_inline()
        lib_questions = QuestionTemplate.objects.filter(
            identifier__in=data.get('identifier', ''))
        if lib_questions:
            for lib_question in lib_questions:
                question = Question.objects.create(
                    identifier=lib_question.identifier,
                    text=lib_question.text,
                    answer_type=lib_question.answer_type,
                    qset=batch,
                )
                # assign the options
                for option in lib_question.options.all():
                    QuestionOption.objects.create(question=question,
                                                  text=option.text,
                                                  order=option.order)
                if last_question:
                    QuestionFlow.objects.create(question=last_question,
                                                next_question=question)
                else:
                    batch.start_question = question
                    batch.save()
                last_question = question
            #batch_questions_form = BatchQuestionsForm(batch=batch,\
            #\data=request.POST, instance=batch)
        success_message = "Questions successfully assigned to %s: %s." % (
            batch.verbose_name(), batch.name.capitalize())
        messages.success(request, success_message)
        return HttpResponseRedirect(
            reverse('qset_questions_page', args=(batch.pk, )))
    used_identifiers = [
        question.identifier for question in batch.questions.all()
    ]
    library_questions = QuestionTemplate.objects.exclude(
        identifier__in=used_identifiers).order_by('identifier')
    question_filter_form = QuestionFilterForm()
    #     library_questions =  question_filter_form.filter(library_questions)
    breadcrumbs = Question.edit_breadcrumbs(qset=batch)
    page_name = ''
    if breadcrumbs:
        if breadcrumbs[0][0] == 'Listing Form':
            page_name = 'Listing'
        else:
            page_name = 'Batch'
        request.breadcrumbs(breadcrumbs)
    context = {
        'batch_questions_form': QuestionForm(batch),
        'batch': batch,
        'button_label': 'Save',
        'id': 'assign-question-to-batch-form',
        'library_questions': library_questions,
        'question_filter_form': question_filter_form,
        'page_name': page_name,
        'redirect_url': '/qsets/%s/questions/' % qset_id
    }
    return render(request, 'set_questions/assign.html', context)