Metadata-Version: 2.4
Name: django-lifecycle-hooks
Version: 1.0.2
Summary: A next-generation, high-performance declarative lifecycle hooks library for Django 5.x+
Project-URL: Homepage, https://github.com/jotauses/django-lifecycle-hooks
Project-URL: Bug Tracker, https://github.com/jotauses/django-lifecycle-hooks/issues
Author-email: Joaquín Vázquez <jota.dev.es@gmail.com>
License: MIT License
        
        Copyright (c) 2025 Joaquín Vázquez
        
        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: Development Status :: 5 - Production/Stable
Classifier: Framework :: Django
Classifier: Framework :: Django :: 4.2
Classifier: Framework :: Django :: 5.0
Classifier: Framework :: Django :: 5.1
Classifier: Framework :: Django :: 5.2
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
Requires-Python: >=3.10
Requires-Dist: django>=4.2
Description-Content-Type: text/markdown

# Django Lifecycle Hooks

**The high-performance, zero-overhead alternative to Django Signals.**

[![PyPI version](https://img.shields.io/pypi/v/django-lifecycle-hooks.svg?color=2c3e50&labelColor=ecf0f1)](https://pypi.org/project/django-lifecycle-hooks/)
[![Python Versions](https://img.shields.io/pypi/pyversions/django-lifecycle-hooks.svg?color=2c3e50&labelColor=ecf0f1)](https://pypi.org/project/django-lifecycle-hooks/)
[![Django Versions](https://img.shields.io/pypi/djversions/django-lifecycle-hooks.svg?color=2c3e50&labelColor=ecf0f1)](https://pypi.org/project/django-lifecycle-hooks/)
[![License: MIT](https://img.shields.io/badge/License-MIT-2c3e50.svg?labelColor=ecf0f1)](https://opensource.org/licenses/MIT)

---

## ⚡ Why this library?

**Stop slowing down your Django app with inefficient signals.**

Most lifecycle libraries (and Django's own signals) rely on heavy runtime introspection, full object copying, and loose coupling that makes debugging a nightmare.

**Django Lifecycle Hooks** is engineered for **performance-critical** applications:

- **🚀 Zero Runtime Overhead**: Hook resolution happens **once** at import time. Executing hooks is an **O(1)** operation.
- **⚡ First-Class Async & ASGI Support**: The **only** lifecycle library with native `asave` and `acreate` support. We don't just wrap sync code; we await your async hooks properly.
- **💾 Sparse Snapshotting**: We don't copy your entire model. If you only watch `status`, we only cache `status`. Your memory usage stays flat.
- **🛡️ Type-Safe & Modern**: Built for **Python 3.14+** and **Django 5.2+**. Fully typed, `__slots__` optimized, and ready for strict MyPy validation.
- **✨ Developer Joy**: No more hunting for `@receiver` in random files. Logic lives where it belongs: **inside your model** or **specialized hooks**.

---

## 🏎️ Performance Comparison

| Feature | Standard Signals / Legacy Libs | Django Lifecycle Hooks |
| :--- | :--- | :--- |
| **Hook Resolution** | Runtime Introspection (Slow) | **Import-time Registry (Instant)** |
| **Change Detection** | Full `__dict__` copy (High RAM) | **Sparse Field Copy (Low RAM)** |
| **Lookup Speed** | O(n) Listener Loop | **O(1) Direct Dispatch** |
| **Async / ASGI** | ❌ None or Hacky Wrappers | **✅ Native `asave` & `acreate` Support** |

---

## 🚀 Quick Start

### 1. Install
```bash
pip install django-lifecycle-hooks
```

### 2. Write Elegant Code
Inherit from `LifecycleModelMixin` and declare your logic.

```python
from django.db import models
from django_lifecycle_hooks import LifecycleModelMixin, hook, HookType

class UserAccount(LifecycleModelMixin, models.Model):
    username = models.CharField(max_length=100)
    status = models.CharField(max_length=20, default="active")
    
    # ✅ 1. Simple & Fast
    @hook(HookType.BEFORE_SAVE)
    def clean_username(self):
        self.username = self.username.lower()

    # ✅ 2. Conditional & Declarative
    # Only runs when status changes to 'banned'. 
    # No "if" checks needed inside your method.
    @hook(HookType.AFTER_UPDATE, when="status", was="active", is_now="banned")
    def on_ban(self):
        self.ban_related_assets()

    # ✅ 3. Transaction Safe
    # Only fires after the DB transaction commits successfully.
    @hook(HookType.AFTER_SAVE, on_commit=True)
    def send_welcome_email(self):
        send_email_task.delay(self.id)
```

---

## 📚 Documentation

- [**Documentation**](https://jotauses.github.io/django-lifecycle-hooks/): Master advanced patterns (Async, Stacked Hooks, Conditions).

---
*Built for engineers who care about speed, clean code, and type safety ♥️*
