def index(request, qset_id): # now I'm gonna call question set a batch of questions.\ #If there's time, I'll rename them properly # So don't get confused :) try: batch = QuestionSet.get(pk=qset_id) except QuestionSet.DoesNotExist: raise Http404("No QuestionSet Model matches the given query.") questions = batch.questions_inline() request_data = request.GET if request.method == 'GET' else request.POST question_filter_form = QuestionFilterForm(data=request_data, qset=batch) search_fields = ['identifier', 'text', ] qset_questions = batch.questions.all() if 'q' in request_data: questions = get_filterset(qset_questions, request_data['q'], search_fields) if 'question_types' in request_data: relevant_ids = list(question_filter_form.filter( qset_questions).values_list('id', flat=True)) questions = [q for q in questions if q.id in relevant_ids] # now maintain same inline other exclusing questions in breadcrumbs = Question.index_breadcrumbs(qset=batch) if breadcrumbs: request.breadcrumbs(breadcrumbs) context = {'questions': questions, 'request': request, 'batch': batch, 'question_filter_form': question_filter_form, 'placeholder': 'identifier, text', 'template_file': 'interviews/answer.html', 'is_preview': True # caution atleast on ODK access # at least on access must exist } return render(request, 'set_questions/index.html', context)
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)
def filter(request): question_lib = QuestionTemplate.objects.all() search_fields = ['identifier', 'text', ] if request.GET.has_key('q'): question_lib = get_filterset(question_lib, request.GET['q'], search_fields) question_filter_form = QuestionFilterForm(data=request.GET) questions = question_filter_form.filter(question_lib).values('id', 'text', 'answer_type', 'group', 'module').order_by('text') json_dump = json.dumps(list(questions), cls=DjangoJSONEncoder) return HttpResponse(json_dump, mimetype='application/json')
def index(request): ''' show all library questions ''' question_filter_form = QuestionFilterForm(data=request.GET or None) questions = question_filter_form.filter(QuestionTemplate.objects.all()) search_fields = ['identifier', 'group__name', 'text', ] if request.GET.has_key('q'): questions = get_filterset(questions, request.GET['q'], search_fields) context = {'questions': questions, 'request': request, 'placeholder': 'identifier, group name, text', 'question_filter_form' : question_filter_form} return render(request, 'question_templates/index.html', context)
def filter(request): question_lib = QuestionTemplate.objects.all() search_fields = [ 'identifier', 'text', ] if 'q' in request.GET: question_lib = get_filterset(question_lib, request.GET['q'], search_fields) question_filter_form = QuestionFilterForm(data=request.GET) questions = question_filter_form.filter(question_lib).values( 'id', 'text', 'answer_type', 'module').order_by('text') json_dump = json.dumps(list(questions), cls=DjangoJSONEncoder) return HttpResponse(json_dump, content_type='application/json')
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 index(request, batch_id): batch = Batch.objects.get(id=batch_id) if batch_id else None question_filter_form = QuestionFilterForm(data=request.GET) questions, max_per_page = _questions_given(batch_id, request) if not questions: messages.error( request, 'There are no questions associated with this batch yet.') question_rules_for_batch = {} if batch: for question in questions: question_rules_for_batch[question] = question.rules_for_batch( batch) context = { 'questions': questions, 'request': request, 'batch': batch, 'max_question_per_page': max_per_page, 'question_filter_form': question_filter_form, 'rules_for_batch': question_rules_for_batch } return render(request, 'questions/index.html', context)
def index(request, model_class=QuestionTemplate): ''' show all library questions ''' question_filter_form = QuestionFilterForm(data=request.GET or None) questions = question_filter_form.filter(model_class.objects.all()) search_fields = ['identifier', 'text', ] if 'q' in request.GET: questions = get_filterset(questions, request.GET['q'], search_fields) context = { 'questions': questions, 'request': request, 'placeholder': 'identifier, text', 'question_filter_form': question_filter_form, 'model_class': model_class} if model_class == ParameterTemplate: request.breadcrumbs([ ('Groups', reverse('respondent_groups_page')), ]) return render(request, 'question_templates/index.html', context)
def test_form_instance_should_have_all_question_types(self): question_filter_form = QuestionFilterForm() all_question_types = [('Numerical Answer', 'Numerical Answer'), ('Text Answer', 'Text Answer'), ('Multi Choice Answer', 'Multi Choice Answer')] [ self.assertIn( question_type, question_filter_form.fields['question_types'].choices) for question_type in all_question_types ]
def index(request): ''' show all library questions ''' question_filter_form = QuestionFilterForm(data=request.GET or None) questions = question_filter_form.filter(QuestionTemplate.objects.all()) search_fields = [ 'identifier', 'group__name', 'text', ] if request.GET.has_key('q'): questions = get_filterset(questions, request.GET['q'], search_fields) context = { 'questions': questions, 'request': request, 'placeholder': 'identifier, group name, text', 'question_filter_form': question_filter_form } return render(request, 'question_templates/index.html', context)
def index(request, qset_id): # now I'm gonna call question set a batch of questions.\ #If there's time, I'll rename them properly # So don't get confused :) try: batch = QuestionSet.get(pk=qset_id) except QuestionSet.DoesNotExist: raise Http404("No QuestionSet Model matches the given query.") questions = batch.questions_inline() request_data = request.GET if request.method == 'GET' else request.POST question_filter_form = QuestionFilterForm(data=request_data, qset=batch) search_fields = [ 'identifier', 'text', ] qset_questions = batch.questions.all() if 'q' in request_data: questions = get_filterset(qset_questions, request_data['q'], search_fields) if 'question_types' in request_data: relevant_ids = list( question_filter_form.filter(qset_questions).values_list('id', flat=True)) questions = [q for q in questions if q.id in relevant_ids] # now maintain same inline other exclusing questions in breadcrumbs = Question.index_breadcrumbs(qset=batch) if breadcrumbs: request.breadcrumbs(breadcrumbs) context = { 'questions': questions, 'request': request, 'batch': batch, 'question_filter_form': question_filter_form, 'placeholder': 'identifier, text', 'template_file': 'interviews/answer.html', 'is_preview': True # caution atleast on ODK access # at least on access must exist } return render(request, 'set_questions/index.html', context)
def test_form_instance_should_have_all_modules(self): module_1 = QuestionModule.objects.create(name="Module 1") module_2 = QuestionModule.objects.create(name="Module 2") module_3 = QuestionModule.objects.create(name="Module 2") question_filter_form = QuestionFilterForm() self.assertIn((module_1.id, module_1.name), question_filter_form.fields['modules'].choices) self.assertIn((module_2.id, module_2.name), question_filter_form.fields['modules'].choices) self.assertIn((module_3.id, module_3.name), question_filter_form.fields['modules'].choices)
def test_form_instance_should_have_all_question_types(self): question_filter_form = QuestionFilterForm() all_question_types = [('number', 'Number'), ('text', 'Text'), ('multichoice', 'Multichoice')] [ self.assertIn( question_type, question_filter_form.fields['question_types'].choices) for question_type in all_question_types ]
def index(request, model_class=QuestionTemplate): """show all library questions""" question_filter_form = QuestionFilterForm(data=request.GET or None) questions = question_filter_form.filter(model_class.objects.all()) search_fields = [ 'identifier', 'text', ] if 'q' in request.GET: questions = get_filterset(questions, request.GET['q'], search_fields) context = { 'questions': questions, 'request': request, 'placeholder': 'identifier, text', 'question_filter_form': question_filter_form, 'model_class': model_class } if model_class == ParameterTemplate: request.breadcrumbs([ ('Groups', reverse('respondent_groups_page')), ]) return render(request, 'question_templates/index.html', context)
def list_all_questions(request): batch_id = request.GET.get('batch_id', None) question_filter_form = QuestionFilterForm(data=request.GET) questions, max_per_page = _questions_given(batch_id, request) context = { 'questions': questions, 'request': request, 'question_filter_form': question_filter_form, 'rules_for_batch': {}, 'max_question_per_page': max_per_page } return render(request, 'questions/index.html', context)
def test_form_instance_should_have_all_groups(self): group_1 = HouseholdMemberGroup.objects.create(name="Group 1", order=1) group_2 = HouseholdMemberGroup.objects.create(name="Group 2", order=2) group_3 = HouseholdMemberGroup.objects.create(name="Group 3", order=3) question_filter_form = QuestionFilterForm() all_groups = [group_1, group_2, group_3] [ self.assertIn((group.id, group.name), question_filter_form.fields['groups'].choices) for group in all_groups ]
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': unicode(BatchQuestionsForm()), '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)
def assign(request, batch_id): batch = Batch.objects.get(id=batch_id) if batch.is_open(): error_message = "Questions cannot be assigned to open batch: %s." % batch.name.capitalize( ) messages.error(request, error_message) return HttpResponseRedirect("/batches/%s/questions/" % batch_id) batch_questions_form = BatchQuestionsForm(batch=batch) batch = Batch.objects.get(id=batch_id) groups = HouseholdMemberGroup.objects.all() groups.exists() modules = QuestionModule.objects.all() modules.exists() 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, group=lib_question.group, module=lib_question.module, batch=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 batch: %s." % batch.name.capitalize( ) messages.success(request, success_message) return HttpResponseRedirect( reverse("batch_questions_page", args=(batch_id, ))) all_modules = QuestionModule.objects.all() used_identifiers = [ question.identifier for question in batch.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) 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 = { 'batch_questions_form': unicode(batch_questions_form), 'batch': batch, 'button_label': 'Save', 'id': 'assign-question-to-batch-form', 'groups': groups, 'library_questions': library_questions, 'question_filter_form': question_filter_form, 'modules': all_modules } return render(request, 'batches/assign.html', context)