Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

object-level security and tons of other small improvements to the wiki2 tutorial #2334

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
a6d08e1
improve the models api/usage and add a lot of comments
mmerickel Feb 4, 2016
f4e1ca8
rename dbmaker to session_factory
mmerickel Feb 5, 2016
0e21c2a
make models/__init__.py a template
mmerickel Feb 5, 2016
35dc507
update source for basiclayout
mmerickel Feb 5, 2016
9b83981
fix the Base import
mmerickel Feb 5, 2016
8ec0b01
unindent literalincludes
mmerickel Feb 5, 2016
7b89a7e
update basiclayout prose
mmerickel Feb 5, 2016
21d69fd
link to create_all sqla method
mmerickel Feb 5, 2016
ab68f1f
minor grammar and punctuation tweaks, break up run-on sentences.
stevepiercy Feb 5, 2016
d1cb346
assume the user is in the tutorial folder
mmerickel Feb 7, 2016
8d45715
reference addon links for pyramid_jinja2, pyramid_tm, zope.sqlalchemy…
mmerickel Feb 7, 2016
d6243ac
update definingmodels chapter of wiki2 tutorial
mmerickel Feb 7, 2016
4b23c9f
update definingviews chapter of wiki2 tutorial
mmerickel Feb 8, 2016
14cff75
update authorization chapter of wiki2 tutorial
mmerickel Feb 8, 2016
4337a25
minor fixes to wiki2 distributing chapter
mmerickel Feb 8, 2016
0b02e46
expose the session factory on the registry
mmerickel Feb 8, 2016
1c10801
[wip] update tests in wiki2 tutorial
mmerickel Feb 8, 2016
ed218d3
Merge pull request #2 from stevepiercy/mmerickel-feature/alchemy-scaf…
mmerickel Feb 9, 2016
d6a758e
fix tests to get the bind from dbsession_factory properly
mmerickel Feb 9, 2016
91ffcca
fix jinja2 none test
mmerickel Feb 9, 2016
62f0411
fix functional tests
mmerickel Feb 9, 2016
ff88c15
split login from forbidden
mmerickel Feb 11, 2016
07d38f5
several simple refactorings
mmerickel Feb 11, 2016
e01b270
explain the base layout.jinja2 template and notfound view
mmerickel Feb 11, 2016
9a7cfe3
update 404 templates
mmerickel Feb 11, 2016
f2e9c68
move security into one place
mmerickel Feb 11, 2016
cb5a848
copy layout and templates from views to authorization
mmerickel Feb 12, 2016
81e5989
create an actual user model to prepare for security
mmerickel Feb 12, 2016
e6e4f65
let's go ahead and bite off more than we can chew by adding object-se…
mmerickel Feb 12, 2016
574ba1a
update the models chapter with the new user model
mmerickel Feb 12, 2016
a115c6d
add the bcrypt dependency
mmerickel Feb 12, 2016
4872a1e
forward port changes to models / scripts to later chapters
mmerickel Feb 12, 2016
23c2d7b
update the views/models with setup.py develop
mmerickel Feb 12, 2016
60891b8
improve the views section by removing quirks and explaining transactions
mmerickel Feb 13, 2016
4c391c5
fix syntax
mmerickel Feb 14, 2016
bca6c99
highlight more appropriate lines in views
mmerickel Feb 14, 2016
42c9316
fix unicode issues with check_password
mmerickel Feb 14, 2016
da5ebc2
split routes into a separate module
mmerickel Feb 14, 2016
00b2c69
implement the authentication example code
mmerickel Feb 14, 2016
659a254
add a new authentication chapter
mmerickel Feb 16, 2016
38b4076
use page.name to prepare for context
mmerickel Feb 17, 2016
f2c4368
remove whitespace
mmerickel Feb 17, 2016
2fa9046
add first cut at source for authorization chapter
mmerickel Feb 17, 2016
9e85d2b
update the authorization chapter
mmerickel Feb 18, 2016
91f7ed4
add webtest and tests_require to setup.py
mmerickel Feb 18, 2016
50e08a7
add fallback for next_url
mmerickel Feb 18, 2016
66fabb4
update tests chapter
mmerickel Feb 18, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyramid/scaffolds/alchemy/+package+/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def main(global_config, **settings):
"""
config = Configurator(settings=settings)
config.include('pyramid_jinja2')
config.include('.models.meta')
config.include('.models')
config.add_static_view('static', 'static', cache_max_age=3600)
config.add_route('home', '/')
config.scan()
Expand Down
71 changes: 68 additions & 3 deletions pyramid/scaffolds/alchemy/+package+/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,72 @@
from sqlalchemy import engine_from_config
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import configure_mappers
# import all models classes here for sqlalchemy mappers
# to pick up
import zope.sqlalchemy

# import or define all models here to ensure they are attached to the
# Base.metadata prior to any initialization routines
from .mymodel import MyModel # flake8: noqa

# run configure mappers to ensure we avoid any race conditions
# run configure_mappers after defining all of the models to ensure
# all relationships can be setup
configure_mappers()


def get_engine(settings, prefix='sqlalchemy.'):
return engine_from_config(settings, prefix)


def get_sessionmaker(engine):
dbmaker = sessionmaker()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for nitpicking but I'm always confused by such aliases. Why not call it session_maker instead of dbmaker? Ditto in other places where dbmaker is used.

dbmaker.configure(bind=engine)
return dbmaker


def get_tm_session(dbmaker, transaction_manager):
"""
Get a ``sqlalchemy.orm.Session`` instance backed by a transaction.

This function will hook the session to the transaction manager which
will take care of committing any changes.

- When using pyramid_tm it will automatically be committed or aborted
depending on whether an exception is raised.

- When using scripts you should wrap the session in a manager yourself.
For example::

import transaction

engine = get_engine(settings)
dbmaker = get_sessionmaker(engine)
with transaction.manager:
dbsession = get_tm_session(dbmaker, transaction.manager)

"""
dbsession = dbmaker()
zope.sqlalchemy.register(
dbsession, transaction_manager=transaction_manager)
return dbsession


def includeme(config):
"""
Initialize the model for a Pyramid app.

Activate this setup using ``config.include('{{package}}.models')``.

"""
settings = config.get_settings()

# use pyramid_tm to hook the transaction lifecycle to the request
config.include('pyramid_tm')

dbmaker = get_sessionmaker(get_engine(settings))

# make request.dbsession available for use in Pyramid
config.add_request_method(
# r.tm is the transaction manager used by pyramid_tm
lambda r: get_tm_session(dbmaker, r.tm),
'dbsession',
reify=True
)
33 changes: 0 additions & 33 deletions pyramid/scaffolds/alchemy/+package+/models/meta.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from sqlalchemy import engine_from_config
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.schema import MetaData
import zope.sqlalchemy

# Recommended naming convention used by Alembic, as various different database
# providers will autogenerate vastly different names making migrations more
Expand All @@ -17,33 +14,3 @@

metadata = MetaData(naming_convention=NAMING_CONVENTION)
Base = declarative_base(metadata=metadata)


def includeme(config):
settings = config.get_settings()
dbmaker = get_dbmaker(get_engine(settings))

config.add_request_method(
lambda r: get_session(r.tm, dbmaker),
'dbsession',
reify=True
)

config.include('pyramid_tm')


def get_session(transaction_manager, dbmaker):
dbsession = dbmaker()
zope.sqlalchemy.register(dbsession,
transaction_manager=transaction_manager)
return dbsession


def get_engine(settings, prefix='sqlalchemy.'):
return engine_from_config(settings, prefix)


def get_dbmaker(engine):
dbmaker = sessionmaker()
dbmaker.configure(bind=engine)
return dbmaker
3 changes: 2 additions & 1 deletion pyramid/scaffolds/alchemy/+package+/models/mymodel.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from .meta import Base
from sqlalchemy import (
Column,
Index,
Integer,
Text,
)

from .meta import Base


class MyModel(Base):
__tablename__ = 'models'
Expand Down
9 changes: 5 additions & 4 deletions pyramid/scaffolds/alchemy/+package+/scripts/initializedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

from ..models import (
Base,
get_session,
get_engine,
get_dbmaker,
get_sessionmaker,
get_tm_session,
)
from ..models import MyModel

Expand All @@ -36,9 +36,10 @@ def main(argv=sys.argv):
engine = get_engine(settings)
Base.metadata.create_all(engine)

dbmaker = get_dbmaker(engine)
dbsession = get_session(transaction.manager, dbmaker)
dbmaker = get_sessionmaker(engine)

with transaction.manager:
dbsession = get_tm_session(dbmaker, transaction.manager)

model = MyModel(name='one', value=1)
dbsession.add(model)
8 changes: 4 additions & 4 deletions pyramid/scaffolds/alchemy/+package+/tests.py_tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class BaseTest(unittest.TestCase):
settings = self.config.get_settings()

from .models import (
get_session,
get_engine,
get_dbmaker,
get_sessionmaker,
get_tm_session,
)

self.engine = get_engine(settings)
dbmaker = get_dbmaker(self.engine)
dbmaker = get_sessionmaker(self.engine)

self.session = get_session(transaction.manager, dbmaker)
self.session = get_tm_session(dbmaker, transaction.manager)

def init_database(self):
from .models import Base
Expand Down