|
| 1 | +import sys |
| 2 | +import warnings |
| 3 | + |
| 4 | +from django.core.exceptions import AppRegistryNotReady |
| 5 | +from django.db import transaction |
| 6 | +from django.db.utils import DatabaseError, OperationalError, ProgrammingError |
1 | 7 | from netbox.plugins import PluginConfig
|
2 | 8 |
|
3 | 9 |
|
| 10 | +def is_running_migration(): |
| 11 | + """ |
| 12 | + Check if the code is currently running during a Django migration. |
| 13 | + """ |
| 14 | + # Check if 'makemigrations' or 'migrate' command is in sys.argv |
| 15 | + if any(cmd in sys.argv for cmd in ["makemigrations", "migrate"]): |
| 16 | + return True |
| 17 | + |
| 18 | + return False |
| 19 | + |
| 20 | + |
| 21 | +def is_in_clear_cache(): |
| 22 | + """ |
| 23 | + Check if the code is currently being called from Django's clear_cache() method. |
| 24 | +
|
| 25 | + TODO: This is fairly ugly, but in models.CustomObjectType.get_model() we call |
| 26 | + meta = type() which calls clear_cache on the model which causes a call to |
| 27 | + get_models() which in-turn calls get_model and therefore recurses. |
| 28 | +
|
| 29 | + This catches the specific case of a recursive call to get_models() from |
| 30 | + clear_cache() which is the only case we care about, so should be relatively |
| 31 | + safe. An alternative should be found for this. |
| 32 | + """ |
| 33 | + import inspect |
| 34 | + |
| 35 | + frame = inspect.currentframe() |
| 36 | + try: |
| 37 | + # Walk up the call stack to see if we're being called from clear_cache |
| 38 | + while frame: |
| 39 | + if ( |
| 40 | + frame.f_code.co_name == "clear_cache" |
| 41 | + and "django/apps/registry.py" in frame.f_code.co_filename |
| 42 | + ): |
| 43 | + return True |
| 44 | + frame = frame.f_back |
| 45 | + return False |
| 46 | + finally: |
| 47 | + # Clean up the frame reference |
| 48 | + del frame |
| 49 | + |
| 50 | + |
| 51 | +def check_custom_object_type_table_exists(): |
| 52 | + """ |
| 53 | + Check if the CustomObjectType table exists in the database. |
| 54 | + Returns True if the table exists, False otherwise. |
| 55 | + """ |
| 56 | + from .models import CustomObjectType |
| 57 | + |
| 58 | + try: |
| 59 | + # Try to query the model - if the table doesn't exist, this will raise an exception |
| 60 | + # this check and the transaction.atomic() is only required when running tests as the |
| 61 | + # migration check doesn't work correctly in the test environment |
| 62 | + with transaction.atomic(): |
| 63 | + # Force immediate execution by using first() |
| 64 | + CustomObjectType.objects.first() |
| 65 | + return True |
| 66 | + except (OperationalError, ProgrammingError, DatabaseError): |
| 67 | + # Catch database-specific errors (table doesn't exist, permission issues, etc.) |
| 68 | + return False |
| 69 | + |
| 70 | + |
4 | 71 | # Plugin Configuration
|
5 | 72 | class CustomObjectsPluginConfig(PluginConfig):
|
6 | 73 | name = "netbox_custom_objects"
|
7 | 74 | verbose_name = "Custom Objects"
|
8 | 75 | description = "A plugin to manage custom objects in NetBox"
|
9 |
| - version = "0.1.0" |
| 76 | + version = "0.2.0" |
10 | 77 | base_url = "custom-objects"
|
11 |
| - min_version = "4.2.0" |
| 78 | + min_version = "4.4.0" |
12 | 79 | default_settings = {}
|
13 | 80 | required_settings = []
|
14 | 81 | template_extensions = "template_content.template_extensions"
|
| 82 | + _in_get_models = False # Recursion guard |
| 83 | + |
| 84 | + def get_model(self, model_name, require_ready=True): |
| 85 | + try: |
| 86 | + # if the model is already loaded, return it |
| 87 | + return super().get_model(model_name, require_ready) |
| 88 | + except LookupError: |
| 89 | + try: |
| 90 | + self.apps.check_apps_ready() |
| 91 | + except AppRegistryNotReady: |
| 92 | + raise |
| 93 | + |
| 94 | + # only do database calls if we are sure the app is ready to avoid |
| 95 | + # Django warnings |
| 96 | + if "table" not in model_name.lower() or "model" not in model_name.lower(): |
| 97 | + raise LookupError( |
| 98 | + "App '%s' doesn't have a '%s' model." % (self.label, model_name) |
| 99 | + ) |
| 100 | + |
| 101 | + from .models import CustomObjectType |
| 102 | + |
| 103 | + custom_object_type_id = int( |
| 104 | + model_name.replace("table", "").replace("model", "") |
| 105 | + ) |
| 106 | + |
| 107 | + try: |
| 108 | + obj = CustomObjectType.objects.get(pk=custom_object_type_id) |
| 109 | + except CustomObjectType.DoesNotExist: |
| 110 | + raise LookupError( |
| 111 | + "App '%s' doesn't have a '%s' model." % (self.label, model_name) |
| 112 | + ) |
| 113 | + |
| 114 | + return obj.get_model() |
| 115 | + |
| 116 | + def get_models(self, include_auto_created=False, include_swapped=False): |
| 117 | + """Return all models for this plugin, including custom object type models.""" |
| 118 | + |
| 119 | + # Get the regular Django models first |
| 120 | + for model in super().get_models(include_auto_created, include_swapped): |
| 121 | + yield model |
| 122 | + |
| 123 | + # Prevent recursion |
| 124 | + if self._in_get_models and is_in_clear_cache(): |
| 125 | + # Skip dynamic model creation if we're in a recursive get_models call |
| 126 | + return |
| 127 | + |
| 128 | + self._in_get_models = True |
| 129 | + try: |
| 130 | + # Suppress warnings about database calls during model loading |
| 131 | + with warnings.catch_warnings(): |
| 132 | + warnings.filterwarnings( |
| 133 | + "ignore", category=RuntimeWarning, message=".*database.*" |
| 134 | + ) |
| 135 | + warnings.filterwarnings( |
| 136 | + "ignore", category=UserWarning, message=".*database.*" |
| 137 | + ) |
| 138 | + |
| 139 | + # Skip custom object type model loading if running during migration |
| 140 | + if ( |
| 141 | + is_running_migration() |
| 142 | + or not check_custom_object_type_table_exists() |
| 143 | + ): |
| 144 | + return |
| 145 | + |
| 146 | + # Add custom object type models |
| 147 | + from .models import CustomObjectType |
15 | 148 |
|
16 |
| - # def get_model(self, model_name, require_ready=True): |
17 |
| - # if require_ready: |
18 |
| - # self.apps.check_models_ready() |
19 |
| - # else: |
20 |
| - # self.apps.check_apps_ready() |
21 |
| - # |
22 |
| - # if model_name.lower() in self.models: |
23 |
| - # return self.models[model_name.lower()] |
24 |
| - # |
25 |
| - # from .models import CustomObjectType |
26 |
| - # if "table" not in model_name.lower() or "model" not in model_name.lower(): |
27 |
| - # raise LookupError( |
28 |
| - # "App '%s' doesn't have a '%s' model." % (self.label, model_name) |
29 |
| - # ) |
30 |
| - # |
31 |
| - # custom_object_type_id = int(model_name.replace("table", "").replace("model", "")) |
32 |
| - # |
33 |
| - # try: |
34 |
| - # obj = CustomObjectType.objects.get(pk=custom_object_type_id) |
35 |
| - # except CustomObjectType.DoesNotExist: |
36 |
| - # raise LookupError( |
37 |
| - # "App '%s' doesn't have a '%s' model." % (self.label, model_name) |
38 |
| - # ) |
39 |
| - # return obj.get_model() |
| 149 | + custom_object_types = CustomObjectType.objects.all() |
| 150 | + for custom_type in custom_object_types: |
| 151 | + model = custom_type.get_model() |
| 152 | + if model: |
| 153 | + yield model |
| 154 | + finally: |
| 155 | + # Clean up the recursion guard |
| 156 | + self._in_get_models = False |
40 | 157 |
|
41 | 158 |
|
42 | 159 | config = CustomObjectsPluginConfig
|
0 commit comments