Skip to content

Commit

Permalink
Merge remote branch 'boardman/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
pydanny committed May 11, 2010
2 parents d4e86b0 + a3bcb93 commit d2a2b14
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 30 deletions.
76 changes: 54 additions & 22 deletions uni_form/helpers.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -66,19 +66,16 @@ class Reset(BaseInput):
input_type = 'reset' input_type = 'reset'
field_classes = 'reset resetButton' field_classes = 'reset resetButton'


def render_field(field, form): def render_field(field, form, template="uni_form/field.html", labelclass=None):
if isinstance(field, str): if not isinstance(field, str):
return render_form_field(form, field)
else:
return field.render(form) return field.render(form)


def render_form_field(form, field):
try: try:
field_instance = form.fields[field] field_instance = form.fields[field]
except KeyError: except KeyError:
raise Exception("Could not resolve form field '%s'." % field) raise Exception("Could not resolve form field '%s'." % field)
bound_field = BoundField(form, field_instance, 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'): if not hasattr(form, 'rendered_fields'):
form.rendered_fields = [] form.rendered_fields = []
if not field in form.rendered_fields: if not field in form.rendered_fields:
Expand Down Expand Up @@ -120,10 +117,7 @@ class Fieldset(object):
''' Fieldset container. Renders to a <fieldset>. ''' ''' Fieldset container. Renders to a <fieldset>. '''


def __init__(self, legend, *fields, **args): def __init__(self, legend, *fields, **args):
if 'css_class' in args.keys(): self.css = args.get('css_class', None)
self.css = args['css_class']
else:
self.css = None
self.legend_html = legend and ('<legend>%s</legend>' % unicode(legend)) or '' self.legend_html = legend and ('<legend>%s</legend>' % unicode(legend)) or ''
self.fields = fields self.fields = fields


Expand All @@ -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): class Row(object):
''' row container. Renders to a set of <div>''' ''' row container. Renders to a set of <div>'''
def __init__(self, *fields, **kwargs): def __init__(self, *fields, **kwargs):
self.fields = fields self.fields = fields
if 'css_class' in kwargs.keys(): self.css = kwargs.get('css_class', u'formRow')
self.css = kwargs['css_class']
else:
self.css = "formRow"

def render(self, form): def render(self, form):
output = u'<div class="%s">' % self.css output = u'<div class="%s">' % self.css
for field in self.fields: for field in self.fields:
output += render_field(field, form) output += render_field(field, form)
output += u'</div>' output += u'</div>'
return u''.join(output) return u''.join(output)



class Column(object): class Column(object):
''' column container. Renders to a set of <div>''' ''' column container. Renders to a set of <div>'''
def __init__(self, *fields, **kwargs): def __init__(self, *fields, **kwargs):
self.fields = fields self.fields = fields
if 'css_class' in kwargs.keys(): self.css = kwargs.get('css_class', u'formColumn')
self.css = kwargs['css_class']
else:
self.css = "formColumn"

def render(self, form): def render(self, form):
output = u'<div class="%s">' % self.css output = u'<div class="%s">' % self.css
for field in self.fields: for field in self.fields:
output += render_field(field, form) output += render_field(field, form)
output += u'</div>' output += u'</div>'
return u''.join(output) return u''.join(output)



class HTML(object): class HTML(object):


''' HTML container ''' ''' HTML container '''
Expand All @@ -184,8 +218,6 @@ def render(self, form):
return self.html return self.html






class FormHelper(object): class FormHelper(object):
""" """
By setting attributes to me you can easily create the text that goes By setting attributes to me you can easily create the text that goes
Expand Down
27 changes: 27 additions & 0 deletions uni_form/templates/uni_form/multifield.html
Original file line number Original file line Diff line number Diff line change
@@ -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 %}
16 changes: 8 additions & 8 deletions uni_form/templatetags/uni_form_tags.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
if django_version.startswith('1.1.2') or django_version.startswith('1.2'): if django_version.startswith('1.1.2') or django_version.startswith('1.2'):
is_old_django = False is_old_django = False
else: 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""") 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""")


################################################### ###################################################
Expand Down Expand Up @@ -227,21 +227,21 @@ def render(self,context):


# TODO: remove when pre-CSRF token templatetags are no longer supported # TODO: remove when pre-CSRF token templatetags are no longer supported
if is_old_django: if is_old_django:

# csrf token fix hack. # csrf token fix hack.
# Creates bogus csrf_token so we can continue to support older versions of Django. # Creates bogus csrf_token so we can continue to support older versions of Django.


class CsrfTokenNode(template.Node): class CsrfTokenNode(template.Node):


def render(self, context): def render(self, context):

return '' return ''


@register.tag(name="csrf_token") @register.tag(name="csrf_token")
def dummy_csrf_token(parser, data): def dummy_csrf_token(parser, data):

return CsrfTokenNode()




return CsrfTokenNode()





0 comments on commit d2a2b14

Please sign in to comment.