From 51cca7a13c550bfae6d84178ffa2169c56e2bfe9 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 11 May 2022 13:10:29 +0200 Subject: [PATCH] move apis to their respective bases --- InvenTree/InvenTree/api.py | 41 ----------------- InvenTree/InvenTree/urls.py | 13 +----- InvenTree/plugin/api.py | 13 ++++++ InvenTree/plugin/base/action/api.py | 45 +++++++++++++++++++ .../base/barcodes/{barcode.py => api.py} | 2 +- InvenTree/plugin/urls.py | 1 + 6 files changed, 62 insertions(+), 53 deletions(-) create mode 100644 InvenTree/plugin/base/action/api.py rename InvenTree/plugin/base/barcodes/{barcode.py => api.py} (99%) diff --git a/InvenTree/InvenTree/api.py b/InvenTree/InvenTree/api.py index 171fe414d2..a8b2903a90 100644 --- a/InvenTree/InvenTree/api.py +++ b/InvenTree/InvenTree/api.py @@ -13,15 +13,11 @@ from django_filters.rest_framework import DjangoFilterBackend from rest_framework import filters from rest_framework import permissions -from rest_framework.response import Response -from rest_framework.views import APIView from .views import AjaxView from .version import inventreeVersion, inventreeApiVersion, inventreeInstanceName from .status import is_worker_running -from plugin import registry - class InfoView(AjaxView): """ Simple JSON endpoint for InvenTree information. @@ -81,40 +77,3 @@ class AttachmentMixin: attachment = serializer.save() attachment.user = self.request.user attachment.save() - - -class ActionPluginView(APIView): - """ - Endpoint for running custom action plugins. - """ - - permission_classes = [ - permissions.IsAuthenticated, - ] - - def post(self, request, *args, **kwargs): - - action = request.data.get('action', None) - - data = request.data.get('data', None) - - if action is None: - return Response({ - 'error': _("No action specified") - }) - - action_plugins = registry.with_mixin('action') - for plugin in action_plugins: - if plugin.action_name() == action: - # TODO @matmair use easier syntax once InvenTree 0.7.0 is released - plugin.init(request.user, data=data) - - plugin.perform_action() - - return Response(plugin.get_response()) - - # If we got to here, no matching action was found - return Response({ - 'error': _("No matching action found"), - "action": action, - }) diff --git a/InvenTree/InvenTree/urls.py b/InvenTree/InvenTree/urls.py index 9c89422a18..591a482450 100644 --- a/InvenTree/InvenTree/urls.py +++ b/InvenTree/InvenTree/urls.py @@ -27,7 +27,6 @@ from order.api import order_api_urls from label.api import label_api_urls from report.api import report_api_urls from plugin.api import plugin_api_urls -from plugin.base.barcodes.barcode import barcode_api_urls from django.conf import settings from django.conf.urls.static import static @@ -45,20 +44,13 @@ from .views import DynamicJsView from .views import NotificationsView from .api import InfoView, NotFoundView -from .api import ActionPluginView from users.api import user_urls admin.site.site_header = "InvenTree Admin" -apipatterns = [] -if settings.PLUGINS_ENABLED: - apipatterns.append( - re_path(r'^plugin/', include(plugin_api_urls)) - ) - -apipatterns += [ +apipatterns = [ re_path(r'^settings/', include(settings_api_urls)), re_path(r'^part/', include(part_api_urls)), re_path(r'^bom/', include(bom_api_urls)), @@ -73,8 +65,7 @@ apipatterns += [ re_path(r'^user/', include(user_urls)), # Plugin endpoints - re_path(r'^action/', ActionPluginView.as_view(), name='api-action-plugin'), - re_path(r'^barcode/', include(barcode_api_urls)), + path('', include(plugin_api_urls)), # Webhook enpoint path('', include(common_api_urls)), diff --git a/InvenTree/plugin/api.py b/InvenTree/plugin/api.py index b9fd6e643d..64c6f43d57 100644 --- a/InvenTree/plugin/api.py +++ b/InvenTree/plugin/api.py @@ -5,6 +5,7 @@ JSON API for the plugin app # -*- coding: utf-8 -*- from __future__ import unicode_literals +from django.conf import settings from django.urls import include, re_path from rest_framework import generics @@ -16,6 +17,8 @@ from rest_framework.response import Response from django_filters.rest_framework import DjangoFilterBackend from common.api import GlobalSettingsPermissions +from plugin.base.barcodes.api import barcode_api_urls +from plugin.base.action.api import ActionPluginView from plugin.models import PluginConfig, PluginSetting import plugin.serializers as PluginSerializers from plugin.registry import registry @@ -157,6 +160,11 @@ class PluginSettingDetail(generics.RetrieveUpdateAPIView): plugin_api_urls = [ + re_path(r'^action/', ActionPluginView.as_view(), name='api-action-plugin'), + re_path(r'^barcode/', include(barcode_api_urls)), +] + +general_plugin_api_urls = [ # Plugin settings URLs re_path(r'^settings/', include([ @@ -174,3 +182,8 @@ plugin_api_urls = [ # Anything else re_path(r'^.*$', PluginList.as_view(), name='api-plugin-list'), ] + +if settings.PLUGINS_ENABLED: + plugin_api_urls.append( + re_path(r'^plugin/', include(general_plugin_api_urls)) + ) diff --git a/InvenTree/plugin/base/action/api.py b/InvenTree/plugin/base/action/api.py new file mode 100644 index 0000000000..998e410dce --- /dev/null +++ b/InvenTree/plugin/base/action/api.py @@ -0,0 +1,45 @@ +"""APIs for action plugins""" +from django.utils.translation import gettext_lazy as _ + +from rest_framework import permissions +from rest_framework.response import Response +from rest_framework.views import APIView + +from plugin import registry + + +class ActionPluginView(APIView): + """ + Endpoint for running custom action plugins. + """ + + permission_classes = [ + permissions.IsAuthenticated, + ] + + def post(self, request, *args, **kwargs): + + action = request.data.get('action', None) + + data = request.data.get('data', None) + + if action is None: + return Response({ + 'error': _("No action specified") + }) + + action_plugins = registry.with_mixin('action') + for plugin in action_plugins: + if plugin.action_name() == action: + # TODO @matmair use easier syntax once InvenTree 0.7.0 is released + plugin.init(request.user, data=data) + + plugin.perform_action() + + return Response(plugin.get_response()) + + # If we got to here, no matching action was found + return Response({ + 'error': _("No matching action found"), + "action": action, + }) diff --git a/InvenTree/plugin/base/barcodes/barcode.py b/InvenTree/plugin/base/barcodes/api.py similarity index 99% rename from InvenTree/plugin/base/barcodes/barcode.py rename to InvenTree/plugin/base/barcodes/api.py index 21a142f841..296986b2d1 100644 --- a/InvenTree/plugin/base/barcodes/barcode.py +++ b/InvenTree/plugin/base/barcodes/api.py @@ -237,7 +237,7 @@ class BarcodeAssign(APIView): barcode_api_urls = [ - + # Link a barcode to a part path('link/', BarcodeAssign.as_view(), name='api-barcode-link'), # Catch-all performs barcode 'scan' diff --git a/InvenTree/plugin/urls.py b/InvenTree/plugin/urls.py index 08f547aca2..e97316773e 100644 --- a/InvenTree/plugin/urls.py +++ b/InvenTree/plugin/urls.py @@ -5,6 +5,7 @@ URL lookup for plugin app from django.urls import include, re_path from plugin import registry +from plugin.base.barcodes.api import barcode_api_urls PLUGIN_BASE = 'plugin' # Constant for links