Ejemplo n.º 1
0
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])
Ejemplo n.º 2
0
def process_choice(question, answer):
    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])
Ejemplo n.º 3
0
def add_answer(runinfo, question, answer_dict):
    """
    Add an Answer to a Question for RunInfo, given the relevant form input

    answer_dict contains the POST'd elements for this question, minus the
    question_{number} prefix.  The question_{number} form value is accessible
    with the ANSWER key.
    """
    answer = Answer()
    answer.question = question
    answer.subject = runinfo.subject
    answer.runid = runinfo.runid

    type = question.get_type()

    if "ANSWER" not in answer_dict:
        answer_dict['ANSWER'] = None

    if type in Processors:
        answer.answer = Processors[type](question, answer_dict) or ''
    else:
        raise AnswerException("No Processor defined for question type %s" %
                              type)

    # first, delete all existing answers to this question for this particular user+run
    delete_answer(question, runinfo.subject, runinfo.runid)

    # then save the new answer to the database
    answer.save(runinfo)

    return True
Ejemplo n.º 4
0
def process_multiple(question, answer):
    multiple = []
    multiple_freeform = []

    requiredcount = 0
    required = question.getcheckdict().get('required', 0)
    if required:
        try:
            requiredcount = int(required)
        except ValueError:
            requiredcount = 1
    if requiredcount and requiredcount > question.choices().count():
        requiredcount = question.choices().count()

    for k, v in answer.items():
        if k.startswith('multiple'):
            multiple.append(v)
        if k.startswith('more') and len(v.strip()) > 0:
            multiple_freeform.append(v)

    if len(multiple) + len(multiple_freeform) < requiredcount:
        raise AnswerException(ungettext(u"You must select at least %d option",
                                        u"You must select at least %d options",
                                        requiredcount) % requiredcount)
    multiple.sort()
    if multiple_freeform:
        multiple.append(multiple_freeform)
    return dumps(multiple)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
def process_simple(question, ansdict):
    checkdict = question.getcheckdict()
    ans = ansdict['ANSWER'] or ''
    qtype = question.get_type()
    if qtype.startswith('choice-yesno'):
        if ans not in ('yes', 'no', 'dontknow'):
            raise AnswerException(_(u'You must select an option'))
        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 and len(ansdict['comment']) > 0:
        return dumps([ans, [ansdict['comment']]])
    if ans:
        return dumps([ans])
    return dumps([])
Ejemplo n.º 7
0
def process_range_or_number(question, answer):
    cd = question.getcheckdict()

    rmin, rmax = parse_range(cd)
    rstep = parse_step(cd)

    convert = range_type(rmin, rmax, rstep)

    ans = answer['ANSWER']
    if not ans:
        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])
Ejemplo n.º 8
0
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"))