def question_choice(request, question): choices = [] jstriggers = [] cd = question.getcheckdict() key = "question_%s" % question.number key2 = "question_%s_comment" % question.number val = None possibledbvalue = get_value_for_run_question(get_runid_from_request(request), question.id) if key in request.POST: val = request.POST[key] elif not possibledbvalue == None: valueaslist = ast.literal_eval(possibledbvalue) val = valueaslist[0] else: if 'default' in cd: val = cd['default'] for choice in question.choices(): choices.append( ( choice.value == val, choice, ) ) if question.type == 'choice-freeform': jstriggers.append('%s_comment' % question.number) return { 'choices' : choices, 'sel_entry' : val == '_entry_', 'qvalue' : val or '', 'required' : True, 'comment' : request.POST.get(key2, ""), 'jstriggers': jstriggers, }
def question_choice(request, question): choices = [] jstriggers = [] cd = question.getcheckdict() key = "question_%s" % question.number key2 = "question_%s_comment" % question.number val = None possibledbvalue = get_value_for_run_question( get_runid_from_request(request), question.id) if key in request.POST: val = request.POST[key] elif not possibledbvalue == None: valueaslist = ast.literal_eval(possibledbvalue) val = valueaslist[0] else: if 'default' in cd: val = cd['default'] for choice in question.choices(): choices.append(( choice.value == val, choice, )) if question.type == 'choice-freeform': jstriggers.append('%s_comment' % question.number) return { 'choices': choices, 'sel_entry': val == '_entry_', 'qvalue': val or '', 'required': True, 'comment': request.POST.get(key2, ""), 'jstriggers': jstriggers, }
def question_range_or_number(request, question): cd = question.getcheckdict() rmin, rmax = parse_range(cd) rstep = parse_step(cd) runit = cd.get('unit', '') #try loading current from database before just setting to min possibledbvalue = get_value_for_run_question(get_runid_from_request(request), question.id) #you can't eval none nor can you eval empty if not possibledbvalue == None and len(possibledbvalue) > 0: valueaslist = ast.literal_eval(possibledbvalue) current = valueaslist[0] else: current = request.POST.get('question_%s' % question.number, rmin) jsinclude = [] if question.type == 'range': jsinclude = [settings.STATIC_URL+'range.js'] return { 'required' : True, 'type': question.type, 'rmin' : rmin, 'rmax' : rmax, 'rstep' : rstep, 'runit' : runit, 'current' : current, 'jsinclude' : jsinclude }
def question_yesno(request, question): key = "question_%s" % question.number key2 = "question_%s_comment" % question.number val = request.POST.get(key, None) cmt = request.POST.get(key2, '') qtype = question.get_type() cd = question.getcheckdict() jstriggers = [] if qtype == 'choice-yesnocomment': hascomment = True else: hascomment = False if qtype == 'choice-yesnodontknow' or 'dontknow' in cd: hasdontknow = True else: hasdontknow = False #try the database before reverting to default possiblevalue = get_value_for_run_question(get_runid_from_request(request), question.id) if not possiblevalue == None: #save process always listifies the answer so we unlistify it to put it back in the field valueaslist = ast.literal_eval(possiblevalue) if len(valueaslist) > 0: val = valueaslist[0] if not val: if cd.get('default', None): val = cd['default'] checks = '' if hascomment: if cd.get('required-yes'): jstriggers = ['%s_comment' % question.number] checks = ' checks="dep_check(\'%s,yes\')"' % question.number elif cd.get('required-no'): checks = ' checks="dep_check(\'%s,no\')"' % question.number elif cd.get('required-dontknow'): checks = ' checks="dep_check(\'%s,dontknow\')"' % question.number return { 'required': True, 'checks': checks, 'value': val, 'qvalue': val, 'hascomment': hascomment, 'hasdontknow': hasdontknow, 'comment': cmt, 'jstriggers': jstriggers, 'template': 'questionnaire/choice-yesnocomment.html', }
def question_open(request, question): key = "question_%s" % question.number value = question.getcheckdict().get('default', '') if key in request.POST: value = request.POST[key] else: #also try to get it from the database so we can handle back/forward in which post has been cleared possiblevalue = get_value_for_run_question(get_runid_from_request(request), question.id) if not possiblevalue == None: #save process always listifies the answer so we unlistify it to put it back in the field valueaslist = ast.literal_eval(possiblevalue) if len(valueaslist) > 0: value = valueaslist[0] return { 'required': question.getcheckdict().get('required', False), 'value': value, }
def question_multiple(request, question): key = "question_%s" % question.number choices = [] jstriggers = [] counter = 0 qvalues = [] cd = question.getcheckdict() defaults = cd.get('default', '').split(',') possibledbvalue = get_value_for_run_question( get_runid_from_request(request), question.id) possiblelist = [] if not possibledbvalue == None: possiblelist = ast.literal_eval(possibledbvalue) prev_vals = {} if question.type == 'choice-multiple-values': pl = [] for choice_value, prev_value in possiblelist: pl.append(choice_value) prev_vals[choice_value] = str(prev_value) possiblelist = pl # print 'possible value is ', possibledbvalue, ', possiblelist is ', possiblelist for choice in question.choices(): counter += 1 key = "question_%s_multiple_%d" % (question.number, choice.sortid) if question.type == "choice-multiple-values": jstriggers.append("q%s_%s_box" % (question.number, choice.value)) # so that the number box will be activated when item is checked #try database first and only after that fall back to post choices # print 'choice multiple checking for match for choice ', choice checked = ' checked' prev_value = '' qvalue = "%s_%s" % (question.number, choice.value) if key in request.POST or \ (request.method == 'GET' and choice.value in defaults): qvalues.append(qvalue) value_key = "question_%s_%s_value" % (question.number, choice.value) if value_key in request.POST: prev_value = request.POST[value_key] elif choice.value in possiblelist: qvalues.append(qvalue) # so that this choice being checked will trigger anything that depends on it - # for choice-multiple-values right now if choice.value in prev_vals.keys(): prev_value = prev_vals[choice.value] else: checked = '' # bug: you can have one item checked from database and another from POST data choices.append(( choice, key, checked, prev_value, )) extracount = int(cd.get('extracount', 0)) if not extracount and question.type == 'choice-multiple-freeform': extracount = 1 extras = [] for x in range(1, extracount + 1): key = "question_%s_more%d" % (question.number, x) if key in request.POST: extras.append(( key, request.POST[key], )) else: extras.append(( key, '', )) # right now does not retrieve extra fields from database return { "choices": choices, "extras": extras, "type": question.type, "template": "questionnaire/choice-multiple-freeform.html", "required": cd.get("required", False) and cd.get("required") != "0", "jstriggers": jstriggers, "qvalues": qvalues, "placeholder": cd.get('placeholder', ''), }
def question_multiple(request, question): key = "question_%s" % question.number choices = [] jstriggers = [] counter = 0 qvalues = [] cd = question.getcheckdict() defaults = cd.get('default','').split(',') possibledbvalue = get_value_for_run_question(get_runid_from_request(request), question.id) possiblelist = [] if not possibledbvalue == None: possiblelist = ast.literal_eval(possibledbvalue) prev_vals = {} if question.type == 'choice-multiple-values': pl = [] for choice_value, prev_value in possiblelist: pl.append(choice_value) prev_vals[choice_value] = str(prev_value) possiblelist = pl # print 'possible value is ', possibledbvalue, ', possiblelist is ', possiblelist for choice in question.choices(): counter += 1 key = "question_%s_multiple_%d" % (question.number, choice.sortid) if question.type == "choice-multiple-values": jstriggers.append("q%s_%s_box" % (question.number, choice.value)) # so that the number box will be activated when item is checked #try database first and only after that fall back to post choices # print 'choice multiple checking for match for choice ', choice checked = ' checked' prev_value = '' qvalue = "%s_%s" % (question.number, choice.value) if key in request.POST or \ (request.method == 'GET' and choice.value in defaults): qvalues.append(qvalue) value_key = "question_%s_%s_value" % (question.number, choice.value) if value_key in request.POST: prev_value = request.POST[value_key] elif choice.value in possiblelist: qvalues.append(qvalue) # so that this choice being checked will trigger anything that depends on it - # for choice-multiple-values right now if choice.value in prev_vals.keys(): prev_value = prev_vals[choice.value] else: checked = '' # bug: you can have one item checked from database and another from POST data choices.append( (choice, key, checked, prev_value,) ) extracount = int(cd.get('extracount', 0)) if not extracount and question.type == 'choice-multiple-freeform': extracount = 1 extras = [] for x in range(1, extracount+1): key = "question_%s_more%d" % (question.number, x) if key in request.POST: extras.append( (key, request.POST[key],) ) else: extras.append( (key, '',) ) # right now does not retrieve extra fields from database return { "choices": choices, "extras": extras, "type": question.type, "template" : "questionnaire/choice-multiple-freeform.html", "required" : cd.get("required", False) and cd.get("required") != "0", "jstriggers": jstriggers, "qvalues": qvalues }