Ejemplo n.º 1
0
def render_fields_group(form, pattern):
    from django.forms import forms

    first = forms.BoundField(form, form.fields[pattern], pattern)
    label = first.label
    group = [first]
    cpt = 1
    haserror = len(first.errors) != 0
    while True:
        fname = "%s_%d" % (pattern, cpt)
        if not fname in form.fields:
            break
        bfield = forms.BoundField(form, form.fields[fname], fname)
        if len(bfield.errors):
            haserror = True
        group += [bfield]
        cpt += 1

    return render_to_string("common/generic_fields_group.html", dict(
        label=label, help_text=first.help_text, group=group, haserror=haserror
    ))
Ejemplo n.º 2
0
def render_rule_fields(form):
    """Render access rules for a given calendar."""
    from django.forms import forms

    cpt = 1
    result = ""
    while True:
        fname = "username_%d" % cpt
        if fname not in form.fields:
            break
        rfieldname = "read_access_%d" % cpt
        wfieldname = "write_access_%d" % cpt
        result += render_to_string('radicale/accessrule.html', {
            "username": forms.BoundField(form, form.fields[fname], fname),
            "read_access": forms.BoundField(
                form, form.fields[rfieldname], rfieldname),
            "write_access": forms.BoundField(
                form, form.fields[wfieldname], wfieldname)
        })
        cpt += 1
    return result
Ejemplo n.º 3
0
def as_readonly_table(context, form):
    """Outputs a form as a properly formatted html table.

  Args:
    form: the form that should be converted to a table
  """

    # create the bound fields
    fields = [
        forms_in.BoundField(form, field, name)
        for name, field in form.fields.items() if field
    ]

    return {'fields': fields}
Ejemplo n.º 4
0
def as_table_helper(context, form):
    """See as_table().
  """

    fields = []
    hidden_fields = []
    hidden_fields_errors = []
    errors = False

    # entity = context['entity']

    # support defining output order like in Django
    a = form.Meta
    if hasattr(form, 'Meta') and form.Meta.fields:
        items = [(i, form.fields[i]) for i in form.Meta.fields]
    else:
        items = form.fields.items()

    # Iterate over all fields and prepare it for adding
    for name, field in items:
        if not field:
            continue

        bf = forms_in.BoundField(form, field, name)
        attrs = {}

        if isinstance(field, widgets.ReferenceField):
            attrs = field.rf

        # If the field is hidden we display it elsewhere
        if not bf.is_hidden:
            if bf.errors:
                errors = True

            example_text = ''
            group = '0. '

            if hasattr(field, 'group'):
                group = field.group

            if hasattr(field, 'example_text'):
                example_text = force_unicode(field.example_text)

            item = {
                'field': bf,
                'required': field.required,
                'example_text': example_text,
                'group': group,
            }

            item.update(attrs)
            fields.append(item)
        else:
            hidden_fields.append(unicode(bf))

            for error in bf.errors:
                item = (name, force_unicode(error))
                hidden_fields_errors.append(item)

    grouped = dicts.groupby(fields, 'group')
    rexp = re.compile(r"\d+. ")
    fields = [(rexp.sub('', key), grouped[key]) for key in sorted(grouped)]

    context.update({
        'top_errors': form.non_field_errors() or '',
        'hidden_field_errors': hidden_fields_errors or '',
        'errors': errors,
        'groups': fields if fields else '',
        'hidden_fields': hidden_fields or '',
        'form': form,
        'json_for_js': {},
    })

    return context
Ejemplo n.º 5
0
def select_field(form, key):
    key = str(key)
    if key not in form.fields:
        return None

    return forms.BoundField(form, form.fields[key], key)