109 lines
2.5 KiB
Python
109 lines
2.5 KiB
Python
"""
|
|
Main JSON interface views
|
|
"""
|
|
|
|
# -*- coding: utf-8 -*-
|
|
from __future__ import unicode_literals
|
|
|
|
import logging
|
|
|
|
from django.utils.translation import ugettext as _
|
|
from django.http import JsonResponse
|
|
|
|
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, inventreeInstanceName
|
|
|
|
from users.models import check_user_role, RuleSet
|
|
|
|
from plugins import plugins as inventree_plugins
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
logger.info("Loading action plugins...")
|
|
action_plugins = inventree_plugins.load_action_plugins()
|
|
|
|
|
|
class InfoView(AjaxView):
|
|
""" Simple JSON endpoint for InvenTree information.
|
|
Use to confirm that the server is running, etc.
|
|
"""
|
|
|
|
permission_classes = [permissions.AllowAny]
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
data = {
|
|
'server': 'InvenTree',
|
|
'version': inventreeVersion(),
|
|
'instance': inventreeInstanceName(),
|
|
}
|
|
|
|
return JsonResponse(data)
|
|
|
|
|
|
class AttachmentMixin:
|
|
"""
|
|
Mixin for creating attachment objects,
|
|
and ensuring the user information is saved correctly.
|
|
"""
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
filter_backends = [
|
|
DjangoFilterBackend,
|
|
filters.OrderingFilter,
|
|
filters.SearchFilter,
|
|
]
|
|
|
|
def perform_create(self, serializer):
|
|
""" Save the user information when a file is uploaded """
|
|
|
|
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")
|
|
})
|
|
|
|
for plugin_class in action_plugins:
|
|
if plugin_class.action_name() == action:
|
|
|
|
plugin = plugin_class(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,
|
|
})
|