formalchemy.ext.couchdb – CouchDB support

This module provides a subclass of FieldSet to support couchdbkit‘s schema.

Usage

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>...

Classes definitions

FieldSet

class formalchemy.ext.couchdb.FieldSet(model, **kwargs)

See FieldSet

Grid

class formalchemy.ext.couchdb.Grid(cls, instances=None, **kwargs)

See Grid

Session

class formalchemy.ext.couchdb.Session(db)

A SA like Session to implement couchdb

add(record)

add a record

commit()

do nothing since there is no transaction in couchdb

delete(record)

delete a record

query(model, *args, **kwargs)

return a Query bound to model object

remove()

do nothing since there is no transaction in couchdb

update(record)

update a record

Query

class formalchemy.ext.couchdb.Query(model, **options)

A list like object to emulate SQLAlchemy’s Query. This mostly exist to work with webhelpers.paginate.Page

all(**kwargs)

set self to a list of record returned by view named {model_name}/all

get(id)

Get a record by id

view(view_name, **kwargs)

set self to a list of record returned by view named {model_name}/{view_name}

Table Of Contents

Previous topic

Pylons integration

Next topic

formalchemy.ext.fsblob – File system renderer

This Page