This module provides a subclass of FieldSet to support couchdbkit‘s schema.
Define a couchdbkit schema:
>>> from couchdbkit import schema
>>> from formalchemy.ext import couchdb
>>> class Person(couchdb.Document):
... name = schema.StringProperty(required=True)
... @classmethod
... def _render_options(self, fs):
... return [(gawel, gawel._id), (benoitc, benoitc._id)]
... def __unicode__(self): return getattr(self, 'name', None) or u''
>>> gawel = Person(name='gawel')
>>> gawel._id = '123'
>>> benoitc = Person(name='benoitc')
>>> benoitc._id = '456'
>>> class Pet(couchdb.Document):
... name = schema.StringProperty(required=True)
... type = schema.StringProperty(required=True)
... birthdate = schema.DateProperty(auto_now=True)
... weight_in_pounds = schema.IntegerProperty()
... spayed_or_neutered = schema.BooleanProperty()
... owner = schema.SchemaProperty(Person)
... friends = schema.SchemaListProperty(Person)
Configure your FieldSet:
>>> fs = couchdb.FieldSet(Pet)
>>> fs.configure(include=[fs.name, fs.type, fs.birthdate, fs.weight_in_pounds])
>>> p = Pet(name='dewey')
>>> p.name = 'dewey'
>>> p.type = 'cat'
>>> p.owner = gawel
>>> p.friends = [benoitc]
>>> fs = fs.bind(p)
Render it:
>>> # rendering
>>> fs.name.is_required()
True
>>> print fs.render()
<div>
<label class="field_req" for="Pet--name">Name</label>
<input id="Pet--name" name="Pet--name" type="text" value="dewey" />
</div>
<script type="text/javascript">
//<![CDATA[
document.getElementById("Pet--name").focus();
//]]>
</script>
<div>
<label class="field_req" for="Pet--type">Type</label>
<input id="Pet--type" name="Pet--type" type="text" value="cat" />
</div>
<div>
<label class="field_opt" for="Pet--birthdate">Birthdate</label>
<span id="Pet--birthdate"><select id="Pet--birthdate__month" name="Pet--birthdate__month">
<option value="MM">Month</option>
...
<option selected="selected" value="...">...</option>
...
Same for grids:
>>> # grid
>>> grid = couchdb.Grid(Pet, [p, Pet()])
>>> grid.configure(include=[grid.name, grid.type, grid.birthdate, grid.weight_in_pounds, grid.friends])
>>> print grid.render()
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Birthdate</th>
<th>Weight in pounds</th>
<th>Friends</th>
</tr>
</thead>
<tbody>
<tr class="even">
<td>
<input id="Pet--name" name="Pet--name" type="text" value="dewey" />
</td>
<td>
<input id="Pet--type" name="Pet--type" type="text" value="cat" />
</td>
<td>
<span id="Pet--birthdate">...
</td>
<td>
<select id="Pet--friends" multiple="multiple" name="Pet--friends">
<option value="123">gawel</option>
<option selected="selected" value="456">benoitc</option>
</select>
</td>...
A list like object to emulate SQLAlchemy’s Query. This mostly exist to work with webhelpers.paginate.Page
set self to a list of record returned by view named {model_name}/all
Get a record by id
set self to a list of record returned by view named {model_name}/{view_name}