Metadata-Version: 2.1
Name: django_soft_atomic
Version: 1.0.0
Summary: A more forgiving variation of `django`'s `atomic`, allowing you to pass some exceptions through atomic block without rollback.
Project-URL: Homepage, https://github.com/maniek2332/django_soft_atomic
Project-URL: Bug Tracker, https://github.com/maniek2332/django_soft_atomic/issues
Author-email: Mariusz Okulanis <mariusz.okulanis@gmail.com>
License: MIT License
        
        Copyright (c) 2022 Mariusz Okulanis
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.6
Description-Content-Type: text/markdown

# 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.12+

## Installation

### With PIP

Execute: `pip install django-soft-atomic`

### Manual

Copy `django_soft_atomic.py` to your codebase and simply start using it.

## Usage (docs)

This "package" constists of single function/decorator/context-manager:

`soft_atomic(using=None, savepoint=True, durable=False, *, safe_exceptions=(Exception,))`

 * `using` (default Django parameter) - database name to use,
 * `savepoint` (default Django parameter) - TODO,
 * `durable` (default Django parameter) - TODO,
 * `safe_exceptions` - collection (e.g. `tuple`) of exceptions which are allowed to pass through `soft_atomic` block without rollback. Typical DB errors (like `IntegrityError`) will still throw. Defaults to: `(Exception,)`.

## Example

```
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
```
