Not so long ago, I started to work on a new pylons project at work. I was now in charge of the development of a new website dealing with accountancy datas.
The particularity of this project is that data is stocked in a proprietary software and we communicate with it using a socket and a special kind of textual request language.
Doing it with Zope ?
Request are in a csv-like format. For example, to fetch all products id, name and available quantity in stock, we would send this:
RQ39,R01,,PRO,PNOM,,PNUM,PNOM,PQST
The response will be something like a csv list of the requested attributes and values. I decided to build a simple kind of ORM and to connect it to pylons with formalchemy and normal html tables.
Formalchemy is a form generator especially useful when using SQLAlchemy. But I did not want to create a kind of adaptor for our strange request system. So I decided to use zope.schema and zope.interface to define the structure of the different fields and to connect everything to my custom ORM.
1. First of all, I defined my form's model with the different field types and their attributes, that way:
class IBuyEditForm(interface.Interface):
buyid = schema.Int(title=u'Id', readonly=True)
prodid = schema.Int(title=u'Product', required=True)
quantity = schema.Int(title=Quantity', required=True)
2. Secondly I implemented my form model as a new type of FieldSet:
from formalchemy.ext.zope import FieldSet
BuyEditForm = FieldSet(IBuyEditForm)
3. Now I use my form's fieldset in a pylons controller:
from formalchemy.ext.zope import FlexibleDict
from your.project.forms import BuyEditForm
from your.project.lib.buy import BuyWriter
def edit(self):
instance = FlexibleDict()
c.form = BuyEditForm.bind(instance, data=request.params or None)
if request.method == "POST" and c.form.validate():
c.form.sync()
BuyWriter.update(**instance)
return redirect_to(controler='buy', action='detail', id=instance.id)
return render("/buy/edit.mako")
Conclusion
With the simplicity of formalchemy and zope.schema, I successfuly implemented all needed forms dealing with all my custom queries in my custom ORM. The zope.schema part is very useful to define generic validation parameters and structure of forms. The formalchemy part was very easy to integrate with this kind of strange logic of development, connected with a none common way to interact data.
Thank you zope, you can be easy and useful some times!
Thank you formalchemy, you're the best !