Example #1
File: plugin.py Project: bbcf/bs
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

    modified = []  # list of modified fields
    debug("\nPREFILL FIELDS")
    for type_to_prefill, prefill_with in prefill_params.iteritems():
        debug("type, prefill: %s, %s" % (type_to_prefill, prefill_with))
        for fparam in form_parameters:
            # debug('Checking parameter %s' % fparam,)

            if wordlist.is_of_type(fparam.get("type"), type_to_prefill):
                fid = fparam.get("id")
                # debug('prefilling "%s" ... ' % fid,)

                # when validation occurs, there no need to replace parameters (replace_value = False)
                if replace_value:
                    kw[fid] = prefill_with
                # debug(kw,)
                # if fparam is of `file` type, we need to modify it
                if wordlist.is_of_type(fparam.get("type"), wordlist.FILE):
                    # debug('change file field ...', 3)

                    # multiple = fparam.get('multiple', False)
                    # TODO utility method to get all children
                    for child_index, field in enumerate(form.children_deep()):
                        # debug('test %s' % field, 3)
                        if field.id == fid or fid.startswith("%s:" % field.id) and "BsFileField" in str(field):
                            # if multiple:
                            # mod = _change_file_field(form, field, twb.BsMultiple, prefill_with)
                            # else:
                            mod = _change_file_field(form, field, twb.BsTripleFileField, prefill_with, child_index)
                        # fetch children for this field if any and if not already replaced
                        # it 's not recursive and should bs
                            mod = recursivly_check_and_change_field_children(
                                field, fid, form, prefill_with, child_index
                            if mod:
    return modified
Example #2
File: form.py Project: bbcf/bs
def validator(ptype, required):
    from bs.lib.operations import wordlist

    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