Skip to content

Commit be5c2a4

Browse files
Closes #97: Introduce the exempt_models config parameter (#109)
* Closes #97: Introduce the exempt_models config parameter to disable branching support for specific plugin models * Add warning
1 parent d5193bd commit be5c2a4

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

docs/configuration.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
# Configuration Parameters
22

3+
## `exempt_models`
4+
5+
Default: `[]` (empty list)
6+
7+
A list of models provided by other plugins which should be exempt from branching support. (Only models which support change logging need be listed; all other models are ineligible for branching support.)
8+
9+
!!! warning
10+
A model may not be exempted from branching support if it has one or more relationships to models for which branching is supported. Branching **must** be supported consistently for all inter-related models; otherwise, data corruption can occur. Configure this setting only if you have a specific need to disable branching for certain models provided by plugins.
11+
12+
Models must be specified by app label and model name, as such:
13+
14+
```python
15+
exempt_models = (
16+
'my_plugin.foo',
17+
'my_plugin.bar',
18+
)
19+
```
20+
21+
It is also possible to exclude _all_ models from within a plugin by substituting an asterisk (`*`) for the model name:
22+
23+
```python
24+
exempt_models = (
25+
'my_plugin.*',
26+
)
27+
```
28+
29+
---
30+
331
## `max_working_branches`
432

533
Default: None

netbox_branching/__init__.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.conf import settings
22
from django.core.exceptions import ImproperlyConfigured
33

4-
from netbox.plugins import PluginConfig
4+
from netbox.plugins import PluginConfig, get_plugin_config
55
from netbox.registry import registry
66

77

@@ -22,6 +22,9 @@ class AppConfig(PluginConfig):
2222
# The maximum number of branches which can be provisioned simultaneously
2323
'max_branches': None,
2424

25+
# Models from other plugins which should be excluded from branching support
26+
'exempt_models': [],
27+
2528
# This string is prefixed to the name of each new branch schema during provisioning
2629
'schema_prefix': 'branch_',
2730
}
@@ -42,11 +45,20 @@ def ready(self):
4245
)
4346

4447
# Record all object types which support branching in the NetBox registry
45-
if 'branching' not in registry['model_features']:
46-
registry['model_features']['branching'] = {
47-
k: v for k, v in registry['model_features']['change_logging'].items()
48-
if k not in constants.EXCLUDED_APPS
49-
}
48+
exempt_models = (
49+
*constants.EXEMPT_MODELS,
50+
*get_plugin_config('netbox_branching', 'exempt_models'),
51+
)
52+
branching_models = {}
53+
for app_label, models in registry['model_features']['change_logging'].items():
54+
# Wildcard exclusion for all models in this app
55+
if f'{app_label}.*' in exempt_models:
56+
continue
57+
branching_models[app_label] = [
58+
model for model in models
59+
if f'{app_label}.{model}' not in exempt_models
60+
]
61+
registry['model_features']['branching'] = branching_models
5062

5163

5264
config = AppConfig

netbox_branching/constants.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
# URL query parameter name
1111
QUERY_PARAM = '_branch'
1212

13-
# Apps which are explicitly excluded from branching
14-
EXCLUDED_APPS = (
15-
'netbox_branching',
16-
'netbox_changes',
13+
# Models for which branching support is explicitly disabled
14+
EXEMPT_MODELS = (
15+
'netbox_branching.*',
16+
'netbox_changes.*',
1717
)

0 commit comments

Comments
 (0)