Commit 91ca258f authored by yorn's avatar yorn

Install pyramid_crud

parent cd29d00a
from pyramid.config import Configurator
from sqlalchemy import engine_from_config
from pyramid.session import SignedCookieSessionFactory
from pyramid.events import subscriber, NewRequest
from .models import (
DBSession,
Base,
)
COOKIE_SECRET='ThV1DNsNLgU7viLKmwe3Gg8qTcnbGuaN'
@subscriber(NewRequest)
def new_request(event):
request = event.request
request.dbsession = DBSession
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine)
my_session_factory = SignedCookieSessionFactory(
COOKIE_SECRET,
cookie_name='crud', timeout=28800
)
Base.metadata.bind = engine
config = Configurator(settings=settings)
config = Configurator(settings=settings, session_factory=my_session_factory)
config.include('pyramid_chameleon')
config.add_static_view('static', 'static', cache_max_age=3600)
config.add_route('home', '/')
......
from pyramid.response import Response
from pyramid.view import view_config
from pyramid_crud.forms import CSRFModelForm
from pyramid_crud.views import CRUDView
from sqlalchemy.exc import DBAPIError
......@@ -9,28 +11,11 @@ from .models import (
)
@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(request):
try:
one = DBSession.query(MyModel).filter(MyModel.name == 'one').first()
except DBAPIError:
return Response(conn_err_msg, content_type='text/plain', status_int=500)
return {'one': one, 'project': 'crud'}
class MyModelForm(CSRFModelForm):
class Meta:
model = MyModel
conn_err_msg = """\
Pyramid is having a problem using your SQL database. The problem
might be caused by one of the following things:
1. You may need to run the "initialize_crud_db" script
to initialize your database tables. Check your virtual
environment's "bin" directory for this script and try to run it.
2. Your database server may not be running. Check that the
database server referred to by the "sqlalchemy.url" setting in
your "development.ini" file is running.
After you fix the problem, please restart the Pyramid application to
try it again.
"""
class MyModelView(CRUDView):
Form = MyModelForm
url_path = '/mymodel'
......@@ -14,6 +14,7 @@ pyramid.default_locale_name = en
pyramid.includes =
pyramid_debugtoolbar
pyramid_tm
pyramid_crud
sqlalchemy.url = sqlite:///%(here)s/crud.sqlite
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment