def create(self): request = self.request form = self.bake_form(self.formClass(request.POST)) _object = self.modelClass() message = None if request.method == 'POST': if form.validate(): self.populate_model(form, _object) with transaction.manager: try: DBSession.add(_object) except SQLAlchemyError: message = SQLERROR_MESSAGE else: message = UPDATED_MESSAGE else: message = FORMERRORS_MESSAGE _dict = { 'form' : form, 'save_url' : request.resource_url(request.root, self.baseUrl , 'create'), 'message' : message, 'user' : authenticated_userid(request) } return self.bake_dict(_dict)
def setUp(self): self.config = testing.setUp() from sqlalchemy import create_engine engine = create_engine('sqlite://') from .models import ( Base, MyModel, ) DBSession.configure(bind=engine) Base.metadata.create_all(engine) with transaction.manager: model = MyModel(name='one', value=55) DBSession.add(model)
def update(self): request = self.request _id = request.matchdict['id'] _object = DBSession.query(self.modelClass).get(_id) message = None if _object is not None: form = self.bake_form(self.formClass(request.POST, _object)) if request.method == 'POST': if form.validate(): try: self.populate_model(form, _object) except SQLAlchemyError: message = SQLERROR_MESSAGE else: message = UPDATED_MESSAGE else: message = FORMERRORS_MESSAGE _dict = { 'form' : form, 'save_url' : request.resource_url(request.root, self.baseUrl , 'update', _id), 'message' : message, 'user' : authenticated_userid(request) } return self.bake_dict(_dict) raise HTTPNotFound()
def main(global_config, **settings): """ This function returns a Pyramid WSGI application. """ engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) Base.metadata.bind = engine authentication_policy = AuthTktAuthenticationPolicy(settings['auth.secret'], callback = groupfinder) authorization_policy = ACLAuthorizationPolicy() config = Configurator(settings = settings, authentication_policy = authentication_policy, authorization_policy = authorization_policy, root_factory = EntryFactory) config.include('pyramid_jinja2') config.add_renderer('.html', 'pyramid_jinja2.renderer_factory') config.add_static_view('static', 'static', cache_max_age=3600) config.include(addroutes) config.scan() return config.make_wsgi_app()
def tearDown(self): DBSession.remove() testing.tearDown()