Permalink
Browse files

Merging in what patrys (Patryk Zawadzki) did for helper subclassing

  • Loading branch information...
1 parent c80838f commit 80b73d366452becf08496d4df888d11030b66188 @pydanny pydanny committed Apr 26, 2010
Showing with 60 additions and 57 deletions.
  1. +60 −57 uni_form/helpers.py
View
@@ -22,47 +22,47 @@ class FormHelpersException(Exception):
class Submit(BaseInput):
"""
Used to create a Submit button descriptor for the uni_form template tag:
-
+
submit = Submit('Search the Site','search this site')
-
+
Note: The first argument is also slugified and turned into the id for the submit button.
-
+
"""
-
+
input_type = 'submit'
field_classes = 'submit submitButton'
class Button(BaseInput):
"""
Used to create a Submit input descriptor for the uni_form template tag:
-
+
button = Button('Button 1','Press Me!')
-
+
Note: The first argument is also slugified and turned into the id for the button.
"""
-
+
input_type = 'button'
field_classes = 'button'
class Hidden(BaseInput):
"""
Used to create a Hidden input descriptor for the uni_form template tag.
"""
-
+
input_type = 'hidden'
field_classes = 'hidden'
class Reset(BaseInput):
"""
Used to create a Hidden input descriptor for the uni_form template tag.
-
+
reset = Reset('Reset This Form','Revert Me!')
-
+
Note: The first argument is also slugified and turned into the id for the reset.
-
+
"""
-
+
input_type = 'reset'
field_classes = 'reset resetButton'
@@ -90,9 +90,9 @@ def render_form_field(form, field):
class Layout(object):
'''
Form Layout, add fieldsets, rows, fields and html
-
+
example:
-
+
>>> layout = Layout(Fieldset('', 'is_company'),
... Fieldset(_('Contact details'),
... 'email',
@@ -105,7 +105,7 @@ class Layout(object):
'''
def __init__(self, *fields):
self.fields = fields
-
+
def render(self, form):
html = ""
for field in self.fields:
@@ -116,9 +116,9 @@ def render(self, form):
return html
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']
@@ -127,7 +127,7 @@ def __init__(self, legend, *fields, **args):
self.legend_html = legend and ('<legend>%s</legend>' % unicode(legend)) or ''
self.fields = fields
-
+
def render(self, form):
if self.css:
html = u'<fieldset class="%s">' % self.css
@@ -149,7 +149,7 @@ def __init__(self, *fields, **kwargs):
self.css = kwargs['css_class']
else:
self.css = "formRow"
-
+
def render(self, form):
output = u'<div class="%s">' % self.css
for field in self.fields:
@@ -165,7 +165,7 @@ def __init__(self, *fields, **kwargs):
self.css = kwargs['css_class']
else:
self.css = "formColumn"
-
+
def render(self, form):
output = u'<div class="%s">' % self.css
for field in self.fields:
@@ -174,12 +174,12 @@ def render(self, form):
return u''.join(output)
class HTML(object):
-
+
''' HTML container '''
-
+
def __init__(self, html):
self.html = unicode(html)
-
+
def render(self, form):
return self.html
@@ -191,36 +191,36 @@ class FormHelper(object):
By setting attributes to me you can easily create the text that goes
into the uni_form template tag. One use case is to add to your form
class.
-
+
Special attribute behavior:
-
+
method: Defaults to POST but you can also do 'GET'
-
+
form_action: applied to the form action attribute. Can be a named url in
your urlconf that can be executed via the *url* default template tag or can
simply point to another URL.
-
+
id: Generates a form id for dom identification.
If no id provided then no id attribute is created on the form.
-
+
class: add space seperated classes to the class list.
Defaults to uniForm.
Always starts with uniForm even do specify classes.
-
+
form_tag: Defaults to True. If set to False it renders the form without the form tags.
-
+
use_csrf_protection: Defaults to False. If set to True a CSRF protection token is
rendered in the form. This should only be left as False for forms targeting
external sites or internal sites without CSRF protection (as described in the
Django documentation).
Requires the presence of a csrf token in the current context with the identifier
"csrf_token" (which is automatically added to your context when using RequestContext).
-
+
Demonstration:
-
+
First we create a MyForm class and instantiate it
-
+
>>> from django import forms
>>> from uni_form.helpers import FormHelper, Submit, Reset
>>> from django.utils.translation import ugettext_lazy as _
@@ -234,66 +234,69 @@ class FormHelper(object):
... helper.add_input(submit)
... reset = Reset('reset','reset button')
... helper.add_input(reset)
-
+
After this in the template::
-
+
{% load uni_form_tags %}
{% uni_form form form.helper %}
-
+
"""
-
+ _form_method = 'post'
+ _form_action = ''
+ form_id = ''
+ form_class = ''
+ inputs = []
+ layout = None
+ form_tag = True
+ use_csrf_protection = False
+
+
def __init__(self):
- self._form_method = 'post'
- self._form_action = ''
- self.form_id = ''
- self.form_class = ''
- self.inputs = []
+
+ self.inputs = self.inputs[:]
self.toggle = Toggle()
- self.layout = None
- self.form_tag = True
- self.use_csrf_protection = False
-
+
def get_form_method(self):
return self._form_method
-
+
def set_form_method(self, method):
if method.lower() not in ('get','post'):
raise FormHelpersException('Only GET and POST are valid in the \
form_method helper attribute')
-
+
self._form_method = method.lower()
-
+
# we set properties the old way because we want to support pre-2.6 python
form_method = property(get_form_method, set_form_method)
-
+
def get_form_action(self):
return self._form_action
-
+
def set_form_action(self, action):
try:
self._form_action = reverse(action)
except NoReverseMatch:
self._form_action = action
-
+
# we set properties the old way because we want to support pre-2.6 python
form_action = property(get_form_action, set_form_action)
-
+
def add_input(self, input_object):
self.inputs.append(input_object)
-
+
def add_layout(self, layout):
self.layout = layout
-
+
def render_layout(self, form):
return mark_safe(self.layout.render(form))
-
+
def get_attr(self):
items = {}
items['form_method'] = self.form_method.strip()
items['form_tag'] = self.form_tag
items['use_csrf_protection'] = self.use_csrf_protection
-
+
if self.form_action:
items['form_action'] = self.form_action.strip()
if self.form_id:

0 comments on commit 80b73d3

Please sign in to comment.