Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ For example, the [Pyramid cookiecutter](https://github.com/Pylons/pyramid-cookie
uses `pyramid_tm` and `zope.sqlalchemy` to integrate SQLAlchemy into Pyramid.

Pyramid Tasks includes built-in support for pyramid_tm.
It can be enabled by including `pyramid_tasks.transaction` in your project.
It can be enabled by including `pyramid_tasks.contrib.pyramid_tm` in your project.
This must be included after Pyramid Tasks, but doesn't need to be included before pyramid_tm.

To see Pyramid Tasks, pyramid_tm, and SQLAlchemy in action, check out the [SQLAlchemy sample app](https://github.com/luhn/pyramid-tasks/tree/main/examples/sqlalchemy).
Expand Down
Empty file.
24 changes: 24 additions & 0 deletions pyramid_tasks/contrib/pyramid_tm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
Emulates ``pyramid_tm`` functionality inside of Celery.

"""


def includeme(config):
config.add_task_deriver(transaction_task_deriver)


def transaction_task_deriver(task, info):
def deriver(request, *args, **kwargs):
environ = request.environ
if environ.get("tm.active"):
return task(request, *args, **kwargs)

manager = request.tm
environ["tm.active"] = True
environ["tm.manager"] = manager

with manager:
return task(request, *args, **kwargs)

return deriver
24 changes: 8 additions & 16 deletions pyramid_tasks/transaction.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
"""
Support for transaction/pyramid_tm.
For backwards compatibility. For new proejcts, use
``pyramid_tasks.contrib.pyramid_tm`` instead.

"""


def includeme(config):
config.add_task_deriver(transaction_task_deriver)
from .contrib.pyramid_tm import transaction_task_deriver


def transaction_task_deriver(task, info):
def deriver(request, *args, **kwargs):
environ = request.environ
if environ.get("tm.active"):
return task(request, *args, **kwargs)

manager = request.tm
environ["tm.active"] = True
environ["tm.manager"] = manager
def includeme(config):
config.include(".contrib.pyramid_tm")

with manager:
return task(request, *args, **kwargs)

return deriver
__all__ = [
"transaction_task_deriver",
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from pyramid.testing import DummyRequest

from pyramid_tasks.transaction import transaction_task_deriver
from pyramid_tasks.contrib.pyramid_tm import transaction_task_deriver


class TestTransactionTastDeriver:
Expand Down
2 changes: 1 addition & 1 deletion tests/pkgs/transactionapp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def includeme(config):
}
)
config.include("pyramid_tm")
config.include("pyramid_tasks.transaction")
config.include("pyramid_tasks.contrib.pyramid_tm")

config.add_settings({"sqlalchemy.url": "sqlite:///db.sqlite3"})
dbengine = get_engine(config.get_settings())
Expand Down