Added forms.alchemy module and changed CRUD view to use it.

This commit is contained in:
Lance Edgar 2013-09-10 09:45:01 -07:00
parent 716f3cbd71
commit 73686b822f
3 changed files with 83 additions and 6 deletions

View file

@ -27,5 +27,6 @@ Forms
""" """
from .simpleform import * from .simpleform import *
from .alchemy import *
from .fields import * from .fields import *
from .renderers import * from .renderers import *

76
tailbone/forms/alchemy.py Normal file
View file

@ -0,0 +1,76 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2012 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
FormAlchemy Forms
"""
from edbob import Object
from pyramid.renderers import render
from ..db import Session
__all__ = ['AlchemyForm']
class AlchemyForm(Object):
"""
Form to contain a :class:`formalchemy.FieldSet` instance.
"""
create_label = "Create"
update_label = "Update"
allow_successive_creates = False
def __init__(self, request, fieldset, **kwargs):
super(AlchemyForm, self).__init__(**kwargs)
self.request = request
self.fieldset = fieldset
def _get_readonly(self):
return self.fieldset.readonly
def _set_readonly(self, val):
self.fieldset.readonly = val
readonly = property(_get_readonly, _set_readonly)
@property
def successive_create_label(self):
return "%s and continue" % self.create_label
def render(self, **kwargs):
kwargs['form'] = self
if self.readonly:
template = '/forms/form_readonly.mako'
else:
template = '/forms/form.mako'
return render(template, kwargs)
def save(self):
self.fieldset.sync()
Session.flush()
def validate(self):
self.fieldset.rebind(data=self.request.params)
return self.fieldset.validate()

View file

@ -26,15 +26,15 @@
CRUD View CRUD View
""" """
from .core import View
from pyramid.httpexceptions import HTTPFound from pyramid.httpexceptions import HTTPFound
import formalchemy from ..forms import AlchemyForm
from formalchemy import FieldSet
from edbob.pyramid.forms.formalchemy import AlchemyForm
from .core import View
from edbob.util import requires_impl, prettify
from ..db import Session from ..db import Session
from edbob.util import requires_impl, prettify
__all__ = ['CrudView'] __all__ = ['CrudView']
@ -74,7 +74,7 @@ class CrudView(View):
def make_fieldset(self, model, **kwargs): def make_fieldset(self, model, **kwargs):
kwargs.setdefault('session', Session()) kwargs.setdefault('session', Session())
kwargs.setdefault('request', self.request) kwargs.setdefault('request', self.request)
fieldset = formalchemy.FieldSet(model, **kwargs) fieldset = FieldSet(model, **kwargs)
fieldset.prettify = prettify fieldset.prettify = prettify
return fieldset return fieldset