From 058e53459b87b808a65f4f268c896cfd3a40dc1d Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 23 Jun 2021 20:07:56 +1000 Subject: [PATCH] Add simple function for determining OPTIONS --- InvenTree/InvenTree/urls.py | 2 + InvenTree/templates/base.html | 3 +- .../script/inventree => templates/js}/api.js | 0 InvenTree/templates/js/forms.js | 38 +++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) rename InvenTree/{InvenTree/static/script/inventree => templates/js}/api.js (100%) create mode 100644 InvenTree/templates/js/forms.js diff --git a/InvenTree/InvenTree/urls.py b/InvenTree/InvenTree/urls.py index 0108418517..2ec7b02f23 100644 --- a/InvenTree/InvenTree/urls.py +++ b/InvenTree/InvenTree/urls.py @@ -103,6 +103,8 @@ settings_urls = [ # Some javascript files are served 'dynamically', allowing them to pass through the Django translation layer dynamic_javascript_urls = [ + url(r'^api.js', DynamicJsView.as_view(template_name='js/api.js'), name='api.js'), + url(r'^forms.js', DynamicJsView.as_view(template_name='js/forms.js'), name='forms.js'), url(r'^modals.js', DynamicJsView.as_view(template_name='js/modals.js'), name='modals.js'), url(r'^barcode.js', DynamicJsView.as_view(template_name='js/barcode.js'), name='barcode.js'), url(r'^bom.js', DynamicJsView.as_view(template_name='js/bom.js'), name='bom.js'), diff --git a/InvenTree/templates/base.html b/InvenTree/templates/base.html index 65712b7394..128cbba2dd 100644 --- a/InvenTree/templates/base.html +++ b/InvenTree/templates/base.html @@ -144,11 +144,12 @@ - + + diff --git a/InvenTree/InvenTree/static/script/inventree/api.js b/InvenTree/templates/js/api.js similarity index 100% rename from InvenTree/InvenTree/static/script/inventree/api.js rename to InvenTree/templates/js/api.js diff --git a/InvenTree/templates/js/forms.js b/InvenTree/templates/js/forms.js new file mode 100644 index 0000000000..fa2ca9eb2e --- /dev/null +++ b/InvenTree/templates/js/forms.js @@ -0,0 +1,38 @@ +/** + * This file contains code for rendering (and managing) HTML forms + * which are served via the django-drf API. + * + * The django DRF library provides an OPTIONS method for each API endpoint, + * which allows us to introspect the available fields at any given endpoint. + * + * The OPTIONS method provides the following information for each available field: + * + * - Field name + * - Field label (translated) + * - Field help text (translated) + * - Field type + * - Read / write status + * - Field required status + * - min_value / max_value + */ + + +/* + * Get the API endpoint options at the provided URL, + * using a HTTP options request. + */ +function getApiEndpointOptions(url, options={}) { + + $.ajax({ + url: url, + type: 'OPTIONS', + contentType: 'application/json', + dataType: 'json', + accepts: { + json: 'application/json', + }, + success: function(response) { + console.log(response); + } + }); +}