Skip to content

Commit

Permalink
Merge Blaise's css_class patches from https://bitbucket.org/blaf/deform:
Browse files Browse the repository at this point in the history
$ hg diff -r 2d51adca838f > 1.patch
$ cd /path/to/deform/trunk
$ patch -p1 < /wherever/1.patch

- The ``deform.widget.Widget`` class now accepts an extra keyword
  argument in its constructor: ``css_class``.

  All widgets now inherit a ``css_class`` attribute from the base
  ``deform.widget.Widget`` class.  If `css_class`` contains a value,
  the primary element in the rendered widget will get a CSS ``class``
  attribute equal to the value.

Also add a CONTRIBUTORS.txt and fix a couple minor documentation bugs and 
remove a missing import.
  • Loading branch information
mcdonc committed Aug 21, 2010
1 parent f7dec08 commit 7c351db
Show file tree
Hide file tree
Showing 23 changed files with 77 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .hgignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ env
dist
.coverage
.build
*.pyc
.svn

7 changes: 7 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ Features
a text input that can be supplied a URL or iterable of choices to
ease the search and selection of a finite set of choices.

- The ``deform.widget.Widget`` class now accepts an extra keyword
argument in its constructor: ``css_class``.

All widgets now inherit a ``css_class`` attribute from the base
``deform.widget.Widget`` class. If `css_class`` contains a value,
the primary element in the rendered widget will get a CSS ``class``
attribute equal to the value.

0.3
---
Expand Down
4 changes: 4 additions & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Chris McDonough
Tres Seaver
Reed O'Brien
Blaise Laflamme
17 changes: 9 additions & 8 deletions deform/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ class Form(field.Field):
formid
The identifier for this form. This value will be used as the
value of a hidden form input control (``__formid__``) which
will be placed in this form's rendering. You should pass a
string value for ``formid`` when more than one Deform form is
placed into a single page and both share the same action.
When one of the forms on the page is posted, your code will to
be able to decide which of those forms was posted based on the
differing values of ``__formid__``. By default, ``formid`` is
``deform``.
HTML ``id`` attribute of the rendered HTML form. It will also
be used as the value of a hidden form input control
(``__formid__``) which will be placed in this form's
rendering. You should pass a string value for ``formid`` when
more than one Deform form is placed into a single page and
both share the same action. When one of the forms on the page
is posted, your code will to be able to decide which of those
forms was posted based on the differing values of
``__formid__``. By default, ``formid`` is ``deform``.
use_ajax
If this option is ``True``, the form will use AJAX (actually
Expand Down
11 changes: 11 additions & 0 deletions deform/static/css/form.css
Original file line number Diff line number Diff line change
Expand Up @@ -931,3 +931,14 @@ a.deformSeqAdd:visited {
div.deformReplaces {
float: left;
}

/* css_class test */
form .deformWidgetWithStyle{
width:100px;
margin:0;
padding: 10px;
border: 2px solid #71a23c;
background-color: #fff7c0;
font-weight: bold;
}

2 changes: 1 addition & 1 deletion deform/templates/autocomplete_input.pt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<span tal:omit-tag="">
<input type="text" name="${field.name}" value="${cstruct}"
tal:attributes="size field.widget.size"
tal:attributes="size field.widget.size; class field.widget.css_class;"
id="${field.oid}"/>
<script tal:condition="field.widget.values" type="text/javascript">
jQuery(function($) {
Expand Down
2 changes: 1 addition & 1 deletion deform/templates/checkbox.pt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<input type="checkbox"
name="${field.name}" value="${field.widget.true_val}"
id="${field.oid}"
tal:attributes="checked cstruct == field.widget.true_val"/>
tal:attributes="checked cstruct == field.widget.true_val; class field.widget.css_class;"/>

2 changes: 1 addition & 1 deletion deform/templates/checkbox_choice.pt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<tal:loop tal:repeat="choice field.widget.values">
<tal:def tal:define="(value, description) choice">
<label for="${field.oid}-${repeat.choice.index}">${description}</label>
<input tal:attributes="checked value in cstruct"
<input tal:attributes="checked value in cstruct; class field.widget.css_class;"
type="checkbox"
name="checkbox"
value="${value}"
Expand Down
4 changes: 2 additions & 2 deletions deform/templates/checked_input.pt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<div>
<label for="${field.oid}">${subject}</label>
<input type="text" name="value" value="${cstruct}"
tal:attributes="size field.widget.size" id="${field.oid}"/>
tal:attributes="size field.widget.size; class field.widget.css_class;" id="${field.oid}"/>
</div>
<div>
<label for="${field.oid}-confirm">${confirm_subject}</label>
<input type="text" name="confirm" value="${confirm}"
tal:attributes="size field.widget.size" id="${field.oid}-confirm"/>
tal:attributes="size field.widget.size; class field.widget.css_class;" id="${field.oid}-confirm"/>
</div>

<script tal:condition="field.widget.mask" type="text/javascript">
Expand Down
2 changes: 1 addition & 1 deletion deform/templates/checked_password.pt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div>
<label for="${field.oid}-confirm">Confirm Password</label>
<input type="password" name="confirm" value="${confirm}"
tal:attributes="size field.widget.size" id="${field.oid}-confirm"/>
tal:attributes="size field.widget.size; class field.widget.css_class;" id="${field.oid}-confirm"/>
</div>
<input type="hidden" name="__end__" value="${field.name}:mapping"/>
</div>
2 changes: 1 addition & 1 deletion deform/templates/dateinput.pt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<span tal:omit-tag="">
<input type="date" name="${field.name}" value="${cstruct}"
tal:attributes="size field.widget.size"
tal:attributes="size field.widget.size; class field.widget.css_class;"
id="${field.oid}"/>
<script type="text/javascript">
jQuery(function($) {
Expand Down
6 changes: 3 additions & 3 deletions deform/templates/dateparts.pt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<input type="hidden" name="__start__" value="${field.name}:mapping"/>
<label for="${field.oid}">Year</label>
<input type="text" name="year" value="${year}"
tal:attributes="size field.widget.size" id="${field.oid}"/>
tal:attributes="size field.widget.size; class field.widget.css_class;" id="${field.oid}"/>
<label for="${field.oid}-month">Month</label>
<input type="text" name="month" value="${month}"
tal:attributes="size field.widget.size" id="${field.oid}-month"/>
tal:attributes="size field.widget.size; class field.widget.css_class;" id="${field.oid}-month"/>
<label for="${field.oid}-day">Day</label>
<input type="text" name="day" value="${day}"
tal:attributes="size field.widget.size" id="${field.oid}-day"/>
tal:attributes="size field.widget.size; class field.widget.css_class;" id="${field.oid}-day"/>
<input type="hidden" name="__end__" value="${field.name}:mapping"/>
</div>

Expand Down
2 changes: 1 addition & 1 deletion deform/templates/file_upload.pt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

</div>

<input type="file" name="upload" tal:attributes="size field.widget.size"
<input type="file" name="upload" tal:attributes="size field.widget.size; class field.widget.css_class;"
id="${field.oid}"/>

<input type="hidden" name="__end__" value="${field.name}:mapping"/>
Expand Down
2 changes: 1 addition & 1 deletion deform/templates/password.pt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<input type="password" name="${field.name}" value="${cstruct}"
tal:attributes="size field.widget.size" id="${field.oid}"/>
tal:attributes="size field.widget.size; class field.widget.css_class;" id="${field.oid}"/>

2 changes: 1 addition & 1 deletion deform/templates/radio_choice.pt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<span tal:repeat="choice field.widget.values" tal:omit-tag="">
<label for="${field.oid}-${repeat.choice.index}">${choice[1]}</label>
<input tal:attributes="checked choice[0] == cstruct"
<input tal:attributes="checked choice[0] == cstruct; class field.widget.css_class;"
type="radio"
name="${field.name}"
value="${choice[0]}"
Expand Down
2 changes: 1 addition & 1 deletion deform/templates/readonly/autocomplete_input.pt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<span tal:omit-tag="">
<input type="text" name="${field.name}" value="${cstruct}"
tal:attributes="size field.widget.size"
tal:attributes="size field.widget.size; class field.widget.css_class;"
id="${field.oid}"/>
<script tal:condition="field.widget.values" type="text/javascript">
jQuery(function($) {
Expand Down
2 changes: 1 addition & 1 deletion deform/templates/select.pt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<select name="${field.name}"
id="${field.oid}">
<option tal:repeat="(value, description) field.widget.values"
tal:attributes="selected value == cstruct"
tal:attributes="selected value == cstruct; class field.widget.css_class;"
value="${value}">${description}</option>
</select>
3 changes: 2 additions & 1 deletion deform/templates/textarea.pt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<textarea tal:attributes="rows field.widget.rows;
cols field.widget.cols;"
cols field.widget.cols;
class field.widget.css_class;"
id="${field.oid}"
name="${field.name}">${cstruct}</textarea>
2 changes: 1 addition & 1 deletion deform/templates/textinput.pt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<span tal:omit-tag="">
<input type="text" name="${field.name}" value="${cstruct}"
tal:attributes="size field.widget.size"
tal:attributes="size field.widget.size; class field.widget.css_class;"
id="${field.oid}"/>
<script tal:condition="field.widget.mask" type="text/javascript">
jQuery(function($) {
Expand Down
1 change: 1 addition & 0 deletions deform/tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class DummyWidget(object):
name = 'name'
true_val = 'true'
false_val = 'false'
css_class = None

class DummyField(object):
widget = DummyWidget()
Expand Down
7 changes: 6 additions & 1 deletion deform/widget.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import colander
import csv
import random
import string
Expand Down Expand Up @@ -63,6 +62,11 @@ class Widget(object):
The name of the CSS class attached to various tags in the form
renderering indicating an error condition for the field
associated with this widget. Default: ``error``.
css_class
The name of the CSS class attached to various tags in
the form renderering specifying a new class for the field
associated with this widget. Default: ``None`` (no class).
These attributes are also accepted as keyword arguments to all
widget constructors; if they are passed, they will override the
Expand All @@ -77,6 +81,7 @@ class Widget(object):
hidden = False
category = 'default'
error_class = 'error'
css_class = None

def __init__(self, **kw):
self.__dict__.update(kw)
Expand Down
12 changes: 12 additions & 0 deletions deformdemo/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ class Schema(colander.Schema):
form = deform.Form(schema, buttons=('submit',))
form['text'].widget = deform.widget.TextInputWidget(size=60)
return self.render_form(form)

@bfg_view(renderer='templates/form.pt', name='textinput_with_css_class')
@demonstrate('Text Input Widget with CSS Class')
def textinput_with_css_class(self):
class Schema(colander.Schema):
text = colander.SchemaNode(colander.String(),
validator=colander.Length(max=100),
description='Enter some text')
schema = Schema()
form = deform.Form(schema, buttons=('submit',))
form['text'].widget = deform.widget.TextInputWidget(size=60, css_class='deformWidgetWithStyle')
return self.render_form(form)

@bfg_view(renderer='templates/form.pt', name='autocomplete_input')
@demonstrate('Autocomplete Input Widget')
Expand Down
7 changes: 7 additions & 0 deletions deformdemo/tests/test_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,13 @@ def test_submit_filled(self):
captured = browser.get_text('css=#captured')
self.assertEqual(captured, "{'text': u'hello'}")

class TextInputWithCssClassWidgetTests(unittest.TestCase):
url = "/textinput_with_css_class/"
def test_render_default(self):
browser.open(self.url)
browser.wait_for_page_to_load("30000")
self.assertTrue(browser.is_element_present('css=.deformWidgetWithStyle'))

class AutocompleteInputWidgetTests(unittest.TestCase):
url = "/autocomplete_input/"
def test_render_default(self):
Expand Down

0 comments on commit 7c351db

Please sign in to comment.