django-soft-atomic
A more forgiving variation of django's atomic, allowing you to pass some
exceptions through atomic block without rollback.
Rationale
In big applications you may end up relying on exceptions mechanism to pass information
about failure up the stack. Unfortunately, if your business logic involves operations on
database, there is no easy way to wind up execution through atomic block without
rolling back entire transaction. django-soft-atomic tries to solves this problem
by allowing certain exceptions to exit atomic block just like sucessful execution
(still maintaining the raised exception).
Requirements
- Python 3.6+
- Django 3.2+
Installation
With PIP
Execute: pip install django_soft_atomic
See also: PyPI Page
Manual
Copy django_soft_atomic.py to your codebase and simply start using it.
Usage (docs)
This "package" constists of single decorator/context-manager, acting as replacement for django's atomic:
soft_atomic(using=None, savepoint=True, durable=False, *, safe_exceptions=(Exception,))
using- database name to use (same as original atomic),savepoint- disable usage of savepoints in inner blocks (same as original atomic),durable- ensure this is outermost block (same as original atomic),safe_exceptions- collection (e.g.tuple) of exceptions which are allowed to pass throughsoft_atomicblock without rollback. Typical DB errors (likeIntegrityError) will still throw. Defaults to:(Exception,).
Example
Let's take a simple example, where we would like to perform payment operation and raise an exception if it fails. We want to create a database entry for both outcomes.
from django_soft_atomic import soft_atomic
class PaymentProcessingException(Exception):
pass
class PaymentRequest(models.Model):
payment_id = models.TextField()
success = models.BooleanField()
@soft_atomic(safe_exceptions=(PaymentProcessingException,))
def process_payment(payment_details):
payment_id, success = payment_gateway.process_payment(payment_details)
PaymentRequest.objects.create(payment_id=payment_id, success=success)
if not success:
raise PaymentProcessingException("Payment was not sucessful")
def payment_endpoint(payment_details):
try:
process_payment(payment_details)
except PaymentProcessingException:
... # handle a failure
else:
... # payment was successful
# in either case the `PaymentRequest` record was created in the database
Metadata
Release files for django_soft_atomic 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_soft_atomic-1.1.0.tar.gz | 3.5 kB | Without provenance | Details for django_soft_atomic-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_soft_atomic-1.1.0-py3-none-any.whl | Python 3 | none | any | Details for django_soft_atomic-1.1.0-py3-none-any.whl |
Total release size: 8.1 kB
Release files / django_soft_atomic-1.1.0.tar.gz
| Download URL | django_soft_atomic-1.1.0.tar.gz |
|---|---|
| Size | 3.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
09ffa8825a0364308e39b9b954503a17cf453b48e17873407200fe44205fc5b4
|
|
BLAKE2b-256 checksum How to use checksums |
f440cf6f4a9c29e48821d60577146ef820e306e40f22ed13c68c0c2c700e3c2c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.23.0
|
Release files / django_soft_atomic-1.1.0-py3-none-any.whl
| Download URL | django_soft_atomic-1.1.0-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
efe239c049d241f9681de5e792e86712bf35864360718d222f0839b2d3c24b90
|
|
BLAKE2b-256 checksum How to use checksums |
bcd91592810d4cc08f4781ff38f96f2f9c771786e0a08c36b5c2d7af35db4cb2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.23.0
|
Security for django_soft_atomic 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.