move apis to their respective bases

This commit is contained in:
Matthias 2022-05-11 13:10:29 +02:00
parent 499f37a97a
commit 51cca7a13c
No known key found for this signature in database
GPG Key ID: AB6D0E6C4CB65093
6 changed files with 62 additions and 53 deletions

View File

@ -13,15 +13,11 @@ from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters from rest_framework import filters
from rest_framework import permissions from rest_framework import permissions
from rest_framework.response import Response
from rest_framework.views import APIView
from .views import AjaxView from .views import AjaxView
from .version import inventreeVersion, inventreeApiVersion, inventreeInstanceName from .version import inventreeVersion, inventreeApiVersion, inventreeInstanceName
from .status import is_worker_running from .status import is_worker_running
from plugin import registry
class InfoView(AjaxView): class InfoView(AjaxView):
""" Simple JSON endpoint for InvenTree information. """ Simple JSON endpoint for InvenTree information.
@ -81,40 +77,3 @@ class AttachmentMixin:
attachment = serializer.save() attachment = serializer.save()
attachment.user = self.request.user attachment.user = self.request.user
attachment.save() 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,
})

View File

@ -27,7 +27,6 @@ from order.api import order_api_urls
from label.api import label_api_urls from label.api import label_api_urls
from report.api import report_api_urls from report.api import report_api_urls
from plugin.api import plugin_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 import settings
from django.conf.urls.static import static from django.conf.urls.static import static
@ -45,20 +44,13 @@ from .views import DynamicJsView
from .views import NotificationsView from .views import NotificationsView
from .api import InfoView, NotFoundView from .api import InfoView, NotFoundView
from .api import ActionPluginView
from users.api import user_urls from users.api import user_urls
admin.site.site_header = "InvenTree Admin" admin.site.site_header = "InvenTree Admin"
apipatterns = []
if settings.PLUGINS_ENABLED: apipatterns = [
apipatterns.append(
re_path(r'^plugin/', include(plugin_api_urls))
)
apipatterns += [
re_path(r'^settings/', include(settings_api_urls)), re_path(r'^settings/', include(settings_api_urls)),
re_path(r'^part/', include(part_api_urls)), re_path(r'^part/', include(part_api_urls)),
re_path(r'^bom/', include(bom_api_urls)), re_path(r'^bom/', include(bom_api_urls)),
@ -73,8 +65,7 @@ apipatterns += [
re_path(r'^user/', include(user_urls)), re_path(r'^user/', include(user_urls)),
# Plugin endpoints # Plugin endpoints
re_path(r'^action/', ActionPluginView.as_view(), name='api-action-plugin'), path('', include(plugin_api_urls)),
re_path(r'^barcode/', include(barcode_api_urls)),
# Webhook enpoint # Webhook enpoint
path('', include(common_api_urls)), path('', include(common_api_urls)),

View File

@ -5,6 +5,7 @@ JSON API for the plugin app
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
from django.conf import settings
from django.urls import include, re_path from django.urls import include, re_path
from rest_framework import generics from rest_framework import generics
@ -16,6 +17,8 @@ from rest_framework.response import Response
from django_filters.rest_framework import DjangoFilterBackend from django_filters.rest_framework import DjangoFilterBackend
from common.api import GlobalSettingsPermissions 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 from plugin.models import PluginConfig, PluginSetting
import plugin.serializers as PluginSerializers import plugin.serializers as PluginSerializers
from plugin.registry import registry from plugin.registry import registry
@ -157,6 +160,11 @@ class PluginSettingDetail(generics.RetrieveUpdateAPIView):
plugin_api_urls = [ 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 # Plugin settings URLs
re_path(r'^settings/', include([ re_path(r'^settings/', include([
@ -174,3 +182,8 @@ plugin_api_urls = [
# Anything else # Anything else
re_path(r'^.*$', PluginList.as_view(), name='api-plugin-list'), 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))
)

View File

@ -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,
})

View File

@ -237,7 +237,7 @@ class BarcodeAssign(APIView):
barcode_api_urls = [ barcode_api_urls = [
# Link a barcode to a part
path('link/', BarcodeAssign.as_view(), name='api-barcode-link'), path('link/', BarcodeAssign.as_view(), name='api-barcode-link'),
# Catch-all performs barcode 'scan' # Catch-all performs barcode 'scan'

View File

@ -5,6 +5,7 @@ URL lookup for plugin app
from django.urls import include, re_path from django.urls import include, re_path
from plugin import registry from plugin import registry
from plugin.base.barcodes.api import barcode_api_urls
PLUGIN_BASE = 'plugin' # Constant for links PLUGIN_BASE = 'plugin' # Constant for links