Skip to main content

django-routines

License: MIT Ruff PyPI version PyPI pyversions PyPI djversions PyPI status PyPI - Types Documentation Status Code Cov Test Status Lint Status Published on Django Packages OpenSSF Scorecard

Configure batches of Django management commands in your settings files and run them all at once. For example, batch together your common database maintenance tasks, deployment routines or any other set of commands you need to run together. This helps single source general site maintenance into your settings files keeping your code base DRY.

Example

Let's define two named routines, "package" and "deploy". The package routine will be a collection of commands that we typically run to generate package artifacts (like migrations and transpiled javascript). The deploy routine will be a collection of commands we typically run when deploying the site for the first time on a new server or when we deploy version updates on the server.

Routine commands are run in the order they are registered, or by priority.

There are two types of commands, management commands and system commands. The management commands will be called in the same process space as routine unless --subprocess is specified in which case they will use the same management script as routine was invoked with or whatever value you supply to --manage-script. System commands are always invoked as subprocesses.

In our settings file we may define these routines like this:

from django_routines import ManagementCommand, SystemCommand, command, system, routine

# register routines and their help text
routine(
    name="package",
    help_text=(
        "Generate pre-package artifacts like migrations and transpiled javascript."
    ),
)
# you may register commands on a routine after defining a routine (or before!)
command("package", "makemigrations")
command("package", "renderstatic")
system("package", "poetry", "build")

routine(
    "deploy",
    "Deploy the site application into production.",
    # you may also specify commands inline using the ManagementCommand dataclass
    ManagementCommand(
        ("routine", "package"), switches=["prepare"]
    ),  # routine commands can be other routines!
    ManagementCommand("migrate"),
    ManagementCommand("collectstatic"),
    ManagementCommand(("shellcompletion", "install"), switches=["initial"]),
    ManagementCommand(("loaddata", "./fixtures/demo.json"), switches=["demo"]),
    SystemCommand(("touch", "/path/to/wsgi.py")),
    # define switches that toggle commands on and off
    prepare="Generate artifacts like migrations and transpiled javascript.",
    initial="Things to do on the very first deployment on a new server.",
    demo="Load the demo data.",
)

The routine command will read our settings file and generate two subcommands, one called deploy and one called package:

package

Now we can run all of our package routines with one command:

    ?> ./manage.py routine package
    makemigrations
    ...
    renderstatic
    ...
    poetry build
    ...

The deploy command has several switches that we can enable to run additional commands.

deploy

For example to deploy our demo on a new server we would run:

    ?> ./manage.py routine deploy --initial --demo
    migrate
    ...
    collectstatic
    ...
    shellcompletion install
    ...
    loaddata ./fixtures/demo.json
    ...
    touch /path/to/wsgi.py

Settings

The ManagementCommand dataclass, routine and command helper functions in the example above make it easier for us to work with the native configuration format which is a dictionary structure defined in the DJANGO_ROUTINES setting attribute. For example the above configuration is equivalent to:

DJANGO_ROUTINES = {
    "deploy": {
        "commands": [
            {"command": ("routine", "package"), "switches": ["prepare"]},
            {"command": "migrate"},
            {"command": "collectstatic"},
            {
                "command": ("shellcompletion", "install"),
                "switches": ["initial"],
            },
            {
                "command": ("loaddata", "./fixtures/demo.json"),
                "switches": ["demo"],
            },
            {"command": ("touch", "/path/to/wsgi.py"), "kind": "system"},
        ],
        "help_text": "Deploy the site application into production.",
        "name": "deploy",
        "switch_helps": {
            "demo": "Load the demo data.",
            "initial": "Things to do on the very first deployment on a new server.",
            "prepare": "Generate artifacts like migrations and transpiled javascript.",
        },
    },
    "package": {
        "commands": [
            {"command": "makemigrations"},
            {"command": "renderstatic"},
            {"command": ("poetry", "build"), "kind": "system"},
        ],
        "help_text": "Generate pre-package artifacts like migrations and "
        "transpiled javascript.",
        "name": "package",
    },
}

Priorities

If you are composing settings from multiple apps or source files using a utility like django-split-settings you may not be able to define all routines at once. You can use priorities to make sure commands defined in a de-coupled way run in the correct order.

    command("deploy", "makemigrations", priority=1)
    command("deploy", "migrate", priority=2)

Options

When specifying arguments you may add them to the command tuple OR specify them as named options in the style that will be passed to call_command:

    # these two are equivalent
    command("package", "makemigrations", "--no-header")
    command("package", "makemigrations", no_header=True)

Execution Controls

There are several switches that can be used to control the execution of routines. Pass these parameters when you define the Routine.

  • atomic: Run the routine in a transaction.
  • continue_on_error: Continue running the routine even if a command fails.

The default routine behavior for these execution controls can be overridden on the command line.

Installation

  1. Clone django-routines from GitHub or install a release off PyPI :

        pip install django-routines
    

    rich is a powerful library for rich text and beautiful formatting in the terminal. It is not required, but highly recommended for the best experience:

        pip install "django-routines[rich]"
    
  2. Add django_routines to your INSTALLED_APPS setting:

        INSTALLED_APPS = [
            ...
            'django_routines',
            'django_typer',  # optional!
        ]
    

    You only need to install django_typer as an app if you want to use the shellcompletion command to enable tab-completion or if you would like django-typer to install rich traceback rendering for you - which it does by default if rich is also installed.

Rationale

When does it make sense to configure routines in Django settings? Its generally convenient to group common management pathways into easily discoverable and executable aggregations of subroutines. This is usually done in supporting shell scripts or just files and in most cases that is appropriate. If your goal is to keep your Django deployment as tight and self contained as possible and the deployment is not generally very complex, using django-routines can make a lot of sense. It can eliminate extra dependencies on a shell scripting environment or just files and can keep this logic packaged with your installable wheel.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_routines-1.7.2.tar.gz (67.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_routines-1.7.2-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file django_routines-1.7.2.tar.gz.

File metadata

  • Download URL: django_routines-1.7.2.tar.gz
  • Upload date:
  • Size: 67.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for django_routines-1.7.2.tar.gz
Algorithm Hash digest
SHA256 f497d51d883810481acd85804b047b458792b831fe25c6bd33855e2f7dc8502a
MD5 287fe6f0d49fbfbdf400992109d6c56b
BLAKE2b-256 da4e657dbc7b5eab5b767c45cf986c086a8856bd0a0f3f9b7fa2a1bdef3dc956

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_routines-1.7.2.tar.gz:

Publisher: release.yml on bckohan/django-routines

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_routines-1.7.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_routines-1.7.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5b0ffe08f02ce79491332f8d3f6a0e81b71a3e55d19d329cb85d70a4d9313f15
MD5 3c560a82edd0f5e658c5879e9331505c
BLAKE2b-256 5da06ea265575abc80f66a72835738ac3ccbc6c213c05fdbaf7335641fc9d8f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_routines-1.7.2-py3-none-any.whl:

Publisher: release.yml on bckohan/django-routines

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

1.8.0

2 files

This release

1.7.2 This release

2 files

1.7.1

2 files

1.7.0

2 files

1.6.2

2 files

1.6.1

2 files

1.6.0

2 files

1.5.1

2 files

1.5.0

2 files

1.4.0

2 files

1.3.0

2 files

1.2.1

2 files

1.2.0

2 files

1.1.3

2 files

1.1.2

2 files

1.1.1

2 files

1.1.0

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page