def _save_question(self, qset, data): current_count = Question.objects.filter(qset=qset).count() if qset.__class__ == Batch: QuestionForm = get_question_form(BatchQuestion) else: QuestionForm = get_question_form(Question) question_form = QuestionForm(qset, data=data) self.assertTrue(question_form.is_valid()) question = question_form.save() self.assertEquals( Question.objects.filter(qset=qset).count(), current_count + 1) return question
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)
def _save_question(self, qset, data): current_count = Question.objects.count() QuestionForm = get_question_form(BatchQuestion) question_form = QuestionForm(qset, data=data) self.assertTrue(question_form.is_valid()) question = question_form.save() self.assertEquals(Question.objects.count(), current_count + 1) return question
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
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 _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
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 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)
QuestionSet, ListingTemplate, Batch,\ Question, QuestionTemplate, QuestionOption, QuestionFlow, Answer from survey.utils.query_helper import get_filterset from survey.models import LocationType from survey.models import Interview from survey.forms.enumeration_area import LocationsFilterForm from survey.forms.question_set import get_question_set_form from survey.forms.question import get_question_form from survey.forms.filters import QuestionSetResultsFilterForm from survey.forms.filters import SurveyResultsFilterForm from survey.odk.utils.odk_helper import get_zipped_dir from survey.services.results_download_service import ResultsDownloadService from django.db.models import ProtectedError model = QuestionSet QuestionsForm = get_question_form(Question) class QuestionSetView(object): # users of this class methods needs to set their own bread crumbs model = QuestionSet questionSetForm = get_question_set_form(QuestionSet) def __init__(self, model_class=model, *args, **kwargs): if issubclass(model_class, QuestionSet): self.model = model_class self.questionSetForm = get_question_set_form( model_class) # create appropriate qset form else: raise HttpResponseNotAllowed('Illegal access')