A powerful and flexible hook system for Django applications
Project description
Django Hook Package
A powerful and flexible hook system for Django that allows applications to implement and invoke hooks, with results aggregation.
Features
- Modular: Each app can define its own hooks
- Error-resistant: Errors in one hook don't stop other hooks from executing
- Flexible: Supports different types of result aggregation
- Simple: Clean and understandable API
- Well-documented: Complete comments and type hints
Installation
- Add
django_hookto your Django project - Add to
INSTALLED_APPSin yoursettings.py:
INSTALLED_APPS = [
# ...
'django_hook',
# your other apps
]
Usage
Defining Hooks
In your app, create a hooks.py file and define hooks using the @hook decorator:
# app1/django_hook.py
from django_hook import hook
@hook('user_created')
def handle_user_created(user):
# Send welcome email
print(f"Welcome email sent to {user.email}")
return {"status": "email_sent"}
@hook('user_created')
def log_user_creation(user):
# Log user creation
print(f"User {user.username} created")
return {"status": "logged"}
Invoking Hooks
Invoke hooks from anywhere in your code:
from django_hook import HookSystem
from django_hook.utils import aggregate_dict
def create_user_view(request):
# Create user logic here
user = create_user(request.POST)
# Invoke hook and get all results
results = HookSystem.invoke('user_created', user)
print(results) # [{'status': 'email_sent'}, {'status': 'logged'}]
# Or with aggregation
aggregated = HookSystem.invoke_aggregate(
'user_created',
aggregate_dict,
user
)
print(aggregated) # Merged dictionary
Available Aggregators
The package includes several built-in aggregators:
aggregate_sum()- Sums numerical resultsaggregate_list()- Flattens list resultsaggregate_dict()- Merges dictionary resultsaggregate_first_non_none()- Returns first non-None resultaggregate_all()- Returns all results as a list
API Reference
HookSystem Class
HookSystem.invoke(hook_name, *args, **kwargs)- Invoke a hookHookSystem.invoke_aggregate(hook_name, aggregator, *args, **kwargs)- Invoke with custom aggregationHookSystem.get_hook_implementations(hook_name)- Get all hook implementationsHookSystem.register_hook(hook_name, hook_func, app_name)- Manually register a hook
Decorators
@hook(hook_name)- Decorator to register functions as hooks@register_hook(hook_name, app_name)- Alternative registration decorator
Example Use Cases
- User lifecycle events (user_created, user_updated, user_deleted)
- Content moderation (before_publish, after_publish)
- Notification systems (send_notification)
- Data validation (validate_data)
- Search index updates (update_search_index)
Error Handling
The hook system is designed to be fault-tolerant. If one hook implementation fails, the error is logged but other hooks continue to execute.
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
License
MIT License - feel free to use this package in your commercial projects.
Support
If you have any questions or issues, please create an issue in the GitHub repository.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_hook-1.0.0.tar.gz.
File metadata
- Download URL: django_hook-1.0.0.tar.gz
- Upload date:
- Size: 20.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
40e29afca18f1ad1ec2eb8957610a8ff7e6b66a838a64d965645d9d27f18f412
|
|
| MD5 |
2600e5a764f97d8a72be2f7b27b97a43
|
|
| BLAKE2b-256 |
d1f6446a6fc330d0e3ad8b5d409713472446620fdfc693e386b30ba38a520b34
|
File details
Details for the file django_hook-1.0.0-py3-none-any.whl.
File metadata
- Download URL: django_hook-1.0.0-py3-none-any.whl
- Upload date:
- Size: 18.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0b96fa2e9319ffaa550376feb941778ad1a455121155418ea4e68d102064890
|
|
| MD5 |
2d6876924d599619741fd26f26e3d40f
|
|
| BLAKE2b-256 |
69814fb937ba690e427dcd595491c2aa2fc46aff6ea4074faffeb4e30bafafb5
|