Create simple endpoint for barcode decode

This commit is contained in:
Oliver Walters 2020-04-13 23:39:56 +10:00
parent 6c761c2d0f
commit 9ff5032020
4 changed files with 63 additions and 19 deletions

View File

@ -0,0 +1,41 @@
"""
Main JSON interface views
"""
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import JsonResponse
from .views import AjaxView
from .version import inventreeVersion, inventreeInstanceName
class InfoView(AjaxView):
""" Simple JSON endpoint for InvenTree information.
Use to confirm that the server is running, etc.
"""
def get(self, request, *args, **kwargs):
data = {
'server': 'InvenTree',
'version': inventreeVersion(),
'instance': inventreeInstanceName(),
}
return JsonResponse(data)
class BarcodeScanView(AjaxView):
"""
Endpoint for handling barcode scan requests.
"""
def get(self, request, *args, **kwargs):
data = {
'barcode': 'Hello world',
}
return JsonResponse(data)

View File

@ -27,6 +27,18 @@ class APITests(APITestCase):
User = get_user_model() User = get_user_model()
User.objects.create_user(self.username, 'user@email.com', self.password) User.objects.create_user(self.username, 'user@email.com', self.password)
def test_info_view(self):
"""
Test that we can read the 'info-view' endpoint.
"""
url = reverse('api-inventree-info')
response = self.client.get(url, format='json')
print(response)
print(dir(response))
def test_get_token_fail(self): def test_get_token_fail(self):
""" Ensure that an invalid user cannot get a token """ """ Ensure that an invalid user cannot get a token """
@ -65,3 +77,7 @@ class APITests(APITestCase):
response = self.client.get(part_url, format='json') response = self.client.get(part_url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_barcode(self):
url = reverse('api-barcode-view')

View File

@ -35,7 +35,8 @@ from rest_framework.documentation import include_docs_urls
from .views import IndexView, SearchView, DatabaseStatsView from .views import IndexView, SearchView, DatabaseStatsView
from .views import SettingsView, EditUserView, SetPasswordView from .views import SettingsView, EditUserView, SetPasswordView
from .views import InfoView
from .api import InfoView, BarcodeScanView
from users.urls import user_urls from users.urls import user_urls
@ -53,8 +54,11 @@ apipatterns = [
# User URLs # User URLs
url(r'^user/', include(user_urls)), url(r'^user/', include(user_urls)),
# Barcode scanning endpoint
url(r'^barcode/', BarcodeScanView.as_view(), name='api-barcode-scan'),
# InvenTree information endpoint # InvenTree information endpoint
url(r'^$', InfoView.as_view(), name='inventree-info'), url(r'^$', InfoView.as_view(), name='api-inventree-info'),
] ]
settings_urls = [ settings_urls = [

View File

@ -22,7 +22,6 @@ from common.models import InvenTreeSetting
from .forms import DeleteForm, EditUserForm, SetPasswordForm from .forms import DeleteForm, EditUserForm, SetPasswordForm
from .helpers import str2bool from .helpers import str2bool
from .version import inventreeVersion, inventreeInstanceName
from rest_framework import views from rest_framework import views
@ -416,22 +415,6 @@ class AjaxDeleteView(AjaxMixin, UpdateView):
return self.renderJsonResponse(request, form, data=data, context=context) return self.renderJsonResponse(request, form, data=data, context=context)
class InfoView(AjaxView):
""" Simple JSON endpoint for InvenTree information.
Use to confirm that the server is running, etc.
"""
def get(self, request, *args, **kwargs):
data = {
'server': 'InvenTree',
'version': inventreeVersion(),
'instance': inventreeInstanceName(),
}
return JsonResponse(data)
class EditUserView(AjaxUpdateView): class EditUserView(AjaxUpdateView):
""" View for editing user information """ """ View for editing user information """