from questionnaire import add_type from questionnaire import answer_proc from questionnaire import AnswerException from questionnaire import Processors from questionnaire import question_proc from questionnaire import QuestionProcessors @question_proc('custom') def question_custom(request, question): cd = question.getcheckdict() _type = cd['type'] d = {} if _type in QuestionProcessors: d = QuestionProcessors[_type](request, question) if 'template' not in d: d['template'] = 'questionnaire/%s.html' % _type return d @answer_proc('custom') def process_custom(question, answer): cd = question.getcheckdict() _type = cd['type'] if _type in Processors: return Processors[_type](question, answer) raise AnswerException(_(u"Processor not defined for this question")) add_type('custom', 'Custom field')
"required": "required" in cd, "timeperiods": timeperiods, "value": value, } @answer_proc('timeperiod') def process_timeperiod(question, answer): if not answer['ANSWER'] or 'unit' not in answer: raise AnswerException(_(u"Invalid time period")) period = answer['ANSWER'].strip() if period: try: period = str(int(period)) except ValueError: raise AnswerException(_(u"Time period must be a whole number")) unit = answer['unit'] checkdict = question.getcheckdict() if checkdict and 'units' in checkdict: units = checkdict['units'].split(',') else: units = ('day', 'hour', 'week', 'month', 'year') if not period and "required" in checkdict: raise AnswerException(_(u'Field cannot be blank')) if unit not in units: raise AnswerException(_(u"Invalid time period")) return "%s; %s" % (period, unit) add_type('timeperiod', 'Time Period [input, select]')
if qtype == "choice-yesnocomment" and len(ansdict.get("comment", "").strip()) == 0: if checkdict.get("required", False): raise AnswerException(_(u"Field cannot be blank")) if checkdict.get("required-yes", False) and ans == "yes": raise AnswerException(_(u"Field cannot be blank")) if checkdict.get("required-no", False) and ans == "no": raise AnswerException(_(u"Field cannot be blank")) else: if not ans.strip() and checkdict.get("required", False): raise AnswerException(_(u"Field cannot be blank")) if "comment" in ansdict.keys() and len(ansdict["comment"]) > 0: return dumps([ans, [ansdict["comment"]]]) if ans: return dumps([ans]) return dumps([]) add_type("open", "Open Answer, single line [input]") add_type("open-textfield", "Open Answer, multi-line [textarea]") add_type("choice-yesno", "Yes/No Choice [radio]") add_type("choice-yesnocomment", "Yes/No Choice with optional comment [radio, input]") add_type("choice-yesnodontknow", "Yes/No/Don't know Choice [radio]") @answer_proc("comment") def process_comment(question, answer): pass add_type("comment", "Comment Only")
opt = answer['ANSWER'] or '' if not opt: raise AnswerException(_(u'You must select an option')) if opt == '_entry_' and question.type == 'choice-freeform': opt = answer.get('comment', '') if not opt: raise AnswerException(_(u'Field cannot be blank')) return dumps([[opt]]) else: valid = [c.value for c in question.choices()] if opt not in valid: raise AnswerException(_(u'Invalid option!')) return dumps([opt]) add_type('choice', 'Choice [radio]') add_type('choice-freeform', 'Choice with a freeform option [radio]') @question_proc('choice-multiple', 'choice-multiple-freeform') def question_multiple(request, question): key = "question_%s" % question.number choices = [] counter = 0 from questionnaire.models import Answer cd = question.getcheckdict() defaults = cd.get('default', '').split(',') for choice in question.choices(): counter += 1
if checkdict.get('required', False): raise AnswerException(_(u'Field cannot be blank')) if checkdict.get('required-yes', False) and ans == 'yes': raise AnswerException(_(u'Field cannot be blank')) if checkdict.get('required-no', False) and ans == 'no': raise AnswerException(_(u'Field cannot be blank')) else: if not ans.strip() and checkdict.get('required', False): raise AnswerException(_(u'Field cannot be blank')) if 'comment' in ansdict and len(ansdict['comment']) > 0: return dumps([ans, [ansdict['comment']]]) if ans: return dumps([ans]) return dumps([]) add_type('open', 'Open Answer, single line [input]') add_type('open-textfield', 'Open Answer, multi-line [textarea]') add_type('choice-yesno', 'Yes/No Choice [radio]') add_type('choice-yesnocomment', 'Yes/No Choice with optional comment [radio, input]') add_type('choice-yesnodontknow', 'Yes/No/Don\'t know Choice [radio]') @answer_proc('comment') def process_comment(question, answer): pass add_type('comment', 'Comment Only')
raise AnswerException(_(u"Field cannot be blank")) else: return [] try: ans = convert(ans) except: raise AnswerException(_(u"Could not convert the number")) if ans > convert(rmax) or ans < convert(rmin): raise AnswerException(_(u"Out of range")) return dumps([ans]) add_type('range', 'Range of numbers [select]') add_type('number', 'Number [input]') def parse_range(checkdict): """ Given a checkdict for a range widget return the min and max string values. """ Range = checkdict.get('range', '1-5') try: rmin, rmax = Range.split('-', 1) except ValueError: rmin, rmax = '1', '5'
'qvalue' : val or '', 'required' : True, 'comment' : request.POST.get(key2, ""), 'jstriggers': jstriggers, } @answer_proc('choice-select') def process_choice(question, answer): opt = answer['ANSWER'] or '' if not opt: raise AnswerException(_(u'You must select an option')) valid = [c.value for c in question.choices()] if opt not in valid: raise AnswerException(_(u'Invalid option!')) return dumps([opt]) add_type('choice-select', 'Choice [select]') @question_proc('date-select') def question_date(request, question): key = "question_%s" % question.number value = question.getcheckdict().get('default','') if key in request.POST: value = request.POST[key] return { 'required' : question.getcheckdict().get('required', False), 'value' : value, } @answer_proc('date-select') def process_date(question, answer):
if question.is_required(): raise AnswerException(_(u"Field cannot be blank")) else: return [] try: ans = convert(ans) except: raise AnswerException(_(u"Could not convert the number")) if ans > convert(rmax) or ans < convert(rmin): raise AnswerException(_(u"Out of range")) return dumps([ans]) add_type('range', 'Range of numbers [select]') add_type('number', 'Number [input]') def parse_range(checkdict): """Given a checkdict for a range widget return the min and max string values. """ Range = checkdict.get('range', '1-5') try: rmin, rmax = Range.split('-', 1) except ValueError: rmin, rmax = '1', '5'
opt = answer['ANSWER'] or '' if not opt: raise AnswerException(_(u'You must select an option')) if opt == '_entry_' and question.type == 'choice-freeform': opt = answer.get('comment', '') if not opt: raise AnswerException(_(u'Field cannot be blank')) return dumps([[opt]]) else: valid = [c.value for c in question.choices()] if opt not in valid: raise AnswerException(_(u'Invalid option!')) return dumps([opt]) add_type('choice', 'Choice [radio]') add_type('choice-freeform', 'Choice with a freeform option [radio]') add_type('dropdown', 'Dropdown choice [select]') @question_proc('choice-multiple', 'choice-multiple-freeform') def question_multiple(request, question): key = "question_%s" % question.number choices = [] counter = 0 cd = question.getcheckdict() defaults = cd.get('default', '').split(',') for choice in question.choices(): counter += 1 key = "question_%s_multiple_%d" % (question.number, choice.sortid) if key in request.POST or \