Settings¶
DJANGO_ROUTINES¶
The configuration for django-routines is stored in the DJANGO_ROUTINES setting
attribute. It is a dictionary structure that defines the routines and their commands. The structure
is complex so we provide dataclasses and functions that facilitate type safe definition of
the configuration.
Structure¶
- DJANGO_ROUTINES: dict¶
Django routines configuration dictionary. The keys in this dictionary are the names of the routines. Each routine is a dictionary containing a routine specification:
- "<routine name>"
The routine specification dictionary contains the following keys:
- "help_text": str | Promise
The help text to display for the routine. May be a string wrapped by
gettext_lazy()
- "switch_helps": Dict[str, str | Promise]
Help text for switches. The keys are the switch names, and the values are the help text to display for each switch in the CLI help output.
- "subprocess": bool = False
If true run each of the commands in a subprocess.
- "atomic": bool = False
Run all commands in the same transaction.
- "continue_on_error": bool = False
Keep going if a command fails.
- "initialize": str | Callable[[Routine, List[ManagementCommand | SystemCommand], Set[str], Dict[str, Any]], None] | None = None
A function to run before the routine is run. See
InitializeCallbackMay be the callable function or an import string to the callable function.
- "finalize": str | Callable[[Routine, List[Any]], None] | None = None
A function to run after the routine is run. See
FinalizeCallbackMay be the callable function or an import string to the callable function.
- "pre_hook": str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None = None
This function will be run before each command that lacks its own pre_hook in the routine. See
PreHook. You can determine if this is the starting command of the routine by checking if the previous command is None.May be the callable function or an import string to the callable function.
- "post_hook": str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None = None
This function will be run after each command that lacks its own post_hook in the routine. See
PostHook. You can determine if this is the last command of the routine by checking if the next command is None. Post hooks may also be used to exit the routine early by returning True.May be the callable function or an import string to the callable function.
- "commands": List[ManagementCommand | SystemCommand]
The commands to run in the routine.
The command dictionaries must contain either a
managementorsystemkey that maps to a command specification.- "management | system": str | Tuple[str, ...]
The command and its arguments to run the routine, all strings or coercible to strings that the command will parse correctly.
This should be what you would pass as the
argsofcall_command()orsubprocess.run()
- "options": Dict[str, Any]
Any options to pass to the command via
call_command(). Not valid for SystemCommands
- "priority": int = 0
The order of the command in the routine. Priority ties will be run in insertion order.
- "switches": List[str] | Tuple[str, ...] = ()
If any switches are specified, the command will only run when one of the switches is activated on routine invocation from the command line. For example, if you list a switch named
init, you would have to invoke the routine like so to run this command as part of the routine:django-admin routine <routine_name> --init
- "pre_hook": str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None = None
A function that will be run before the command is run. It may make modifications to the command or decide to skip the command by returning True. See
PreHookMay be the callable function or an import string to the callable function.
- "post_hook": str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None = None
A function that will be run after the command has been run. It may make modifications to the command (including its result) or decide to exit the routine early by returning True. See
PostHookIt may also make modifications to the next command that will be run, if any.
May be the callable function or an import string to the callable function.
Examples¶
The dictionary can be specified using any combination of these different methods.
1DJANGO_ROUTINES = {
2 "<routine_name>": {
3 "commands": [
4 {
5 "management": ("<management_command>", "<arg1>"),
6 "options": {}, # kwargs for call_command
7 "priority": 0, # an integery priority for execution order
8 "switches": ["<switch_name>"], # optional switch enablers
9 "pre_hook": None, # or a callable pre hook function
10 "post_hook": None, # or a callable post hook function
11 },
12 {
13 "system": ("<cmd>", "<arg1>"),
14 "priority": 0, # an integery priority for execution order
15 "switches": ["<switch_name>"], # optional switch enablers
16 "pre_hook": None, # or a callable pre hook function
17 "post_hook": None, # or a callable post hook function
18 },
19 ],
20 "help_text": "<help_text>",
21 "switch_helps": {
22 "<switch_name>": "<switch_help_text>.",
23 },
24 "subprocess": False, # or True
25 "atomic": False, # or True
26 "continue_on_error": False, # or True
27 "initialize": None, # or a callable initialize function
28 "finalize": None, # or a callable finalize function
29 "pre_hook": None, # or a callable pre hook function
30 "post_hook": None, # or a callable post hook function
31 },
32}
The data classes provide a type-safe, editor friendly way to define the configuration.
1from django_routines import ManagementCommand, Routine, SystemCommand
2
3
4DJANGO_ROUTINES = {
5 "<routine_name>": Routine(
6 "<routine_name>",
7 commands=[
8 ManagementCommand(
9 ("<management_command>", "<arg1>",),
10 priority=0, # an integery priority for execution order
11 options={}, # kwargs for call_command
12 switches=["<switch_name>"], # optional switch enablers
13 pre_hook=None, # or a callable pre hook function
14 post_hook=None, # or a callable post hook function
15 ),
16 SystemCommand(
17 ("<cmd>", "<arg1>",),
18 priority=0, # an integery priority for execution order
19 switches=["<switch_name>"], # optional switch enablers
20 pre_hook=None, # or a callable pre hook function
21 post_hook=None, # or a callable post hook function
22 ),
23 ],
24 help_text="<help_text>",
25 switch_helps={
26 "<switch_name>": "<switch_help_text>.",
27 },
28 subprocess=False, # or True
29 atomic=False, # or True
30 continue_on_error=False, # or True
31 initialize=None, # or a callable initialize function
32 finalize=None, # or a callable finalize function
33 pre_hook=None, # or a callable pre hook function
34 post_hook=None, # or a callable post hook function
35 )
36}
The shortcut functions are especially useful when composing routines across multiple different settings files. They allow you to not worry about definition order. The priority parameter allows for deterministic routine order even if the order of the functions changes.
1from django_routines import routine, command, system
2
3# the function version of dict.py
4routine(
5 "<routine_name>",
6 help_text="<help_text>",
7 switch_helps={
8 "<switch_name>": "<switch_help_text>.",
9 },
10 subprocess=False, # or True
11 atomic=False, # or True
12 continue_on_error=False, # or True
13 initialize=None, # or a callable initialize function
14 finalize=None, # or a callable finalize function
15 pre_hook=None, # or a callable pre hook function
16 post_hook=None, # or a callable post hook function
17)
18
19command(
20 "<routine_name>",
21 "<management_command>",
22 "<arg1>",
23 priority=0, # an integer priority for execution order
24 options={}, # kwargs for call_command
25 switches=["<switch_name>"], # optional switch enablers
26 pre_hook=None, # or a callable pre hook function
27 post_hook=None, # or a callable post hook function
28)
29
30system(
31 "<routine_name>",
32 "<cmd>",
33 "<arg1>",
34 priority=0, # an integer priority for execution order
35 switches=["<switch_name>"], # optional switch enablers
36 pre_hook=None, # or a callable pre hook function
37 post_hook=None, # or a callable post hook function
38)