-
Notifications
You must be signed in to change notification settings - Fork 887
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
mmerickel
merged 47 commits into
Pylons:feature/alchemy-scaffold-update
from
mmerickel:feature/alchemy-scaffold-update-tweaks
Feb 18, 2016
Merged
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 f4e1ca8
rename dbmaker to session_factory
mmerickel 0e21c2a
make models/__init__.py a template
mmerickel 35dc507
update source for basiclayout
mmerickel 9b83981
fix the Base import
mmerickel 8ec0b01
unindent literalincludes
mmerickel 7b89a7e
update basiclayout prose
mmerickel 21d69fd
link to create_all sqla method
mmerickel ab68f1f
minor grammar and punctuation tweaks, break up run-on sentences.
stevepiercy d1cb346
assume the user is in the tutorial folder
mmerickel 8d45715
reference addon links for pyramid_jinja2, pyramid_tm, zope.sqlalchemy…
mmerickel d6243ac
update definingmodels chapter of wiki2 tutorial
mmerickel 4b23c9f
update definingviews chapter of wiki2 tutorial
mmerickel 14cff75
update authorization chapter of wiki2 tutorial
mmerickel 4337a25
minor fixes to wiki2 distributing chapter
mmerickel 0b02e46
expose the session factory on the registry
mmerickel 1c10801
[wip] update tests in wiki2 tutorial
mmerickel ed218d3
Merge pull request #2 from stevepiercy/mmerickel-feature/alchemy-scaf…
mmerickel d6a758e
fix tests to get the bind from dbsession_factory properly
mmerickel 91ffcca
fix jinja2 none test
mmerickel 62f0411
fix functional tests
mmerickel ff88c15
split login from forbidden
mmerickel 07d38f5
several simple refactorings
mmerickel e01b270
explain the base layout.jinja2 template and notfound view
mmerickel 9a7cfe3
update 404 templates
mmerickel f2e9c68
move security into one place
mmerickel cb5a848
copy layout and templates from views to authorization
mmerickel 81e5989
create an actual user model to prepare for security
mmerickel e6e4f65
let's go ahead and bite off more than we can chew by adding object-se…
mmerickel 574ba1a
update the models chapter with the new user model
mmerickel a115c6d
add the bcrypt dependency
mmerickel 4872a1e
forward port changes to models / scripts to later chapters
mmerickel 23c2d7b
update the views/models with setup.py develop
mmerickel 60891b8
improve the views section by removing quirks and explaining transactions
mmerickel 4c391c5
fix syntax
mmerickel bca6c99
highlight more appropriate lines in views
mmerickel 42c9316
fix unicode issues with check_password
mmerickel da5ebc2
split routes into a separate module
mmerickel 00b2c69
implement the authentication example code
mmerickel 659a254
add a new authentication chapter
mmerickel 38b4076
use page.name to prepare for context
mmerickel f2c4368
remove whitespace
mmerickel 2fa9046
add first cut at source for authorization chapter
mmerickel 9e85d2b
update the authorization chapter
mmerickel 91f7ed4
add webtest and tests_require to setup.py
mmerickel 50e08a7
add fallback for next_url
mmerickel 66fabb4
update tests chapter
mmerickel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ofdbmaker
? Ditto in other places wheredbmaker
is used.