diff --git a/InvenTree/InvenTree/urls.py b/InvenTree/InvenTree/urls.py index 1347809dd7..6bd9ec1fb1 100644 --- a/InvenTree/InvenTree/urls.py +++ b/InvenTree/InvenTree/urls.py @@ -28,6 +28,7 @@ from build.api import build_api_urls 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 django.conf import settings from django.conf.urls.static import static @@ -63,6 +64,7 @@ apipatterns = [ url(r'^order/', include(order_api_urls)), url(r'^label/', include(label_api_urls)), url(r'^report/', include(report_api_urls)), + url(r'^plugin/', include(plugin_api_urls)), # User URLs url(r'^user/', include(user_urls)), diff --git a/InvenTree/plugin/api.py b/InvenTree/plugin/api.py new file mode 100644 index 0000000000..c6f7aadfbe --- /dev/null +++ b/InvenTree/plugin/api.py @@ -0,0 +1,45 @@ +""" +JSON API for the plugin app +""" + +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.conf.urls import url +from django.utils.translation import ugettext_lazy as _ + +from rest_framework import generics + +from plugin.models import PluginConfig +import plugin.serializers as PluginSerializers + + +class PluginList(generics.ListAPIView): + """ API endpoint for list of PluginConfig objects + + - GET: Return a list of all PluginConfig objects + """ + + serializer_class = PluginSerializers.PluginConfigSerializer + queryset = PluginConfig.objects.all() + + ordering_fields = [ + 'key', + 'name', + 'active', + ] + + ordering = [ + 'key', + ] + + search_fields = [ + 'key', + 'name', + ] + + +plugin_api_urls = [ + # Anything else + url(r'^.*$', PluginList.as_view(), name='api-plugin-list'), +] diff --git a/InvenTree/plugin/models.py b/InvenTree/plugin/models.py index f162cfd121..2c9f00d5f8 100644 --- a/InvenTree/plugin/models.py +++ b/InvenTree/plugin/models.py @@ -8,6 +8,7 @@ from __future__ import unicode_literals from django.utils.translation import gettext_lazy as _ from django.db import models from django.apps import apps +from django.conf import settings class PluginConfig(models.Model): @@ -51,11 +52,25 @@ class PluginConfig(models.Model): name += '(not active)' return name + # functions + def __init__(self, *args, **kwargs): """override to set original state of""" super().__init__(*args, **kwargs) self.__org_active = self.active + # append settings from registry + self.plugin = settings.INTEGRATION_PLUGINS.get(self.key, None) + + def get_plugin_meta(name): + if self.plugin: + return str(getattr(self.plugin, name, None)) + return None + + self.meta = {key: get_plugin_meta(key) for key in ['slug', 'human_name', 'description', 'author', \ + 'pub_date', 'version', 'website', 'license', 'package_path', 'settings_url', ]} + + def save(self, force_insert=False, force_update=False, *args, **kwargs): """extend save method to reload plugins if the 'active' status changes""" reload = kwargs.pop('no_reload', False) # check if no_reload flag is set diff --git a/InvenTree/plugin/serializers.py b/InvenTree/plugin/serializers.py new file mode 100644 index 0000000000..b5a2780529 --- /dev/null +++ b/InvenTree/plugin/serializers.py @@ -0,0 +1,28 @@ +""" +JSON serializers for Stock app +""" + +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.utils.translation import ugettext_lazy as _ + +from rest_framework import serializers + +from plugin.models import PluginConfig + + +class PluginConfigSerializer(serializers.ModelSerializer): + """ Serializer for a PluginConfig: + """ + + meta = serializers.DictField(read_only=True) + + class Meta: + model = PluginConfig + fields = [ + 'key', + 'name', + 'active', + 'meta', + ]