I'm having problems creating custom forms in pyramid_formalchemy. I suspect there is a bug in the package and wanted to confirm I'm not missing anything. My setup looks like this:
def includeme(config):
config.include('pyramid_formalchemy')
# Adding the jquery libraries
config.include('fa.jquery')
# Adding the package specific routes
config.include('myapp.web.formalchemy.faroutes')
config.formalchemy_admin('admin',
models=[User],
forms=faforms,
session_factory=session,
view='fa.jquery.pyramid.ModelView',
factory='myapp.model.RootFactory')
config.formalchemy_model('user',
model='myapp.model.user.User',
session_factory=session,
view='fa.jquery.pyramid.ModelView',
factory='myapp.model.RootFactory')
faforms is a module containing my custom forms:
from myapp.model.user import User
from formalchemy import FieldSet
from formalchemy import Grid
class UserFieldSet(FieldSet):
def __init__(self):
FieldSet.__init__(self, User)
self.configure()
class UserGrid(Grid):
def __init__(self):
Grid.__init__(self, User)
self.configure()
If I comment out the two classes above, formalchemy works fine. I can view Users and I can edit them.
When I put the two classes in I run into problems. The problem is pyramid_formalchemy grabs UserGrid and UserFieldSet from the module's namespace and then tries to use them as if they were instantiated classes. This breaks things. On the other hand if pyramid_formalchemy doesn't find the classes in will dynamically create the classes AND instantiate them. I believe the offending code is in pyramid_formalchemy/views.py, line 236 starting at the get_grid() function:
def get_grid(self):
"""return a Grid object"""
request = self.request
model_name = request.model_name
form_name = '%sGrid' % model_name
if hasattr(request.forms, form_name):
g = getattr(request.forms, form_name) <-- g is a class type not an
g.engine = g.engine or self.engine <-- instance!
g.readonly = True <-- why is it not instantiated?
g._request = self.request
self.update_grid(g)
return g
model = self.context.get_model() <-- UserGrid not found in faforms
grid = self.grid_class(model) <-- module.
grid.engine = self.engine <-- so a Grid is instantiated
if not isinstance(request.forms, list):
# add default grid to form module eg: caching
setattr(request.forms, form_name, grid)
grid = grid.copy()
grid._request = self.request
self.update_grid(grid)
return grid
Here you can see if the matching grid (or fieldset) is not found it will be instantiated, but if it is found the class type will be used directly, but not actually instantiated.
Any thoughts here? Am I setting something up wrong?
Basically I'm using CSRF tokens so I need to customize my forms to grab the tokens from the session.
Thanks.
pyramid_formalchemy makes certain, mostly undocumented assumptions, about how you setup your forms file. Here are the gotchas I hit and worked around...
If you have a model User for example, then you will need to have a FieldSet called UserFieldSet.
UserFieldSet must be an instance and not a class.
Be careful on your imports or you will break assumptions pyramid_formalchemy is making. If you have a model class User, import User's package, but not the User class itself. Then reference the class by prefixing the reference with the package name.
Below is a working example using the 3 points above.
from myapp.model import user
from formalchemy import Field
from formalchemy import FieldSet
from formalchemy import Grid
UserFieldSet = FieldSet(user.User)
UserFieldSet.configure()
UserGrid = Grid(user.User)
UserGrid.configure()
You can also post to the formalchemy group for more info.