def validator(ptype, required): """ Get the right validator for the ptype given """ if type2validator.get(ptype, False): if required: return twc.IntValidator(required=True) return twc.IntValidator() elif required : if wordlist.is_of_type(ptype, wordlist.FILE): if required : return twf.FileValidator(required=True) return twf.FileValidator() return twc.Validator(required=True) return None
def prefill_fields(form_parameters, form, prefill_params, kw, replace_value=True): """ If prefill key is in the request, we must pre-fill some fields and perhaps change their definition :param form_parameters: the 'in' parameters of the form :param form: the 'form widget' :param kw: the parameters in the request :return: """ modified = [] # list of modified fields prefill = json.loads(prefill_params) debug('PREFILL %s' % prefill_params) for type_to_prefill, prefill_with in prefill.iteritems(): debug('Trying type %s' % type_to_prefill, 1) for fparam in form_parameters: debug('Trying on parameter %s' % fparam, 2) # check which "type" to prefill if wordlist.is_of_type(fparam.get('type'), type_to_prefill): debug('%s is of type %s' % (type_to_prefill, fparam.get('type'))) fid = fparam.get('id') if replace_value: kw[fid] = prefill_with # if fparam is of `file` type, we need to modify it if wordlist.is_of_type(fparam.get('type'), wordlist.FILE): debug('CHANGE FILEFIELD ##############################################') multiple = fparam.get('multiple', False) #TODO utility method to get all children for field in form.children_deep(): if field.id == fid: if multiple: mod = _change_file_field(form, field, tw2.forms.MultipleSelectField, prefill_with) else: mod = _change_file_field(form, field, tw2.forms.SingleSelectField, prefill_with) modified.append(mod) return modified