Ticket #9387: 04-formtemplates.diff

File 04-formtemplates.diff, 15.1 KB (added by Petr Marhoun <petr.marhoun@…>, 16 years ago)
  • django/contrib/formtemplates/__init__.py

    === added directory 'django/contrib/formtemplates'
    === added file 'django/contrib/formtemplates/__init__.py'
     
     1
  • django/contrib/formtemplates/templates/form.html

    === added directory 'django/contrib/formtemplates/templates'
    === added file 'django/contrib/formtemplates/templates/form.html'
     
     1{% extends base_template %}
     2
     3{% block content %}
     4    {% include "parts/form-wrapper.html" %}
     5{% endblock %}
  • django/contrib/formtemplates/templates/formset.html

    === added file 'django/contrib/formtemplates/templates/formset.html'
     
     1{% extends base_template %}
     2
     3{% block content %}
     4    {% include "parts/formset-wrapper.html" %}
     5{% endblock %}
  • django/contrib/formtemplates/templates/parts/field-errors.html

    === added directory 'django/contrib/formtemplates/templates/parts'
    === added file 'django/contrib/formtemplates/templates/parts/field-errors.html'
     
     1{% if field.errors %}
     2    <ul class="errors">
     3        {% for error in field.errors %}
     4            <li>{{ error }}</li>
     5        {% endfor %}
     6    </ul>
     7{% endif %}
  • django/contrib/formtemplates/templates/parts/field-help-text.html

    === added file 'django/contrib/formtemplates/templates/parts/field-help-text.html'
     
     1{% if field.help_text %}
     2    <p class="help-text">{{ field.help_text }}</p>
     3{% endif %}
  • django/contrib/formtemplates/templates/parts/field.html

    === added file 'django/contrib/formtemplates/templates/parts/field.html'
     
     1{% load forms %}
     2{% if not field.is_hidden %}
     3    {% if field.template %}
     4        {% include field.template %}
     5    {% else %}
     6        <tr {{ field|attrs }}>
     7            <th>{{ field|label_tag }}</th>
     8            <td>
     9                {% include "parts/field-errors.html" %}
     10                {{ field }}
     11                {% include "parts/field-help-text.html" %}
     12            </td>
     13        </tr>
     14    {% endif %}
     15{% endif %}
  • django/contrib/formtemplates/templates/parts/fieldset-legend.html

    === added file 'django/contrib/formtemplates/templates/parts/fieldset-legend.html'
     
     1{% if fieldset.legend %}
     2    <legend>{{ fieldset.legend }}</legend>
     3{% endif %}
  • django/contrib/formtemplates/templates/parts/fieldset.html

    === added file 'django/contrib/formtemplates/templates/parts/fieldset.html'
     
     1{% load forms %}
     2<fieldset {{ fieldset|attrs }}>
     3    {% include "parts/fieldset-legend.html" %}
     4    <table>
     5        {% ifequal fieldset.order 0 %}
     6            {% include "parts/form-hidden.html" %}
     7        {% endifequal %}
     8        {% for field in fieldset.fields %}
     9            {% include "parts/field.html" %}
     10        {% endfor %}
     11        {% if forloop.last %}
     12            {% include "parts/form-submit.html" %}
     13        {% endif %}
     14    </table>
     15</fieldset>
  • django/contrib/formtemplates/templates/parts/form-errors.html

    === added file 'django/contrib/formtemplates/templates/parts/form-errors.html'
     
     1{% if form.non_field_errors %}
     2    <ul class="errors non-field-errors">
     3        {% for error in form.non_field_errors %}
     4            <li>{{ error }}</li>
     5        {% endfor %}
     6    </ul>
     7{% endif %}
  • django/contrib/formtemplates/templates/parts/form-hidden.html

    === added file 'django/contrib/formtemplates/templates/parts/form-hidden.html'
     
     1<tr class="hidden">
     2    <td colspan="2">
     3        {% for field in form %}
     4            {% if field.is_hidden %}
     5                {{ field }}
     6            {% endif %}
     7        {% endfor %}
     8    </td>
     9</tr>
  • django/contrib/formtemplates/templates/parts/form-submit.html

    === added file 'django/contrib/formtemplates/templates/parts/form-submit.html'
     
     1<tr class="submit">
     2    <td colspan="2">
     3        {% if submit_row %}
     4            {{ submit_row }}
     5        {% else %}
     6            <input type="submit" value="{% trans 'Submit' %}"/>
     7        {% endif %}
     8    </td>
     9</tr>
  • django/contrib/formtemplates/templates/parts/form-wrapper.html

    === added file 'django/contrib/formtemplates/templates/parts/form-wrapper.html'
     
     1{% load forms %}
     2{% if form.template %}
     3    {% include form.template %}
     4{% else %}
     5    <form action="." method="post" {% if form.is_multipart %} enctype="multipart/form-data" {% endif %} {{ form|attrs }}>
     6        {% include "parts/form.html" %}
     7    </form>
     8{% endif %}
  • django/contrib/formtemplates/templates/parts/form.html

    === added file 'django/contrib/formtemplates/templates/parts/form.html'
     
     1{% include "parts/form-errors.html" %}
     2{% if form.has_fieldsets %}
     3    {% for fieldset in form.fieldsets %}
     4        {% if fieldset.formset %}
     5            {% with fieldset.formset as formset %}
     6                {% if formset.template %}
     7                    {% include formset.template %}
     8                {% else %}
     9                    {% include "parts/formset.html" %}
     10                {% endif %}
     11            {% endwith %}
     12        {% else %}
     13            {% if fieldset.template %}
     14                {% include fieldset.template %}
     15            {% else %}
     16                {% include "parts/fieldset.html" %}
     17            {% endif %}
     18        {% endif %}
     19    {% endfor %}
     20{% else %}
     21    <table>
     22        {% include "parts/form-hidden.html" %}
     23        {% for field in form %}
     24            {% include "parts/field.html" %}
     25        {% endfor %}
     26        {% include "parts/form-submit.html" %}
     27    </table>
     28{% endif %}
  • django/contrib/formtemplates/templates/parts/formset-errors.html

    === added file 'django/contrib/formtemplates/templates/parts/formset-errors.html'
     
     1{% if formset.non_form_errors %}
     2    <ul class="errors non-form-errors">
     3        {% for error in formset.non_form_errors %}
     4            <li>{{ error }}</li>
     5        {% endfor %}
     6    </ul>
     7{% endif %}
  • django/contrib/formtemplates/templates/parts/formset-field.html

    === added file 'django/contrib/formtemplates/templates/parts/formset-field.html'
     
     1{% load forms %}
     2{% if not field.is_hidden %}
     3    {% if field.template %}
     4        {% include field.template %}
     5    {% else %}
     6        <td {{ field|attrs }}>
     7            {% include "parts/field-errors.html" %}
     8            {{ field }}
     9            {% include "parts/field-help-text.html" %}
     10        </td>
     11    {% endif %}
     12{% endif %}
  • django/contrib/formtemplates/templates/parts/formset-form-errors.html

    === added file 'django/contrib/formtemplates/templates/parts/formset-form-errors.html'
     
     1{% load forms %}
     2{% if form.non_field_errors %}
     3    <tr class="errors non-field-errors">
     4        <td colspan="{{ form|colspan }}">
     5            <ul>
     6                {% for error in form.non_field_errors %}
     7                    <li>{{ error }}</li>
     8                {% endfor %}
     9            </ul>
     10        </td>
     11    </tr>
     12{% endif %}
  • django/contrib/formtemplates/templates/parts/formset-form.html

    === added file 'django/contrib/formtemplates/templates/parts/formset-form.html'
     
     1{% include "parts/formset-form-errors.html" %}
     2<tr>
     3    {% for field in form %}
     4        {% include "parts/formset-field.html" %}
     5    {% endfor %}
     6</tr>
  • django/contrib/formtemplates/templates/parts/formset-header.html

    === added file 'django/contrib/formtemplates/templates/parts/formset-header.html'
     
     1<tr class="header">
     2    {% for field in formset.forms.0 %}
     3        {% if not field.is_hidden %}
     4            <th>{{ field.label }}</th>
     5        {% endif %}
     6    {% endfor %}
     7</tr>
  • django/contrib/formtemplates/templates/parts/formset-hidden.html

    === added file 'django/contrib/formtemplates/templates/parts/formset-hidden.html'
     
     1{% load forms %}
     2<tr class="hidden">
     3    <td colspan="{{ formset.forms.0|colspan }}">
     4        {{ formset.management_form }}
     5        {% for form in formset.forms %}
     6            {% for field in form %}
     7                {% if field.is_hidden %}
     8                    {{ field }}
     9                {% endif %}
     10            {% endfor %}
     11        {% endfor %}
     12    </td>
     13</tr>
  • django/contrib/formtemplates/templates/parts/formset-legend.html

    === added file 'django/contrib/formtemplates/templates/parts/formset-legend.html'
     
     1{% if formset.legend %}
     2    <legend>{{ formset.legend }}</legend>
     3{% endif %}
  • django/contrib/formtemplates/templates/parts/formset-submit.html

    === added file 'django/contrib/formtemplates/templates/parts/formset-submit.html'
     
     1{% load forms %}
     2<tr class="submit">
     3    <td colspan="{{ formset.forms.0|colspan }}">
     4        {% if submit_row %}
     5            {{ submit_row }}
     6        {% else %}
     7            <input type="submit" value="{% trans 'Submit' %}"/>
     8        {% endif %}
     9    </td>
     10</tr>
  • django/contrib/formtemplates/templates/parts/formset-wrapper.html

    === added file 'django/contrib/formtemplates/templates/parts/formset-wrapper.html'
     
     1{% load forms %}
     2{% if formset.template %}
     3    {% include formset.template %}
     4{% else %}
     5    <form action="." method="post" {% if formset.is_multipart %} enctype="multipart/form-data" {% endif %} {{ formset|attrs }}>
     6        {% include "parts/formset.html" %}
     7    </form>
     8{% endif %}
  • django/contrib/formtemplates/templates/parts/formset.html

    === added file 'django/contrib/formtemplates/templates/parts/formset.html'
     
     1{% load forms %}
     2<fieldset {{ formset|attrs }}>
     3    {% include "parts/formset-legend.html" %}
     4    {% include "parts/formset-errors.html" %}
     5    <table>
     6        {% include "parts/formset-hidden.html" %}
     7        {% include "parts/formset-header.html" %}
     8        {% for form in formset.forms %}
     9            {% if form.template %}
     10                {% include form.template %}
     11            {% else %}
     12                {% include "parts/formset-form.html" %}
     13            {% endif %}
     14            {% if forloop.parentloop %}
     15                {% if forloop.parentloop.last and forloop.last %}
     16                    {% include "parts/formset-submit.html" %}
     17                {% endif %}
     18            {% else %}
     19                {% if forloop.last %}
     20                    {% include "parts/formset-submit.html" %}
     21                {% endif %}
     22            {% endif %}
     23        {% endfor %}
     24    </table>
     25</fieldset>
  • django/contrib/formtemplates/templatetags/forms.py

    === added directory 'django/contrib/formtemplates/templatetags'
    === added file 'django/contrib/formtemplates/templatetags/__init__.py'
    === added file 'django/contrib/formtemplates/templatetags/forms.py'
     
     1from django.forms.util import flatatt
     2from django.utils.safestring import mark_safe
     3from django.forms.forms import BaseForm, BoundField
     4from django.forms.formsets import BaseFormSet
     5import re
     6
     7from django.template import Library
     8register = Library()
     9
     10html_class_re = re.compile(r'([a-z])([A-Z])')
     11
     12def attrs(input):
     13    if isinstance(input, BaseForm):
     14        if input.attrs:
     15            return mark_safe(flatatt(input.attrs))
     16        else:
     17            return u''
     18    elif isinstance(input, dict):
     19        if input['attrs']:
     20            return mark_safe(flatatt(input['attrs']))
     21        else:
     22            return u''
     23    elif isinstance(input, BaseFormSet):
     24        attrs = input.attrs and input.attrs.copy() or {}
     25        class_attrs = attrs.get('class', '').split()
     26        class_attrs.append('formset')
     27        attrs['class'] = u' '.join(class_attrs)
     28        return mark_safe(flatatt(attrs))
     29    elif isinstance(input, BoundField):
     30        attrs = input.attrs and input.attrs.copy() or {}
     31        class_attrs = attrs.get('class', '').split()
     32        class_attrs.append(html_class_re.sub(r'\1-\2', input.field.__class__.__name__).lower())
     33        class_attrs.append(html_class_re.sub(r'\1-\2', input.field.widget.__class__.__name__).lower())
     34        if input.field.required:
     35            class_attrs.append(u'required')
     36        if input.errors:
     37            class_attrs.append(u'errors')
     38        attrs['class'] = u' '.join(class_attrs)
     39        return mark_safe(flatatt(attrs))
     40    else:
     41        raise ValueError
     42attrs = register.filter(attrs)
     43
     44def label_tag(bf):
     45    id = bf.field.widget.attrs.get('id') or bf.auto_id
     46    if id:
     47        return mark_safe(u'<label for="%s">%s:</label>' % (bf.field.widget.id_for_label(id), bf.label))
     48    else:
     49        return bf.label
     50label_tag = register.filter(label_tag)
     51
     52def colspan(form):
     53    return len([field for field in form if not field.is_hidden])
     54colspan = register.filter(colspan)
     55 No newline at end of file
Back to Top