Pylons / pyramid-cookiecutter-starter

A Cookiecutter (project template) for creating a Pyramid starter project with choices for template language (Jinja2, Chameleon, or Mako), persistent backend (none, SQLAlchemy with SQLite, or ZODB), and mapping of URLs to routes (URL dispatch or traversal)
95 stars 53 forks source link

Extend SQLAlchemy model to stash the current Request #111

Closed jvanasco closed 3 years ago

jvanasco commented 3 years ago

This PR extends SQLAlchemy model to stash the current Request into the Session's "info" dict.

To maintain backwards compatibility, the request is submitted as an optional kwarg. IMHO it is best supplied as the first argument - but I didn't want to restructure things too much.

This change allows for SQLAlchemy objects to access the active Pyramid request object. Without this change, the methods of SQLAlchemy objects must be explicitly passed the current Request - which means a @property or @reify decorated method cannot access this information, unless the dangerousget_current_request is used or developers invest a lot of work in somehow populating this information during various hooks.

jvanasco commented 3 years ago

@stevepiercy I tried rewriting most of this for clarity, while using your preferred syntax. I hope this is approaching something better - and apologies if this needs more work.

I added a python codeblock to the existing sample code as well. I tried testing nested codeblocks under bullets, but that did not work well in most preview systems. I then tried unnesting them - and while the generated text does not show the nesting, the rendered formats I tried all show what was intended.

jvanasco commented 3 years ago

I think I got it. My big issue was not understanding the nuance of :: vs :. Usually I use :: without defining the code style, but using it seemed to be somewhat incompatible with the code style.

I was indeed trying to nest the code blocks under the list items.

stevepiercy commented 3 years ago

There was a typo in the SQLAlchemy docs URL. I pushed a commit to your branch to fix it.

:: highlights the subsequent code block to the default language specified in conf.py, else Sphinx uses Python, but only if the code is valid Python. Using code-block and specifying the language ensures that blocks render with the syntax highlighting you expect, else fails and complains loudly.

stevepiercy commented 3 years ago

This LGTM. I've asked for a review from @mmerickel. Upon merge, I will run through the Pyramid docs on master to update source files in tutorials.

mmerickel commented 3 years ago

Thanks, this is a great pattern.

stevepiercy commented 3 years ago

Thank you!