Commit 91ca258f authored by yorn's avatar yorn

Install pyramid_crud

parent cd29d00a
from pyramid.config import Configurator from pyramid.config import Configurator
from sqlalchemy import engine_from_config from sqlalchemy import engine_from_config
from pyramid.session import SignedCookieSessionFactory
from pyramid.events import subscriber, NewRequest
from .models import ( from .models import (
DBSession, DBSession,
Base, Base,
) )
COOKIE_SECRET='ThV1DNsNLgU7viLKmwe3Gg8qTcnbGuaN'
@subscriber(NewRequest)
def new_request(event):
request = event.request
request.dbsession = DBSession
def main(global_config, **settings): def main(global_config, **settings):
""" This function returns a Pyramid WSGI application. """ This function returns a Pyramid WSGI application.
""" """
engine = engine_from_config(settings, 'sqlalchemy.') engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine) DBSession.configure(bind=engine)
my_session_factory = SignedCookieSessionFactory(
COOKIE_SECRET,
cookie_name='crud', timeout=28800
)
Base.metadata.bind = engine Base.metadata.bind = engine
config = Configurator(settings=settings) config = Configurator(settings=settings, session_factory=my_session_factory)
config.include('pyramid_chameleon') config.include('pyramid_chameleon')
config.add_static_view('static', 'static', cache_max_age=3600) config.add_static_view('static', 'static', cache_max_age=3600)
config.add_route('home', '/') config.add_route('home', '/')
......
from pyramid.response import Response from pyramid.response import Response
from pyramid.view import view_config from pyramid.view import view_config
from pyramid_crud.forms import CSRFModelForm
from pyramid_crud.views import CRUDView
from sqlalchemy.exc import DBAPIError from sqlalchemy.exc import DBAPIError
...@@ -9,28 +11,11 @@ from .models import ( ...@@ -9,28 +11,11 @@ from .models import (
) )
@view_config(route_name='home', renderer='templates/mytemplate.pt') class MyModelForm(CSRFModelForm):
def my_view(request): class Meta:
try: model = MyModel
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 MyModelView(CRUDView):
conn_err_msg = """\ Form = MyModelForm
Pyramid is having a problem using your SQL database. The problem url_path = '/mymodel'
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.
"""
...@@ -14,6 +14,7 @@ pyramid.default_locale_name = en ...@@ -14,6 +14,7 @@ pyramid.default_locale_name = en
pyramid.includes = pyramid.includes =
pyramid_debugtoolbar pyramid_debugtoolbar
pyramid_tm pyramid_tm
pyramid_crud
sqlalchemy.url = sqlite:///%(here)s/crud.sqlite 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