diff --git a/Dockerfile b/Dockerfile index 5e16036..dbcb30c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -ARG NETBOX_VARIANT=v3.5 +ARG NETBOX_VARIANT=v3.6 FROM netboxcommunity/netbox:${NETBOX_VARIANT} @@ -6,4 +6,4 @@ RUN mkdir -pv /plugins/netbox-metatype-importer COPY . /plugins/netbox-metatype-importer RUN /opt/netbox/venv/bin/python3 /plugins/netbox-metatype-importer/setup.py develop -RUN cp -rf /plugins/netbox-metatype-importer/netbox_metatype_importer/ /opt/netbox/venv/lib/python3.10/site-packages/netbox_metatype_importer +RUN cp -rf /plugins/netbox-metatype-importer/netbox_metatype_importer/ /opt/netbox/venv/lib/python3.11/site-packages/netbox_metatype_importer diff --git a/README.md b/README.md index 417eae1..d5e85d6 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ To use GraphQL API you need to set GitHub personal access token in plugin settin |:--------------:|:--------------:| | 3.4.x | 0.0.x | | 3.5.x | 0.1.x | +| 3.6.x | 0.2.x | ## Installation diff --git a/netbox_metatype_importer/__init__.py b/netbox_metatype_importer/__init__.py index 419ea70..31f6209 100644 --- a/netbox_metatype_importer/__init__.py +++ b/netbox_metatype_importer/__init__.py @@ -12,8 +12,8 @@ class NetBoxMetatypeImporterConfig(PluginConfig): version = metadata.get('Version') author = metadata.get('Author') author_email = metadata.get('Author-email') - min_version = '3.5.0' - max_version = '3.5.99' + min_version = '3.6.0' + max_version = '3.6.99' default_settings = { 'repo_owner': 'netbox-community', 'repo': 'devicetype-library', diff --git a/netbox_metatype_importer/gql.py b/netbox_metatype_importer/gql.py index e269417..4ffe60b 100644 --- a/netbox_metatype_importer/gql.py +++ b/netbox_metatype_importer/gql.py @@ -84,6 +84,12 @@ def get_tree(self): data = self.get_query(query) if not data: return result + + try: + data['data']['repository']['object']['entries'] + except TypeError: + return None + for vendor in data['data']['repository']['object']['entries']: result[vendor['name']] = {} for model in vendor['object'].get('entries', []): diff --git a/netbox_metatype_importer/templates/netbox_metatype_importer/metadevicetype_list.html b/netbox_metatype_importer/templates/netbox_metatype_importer/metadevicetype_list.html index f1b93d7..a29b8f9 100644 --- a/netbox_metatype_importer/templates/netbox_metatype_importer/metadevicetype_list.html +++ b/netbox_metatype_importer/templates/netbox_metatype_importer/metadevicetype_list.html @@ -1,4 +1,5 @@ {% extends 'generic/object_list.html' %} +{% load buttons %} {% block title %}DeviceType Imports{% endblock %} @@ -16,6 +17,7 @@ {% endif %} {% endblock %} + {% block bulk_buttons %} {% if perms.netbox_metatype_importer.add_metadevicetype %} {% endif %} + {% if perms.netbox_metatype_importer.delete_metadevicetype %} + + {% endif %} {% endblock %} \ No newline at end of file diff --git a/netbox_metatype_importer/urls.py b/netbox_metatype_importer/urls.py index e442ae6..54ff684 100644 --- a/netbox_metatype_importer/urls.py +++ b/netbox_metatype_importer/urls.py @@ -7,8 +7,10 @@ path('meta-device-types/', views.MetaDeviceTypeListView.as_view(), name='metadevicetype_list'), path('meta-device-types/load/', views.MetaDeviceTypeLoadView.as_view(), name='metadevicetype_load'), path('meta-device-types/import/', views.MetaDeviceTypeImportView.as_view(), name='metadevicetype_import'), + path('meta-device-types/delete/', views.MetaDeviceTypeBulkDeleteView.as_view(), name='bulk_metadevicetype_delete'), # Module types path('meta-module-types/', views.MetaModuleTypeListView.as_view(), name='metamoduletype_list'), path('meta-module-types/load/', views.MetaModuleTypeLoadView.as_view(), name='metamoduletype_load'), - path('meta-module-types/import/', views.MetaModuleTypeImportView.as_view(), name='metamoduletype_import') + path('meta-module-types/import/', views.MetaModuleTypeImportView.as_view(), name='metamoduletype_import'), + path('meta-module-types/delete/', views.MetaModuleTypeBulkDeleteView.as_view(), name='bulk_metamoduletype_delete'), ] diff --git a/netbox_metatype_importer/views.py b/netbox_metatype_importer/views.py index f5c9443..c2c85a8 100644 --- a/netbox_metatype_importer/views.py +++ b/netbox_metatype_importer/views.py @@ -68,6 +68,10 @@ def post(self, request): messages.error(request, message=f'GraphQL API Error: {e.message}') return redirect('plugins:netbox_metatype_importer:metadevicetype_list') + if models is None: + messages.error(request, 'Check your plugin settings and try again') + models = {} + for vendor, models in models.items(): for model, model_data in models.items(): loaded += 1 @@ -91,7 +95,8 @@ def post(self, request): type=self.path ) created += 1 - messages.success(request, f'Loaded: {loaded}, Created: {created}, Updated: {updated}') + if models: + messages.success(request, f'Loaded: {loaded}, Created: {created}, Updated: {updated}') return redirect(return_url) @@ -197,10 +202,6 @@ def post(self, request): model_form = self.model_form(data) - for field_name, field in model_form.fields.items(): - if field_name not in data and hasattr(field, 'initial'): - model_form.data[field_name] = field.initial - if model_form.is_valid(): try: with transaction.atomic(): @@ -245,9 +246,9 @@ def post(self, request): messages.error(request, f'Failed: {errored}') qparams = urlencode({'id': imported_dt}, doseq=True) # Black magic to get the url path from the type - return redirect(reverse(f'dcim:{str(self.type).replace("-", "").rstrip("s")}_list') + '?' + qparams) + return redirect(reverse(f'dcim:{self.type_model._meta.model_name}_list') + '?' + qparams) else: - messages.error(request, 'Can not import Device Types') + messages.error(request, f'Can not import {self.type_model.__name__}') return redirect(return_url) @@ -265,3 +266,13 @@ class MetaModuleTypeImportView(GenericTypeImportView): type_model = ModuleType model_form = forms.ModuleTypeImportForm related_object = 'module_type' + + +class MetaDeviceTypeBulkDeleteView(generic.BulkDeleteView): + queryset = MetaType.objects.filter(type=TypeChoices.TYPE_DEVICE) + table = MetaTypeTable + + +class MetaModuleTypeBulkDeleteView(generic.BulkDeleteView): + queryset = MetaType.objects.filter(type=TypeChoices.TYPE_MODULE) + table = MetaTypeTable diff --git a/setup.py b/setup.py index aa9e810..71f6222 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='netbox-metatype-importer', - version='0.1.1', + version='0.2.0', description='Easily import Device and Module types from GitHub repo', long_description='Import MetaTypes into NetBox', long_description_content_type="text/markdown",