Permalink
Browse files

Merge remote branch 'boardman/master'

  • Loading branch information...
2 parents d4e86b0 + a3bcb93 commit d2a2b14099e0a70f3f2f673f008fb831c9b980f9 @pydanny pydanny committed May 11, 2010
Showing with 89 additions and 30 deletions.
  1. +54 −22 uni_form/helpers.py
  2. +27 −0 uni_form/templates/uni_form/multifield.html
  3. +8 −8 uni_form/templatetags/uni_form_tags.py
View
@@ -66,19 +66,16 @@ class Reset(BaseInput):
input_type = 'reset'
field_classes = 'reset resetButton'
-def render_field(field, form):
- if isinstance(field, str):
- return render_form_field(form, field)
- else:
+def render_field(field, form, template="uni_form/field.html", labelclass=None):
+ if not isinstance(field, str):
return field.render(form)
-def render_form_field(form, field):
try:
field_instance = form.fields[field]
except KeyError:
raise Exception("Could not resolve form field '%s'." % field)
bound_field = BoundField(form, field_instance, field)
- html = render_to_string("uni_form/field.html", {'field': bound_field})
+ html = render_to_string(template, {'field': bound_field, 'labelclass': labelclass})
if not hasattr(form, 'rendered_fields'):
form.rendered_fields = []
if not field in form.rendered_fields:
@@ -120,10 +117,7 @@ class Fieldset(object):
''' Fieldset container. Renders to a <fieldset>. '''
def __init__(self, legend, *fields, **args):
- if 'css_class' in args.keys():
- self.css = args['css_class']
- else:
- self.css = None
+ self.css = args.get('css_class', None)
self.legend_html = legend and ('<legend>%s</legend>' % unicode(legend)) or ''
self.fields = fields
@@ -141,38 +135,78 @@ def render(self, form):
+class MultiField(object):
+ ''' multiField container. Renders to a multiField <div> '''
+
+ def __init__(self, label, *fields, **kwargs):
+ #TODO: Decide on how to support css classes for both container divs
+ self.div_class = kwargs.get('css_class', u'ctrlHolder')
+ self.label_class = kwargs.get('label_class', u'blockLabel')
+ self.label_html = label and (u'<p class="label">%s</p>\n' % unicode(label)) or ''
+ self.fields = fields
+
+ def render(self, form):
+ fieldoutput = u''
+ errors = u''
+ helptext = u''
+ count = 0
+ for field in self.fields:
+ fieldoutput += render_field(field, form, 'uni_form/multifield.html', self.label_class)
+ try:
+ field_instance = form.fields[field]
+ except KeyError:
+ raise Exception("Could not resolve form field '%s'." % field)
+ bound_field = BoundField(form, field_instance, field)
+ auto_id = bound_field.auto_id
+ for error in bound_field.errors:
+ errors += u'<p id="error_%i_%s" class="errorField">%s</p>' % (count, auto_id, error)
+ count += 1
+ if bound_field.help_text:
+ helptext += u'<p id="hint_%s" class="formHint">%s</p>' % (auto_id, bound_field.help_text)
+
+ if errors:
+ self.css += u' error'
+
+ output = u'<div class="%s">\n' % self.div_class
+ output += errors
+ output += self.label_html
+ output += u'<div class="multiField">\n'
+ output += fieldoutput
+ output += u'</div>\n'
+ output += helptext
+ output += u'</div>\n'
+ return output
+
+
+
class Row(object):
''' row container. Renders to a set of <div>'''
def __init__(self, *fields, **kwargs):
self.fields = fields
- if 'css_class' in kwargs.keys():
- self.css = kwargs['css_class']
- else:
- self.css = "formRow"
-
+ self.css = kwargs.get('css_class', u'formRow')
+
def render(self, form):
output = u'<div class="%s">' % self.css
for field in self.fields:
output += render_field(field, form)
output += u'</div>'
return u''.join(output)
+
class Column(object):
''' column container. Renders to a set of <div>'''
def __init__(self, *fields, **kwargs):
self.fields = fields
- if 'css_class' in kwargs.keys():
- self.css = kwargs['css_class']
- else:
- self.css = "formColumn"
-
+ self.css = kwargs.get('css_class', u'formColumn')
+
def render(self, form):
output = u'<div class="%s">' % self.css
for field in self.fields:
output += render_field(field, form)
output += u'</div>'
return u''.join(output)
+
class HTML(object):
''' HTML container '''
@@ -184,8 +218,6 @@ def render(self, form):
return self.html
-
-
class FormHelper(object):
"""
By setting attributes to me you can easily create the text that goes
@@ -0,0 +1,27 @@
+{% load uni_form_field %}
+
+{% if field.is_hidden %}
+ {{ field }}
+{% else %}
+
+ {% if field.label %}
+ <label for="{{ field.auto_id }}"{% if labelclass %} class="{{ labelclass }}"{% endif %}>
+ {% endif %}
+
+ {% if field|is_checkbox %}
+ {{ field|with_class }}
+ {% endif %}
+
+ {% if field.label %}
+ {{ field.label }}
+ {% endif %}
+
+ {% if not field|is_checkbox %}
+ {{ field|with_class }}
+ {% endif %}
+
+ {% if field.label %}
+ </label>
+ {% endif %}
+
+{% endif %}
@@ -17,7 +17,7 @@
if django_version.startswith('1.1.2') or django_version.startswith('1.2'):
is_old_django = False
else:
- from warnings import warn
+ from warnings import warn
warn("""You are using a version of Django that does not support the new csrf_token templatetag. It is advised that you upgrade to 1.1.2, 1.2, or another modern version of Django""")
###################################################
@@ -227,21 +227,21 @@ def render(self,context):
# TODO: remove when pre-CSRF token templatetags are no longer supported
if is_old_django:
-
+
# csrf token fix hack.
# Creates bogus csrf_token so we can continue to support older versions of Django.
class CsrfTokenNode(template.Node):
def render(self, context):
-
+
return ''
@register.tag(name="csrf_token")
def dummy_csrf_token(parser, data):
-
- return CsrfTokenNode()
-
-
-
+ return CsrfTokenNode()
+
+
+
+

0 comments on commit d2a2b14

Please sign in to comment.