django-generic-tasks
Active Job for Django
Example usage
Define tasks in a tasks.py module within a Django app:
from django.core.mail import send_mail
from pydantic import BaseModel
import django_generic_tasks as tasks
# define task params as a Pydantic BaseModel
class EmailNotificationParams(BaseModel):
subject: str
content: str
recipients: list[str]
# subclass Task and specify params type as a generic type argument and implement the run method
class EmailNotificationTask(tasks.Task[EmailNotificationParams]):
def run(self):
send_mail(
subject=self.params.subject,
message=self.params.content,
from_email=None,
recipient_list=self.params.recipients,
)
if __name__ == "__main__":
params = EmailNotificationParams(
subject="Hello",
content="Have a nice day",
recipients=["alice@example.com", "bob@example.com"],
)
task = EmailNotificationTask(params)
# run a task synchronously
task.run()
# run a task asynchronously using settings.TASKS_BACKEND
task.start()
Registering tasks
Similar to signals, tasks have to be implicitly registered by ensuring they are imported during application startup. This can be done in the ready method in an application’s AppConfig.
from django.apps import AppConfig
class MyAppConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "my_app"
def ready(self):
from . import tasks # noqa: F401
HTTP endpoints
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("admin/", admin.site.urls),
path("tasks/", include("django_generic_tasks.urls")),
]
django-generic-tasks uses django-ninja to automatically expose API endpoints for running tasks. Each defined task gets its own API endpoint and uses the specified Pydantic BaseModel for parameter verification.
Supported backends
ThreadingBackend
Runs tasks in a new threading.Thread.
CloudTasksBackend
Runs tasks using Cloud Tasks HTTP Target tasks.
Configuration
TASKS_API_AUTHENTICATION
Specifies the Python path to a function or class whose instances are callable to use to authenticate incoming task API calls. The function or callable class should accept a single parameter request representing the incoming request.
Type: Optional[str]
Default: django_generic_tasks.security.BasicAuth
Examples:
my_app/authentication.py
# function
def authenticate(request):
"""Only allow requests from localhost"""
return request.META["REMOTE_ADDR"] == "127.0.0.1"
# callable class instance
class Authenticator:
def __call__(self, request):
"""Only allow requests from localhost"""
return request.META["REMOTE_ADDR"] == "127.0.0.1"
my_app/settings.py
TASKS_API_AUTHENTICATION = "my_app.authentication.authenticate" # or TASKS_API_AUTHENTICATION = "my_app.authentication.Authenticator"
Built-in authentication methods:
django_generic_tasks.security.GoogleOIDCAuth - Enforces that incoming requests contain a Google-issued OIDC token in the authorization header. This can be automatically added to requests from Cloud Tasks and Cloud Scheduler.
django_generic_tasks.security.BasicAuth - Authenticates basic auth credentials using the Django authentication system
django_generic_tasks.security.NoAuth - No authentication, useful for development.
TASKS_BACKEND
The default backend to use to run tasks asynchronously.
Type: any class which implements the django_generic_tasks.backends.Backend protocol
Metadata
Release files for django-generic-tasks 0.5.1
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-generic-tasks-0.5.1.tar.gz | 5.8 kB | Without provenance | Details for django-generic-tasks-0.5.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_generic_tasks-0.5.1-py3-none-any.whl | Python 3 | none | any | Details for django_generic_tasks-0.5.1-py3-none-any.whl |
Total release size: 12.2 kB
Release files / django-generic-tasks-0.5.1.tar.gz
| Download URL | django-generic-tasks-0.5.1.tar.gz |
|---|---|
| Size | 5.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bfe6fee81c03a174d0d1881a15edf6a6d760ea3dde10745b938f21ac88717f9f
|
|
BLAKE2b-256 checksum How to use checksums |
4f1cdd8d917de63adae81a0edc3932c54c936a6d2983093ea9bb3f7311e4ce37
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.11 CPython/3.10.4 Darwin/21.5.0
|
Release files / django_generic_tasks-0.5.1-py3-none-any.whl
| Download URL | django_generic_tasks-0.5.1-py3-none-any.whl |
|---|---|
| Size | 6.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
46834fc75d286be4dc3fab9bb33434f4a8f5fc5f5f3ff7dd53b3b9e918bdfa35
|
|
BLAKE2b-256 checksum How to use checksums |
995c28dbe66b23e7c4b35d92cf20494d5e0da3f7d411fa6fba8a87005885d1d4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.11 CPython/3.10.4 Darwin/21.5.0
|
Security for django-generic-tasks 0.5.1
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.