From e8ef3fc5228a009de799dcbb7c92f563027f2b4a Mon Sep 17 00:00:00 2001 From: Per Kraulis Date: Sat, 28 Oct 2023 17:24:34 +0200 Subject: [PATCH 1/2] Added API delete of report. Closes #1215. --- api_scripts/add_report.py | 2 +- api_scripts/create_order.py | 2 +- api_scripts/delete_report.py | 27 +++++++++++++++++++++++++++ api_scripts/edit_order.py | 2 +- api_scripts/edit_report.py | 4 ++-- api_scripts/get_account.py | 2 +- api_scripts/get_order.py | 2 +- api_scripts/get_reports.py | 4 ++-- api_scripts/submit_order.py | 2 +- orderportal/__init__.py | 2 +- orderportal/documentation.md | 34 +++++++++++++++++++++++++++++++--- orderportal/report.py | 16 ++++++++++++++++ 12 files changed, 85 insertions(+), 14 deletions(-) create mode 100644 api_scripts/delete_report.py diff --git a/api_scripts/add_report.py b/api_scripts/add_report.py index 5f7f4a5..e6ad786 100644 --- a/api_scripts/add_report.py +++ b/api_scripts/add_report.py @@ -5,7 +5,7 @@ must be provided in the JSON indata, and must therefore be base64-encoded in UTF-8. -NOTE: You need to change several upper-case variables. +NOTE: You need to review/change the upper-case variables. NOTE: The third-party 'requests' module is used. """ diff --git a/api_scripts/create_order.py b/api_scripts/create_order.py index e0c9ae2..f1c3e91 100644 --- a/api_scripts/create_order.py +++ b/api_scripts/create_order.py @@ -1,6 +1,6 @@ """API example script: Create an order from a given form. -NOTE: You need to change several upper-case variables. +NOTE: You need to review/change the upper-case variables. NOTE: The third-party 'requests' module is used. """ diff --git a/api_scripts/delete_report.py b/api_scripts/delete_report.py new file mode 100644 index 0000000..3354e7e --- /dev/null +++ b/api_scripts/delete_report.py @@ -0,0 +1,27 @@ +"""API example script: Delete a report for an order. + +NOTE: You need to review/change the upper-case variables. + +NOTE: The third-party 'requests' module is used. +""" + +import base64 +import json +import os + +import requests + +# Base URL for your OrderPortal instance. +BASE_URL = "http://localhost:8881/" + +# The IUID for the report. +REPORT_IUID = "f753f72504b04186aa5ee03a41fa955e" + +API_KEY = os.environ["ORDERPORTAL_API_KEY"] + + +url = f"{BASE_URL}api/v1/report/{REPORT_IUID}" +headers = {"X-OrderPortal-API-key": API_KEY} + +response = requests.delete(url, headers=headers) +assert response.status_code == 204, (response.status_code, response.reason) diff --git a/api_scripts/edit_order.py b/api_scripts/edit_order.py index b19a5cc..c30f33b 100644 --- a/api_scripts/edit_order.py +++ b/api_scripts/edit_order.py @@ -1,6 +1,6 @@ """API example script: Edit an order; fields, title. -NOTE: You need to change several upper-case variables. +NOTE: You need to review/change the upper-case variables. NOTE: The third-party 'requests' module is used. """ diff --git a/api_scripts/edit_report.py b/api_scripts/edit_report.py index 18f2989..45ed0d4 100644 --- a/api_scripts/edit_report.py +++ b/api_scripts/edit_report.py @@ -1,8 +1,8 @@ """API example script: Edit the content and status of a report for an order. -NOTE: The 'order' and 'owner' of the report cannot be edited. +NOTE: You need to review/change the upper-case variables. -NOTE: You need to change several upper-case variables. +NOTE: The 'order' and 'owner' of the report cannot be edited. NOTE: The third-party 'requests' module is used. """ diff --git a/api_scripts/get_account.py b/api_scripts/get_account.py index ca32d78..bd5f1ee 100644 --- a/api_scripts/get_account.py +++ b/api_scripts/get_account.py @@ -1,6 +1,6 @@ """API example script: Get account data. -NOTE: You need to change several upper-case variables. +NOTE: You need to review/change the upper-case variables. NOTE: The third-party 'requests' module is used. """ diff --git a/api_scripts/get_order.py b/api_scripts/get_order.py index 5861bdf..af4b100 100644 --- a/api_scripts/get_order.py +++ b/api_scripts/get_order.py @@ -1,6 +1,6 @@ """API example script: Get order data. -NOTE: You need to change several upper-case variables. +NOTE: You need to review/change the upper-case variables. NOTE: The third-party 'requests' module is used. """ diff --git a/api_scripts/get_reports.py b/api_scripts/get_reports.py index 023ed3e..975f95f 100644 --- a/api_scripts/get_reports.py +++ b/api_scripts/get_reports.py @@ -1,6 +1,6 @@ """API example script: Get reports for an order. -NOTE: You need to change several upper-case variables. +NOTE: You need to review/change the upper-case variables. NOTE: The third-party 'requests' module is used. """ @@ -15,7 +15,7 @@ BASE_URL = "http://localhost:8881" # The identifier for the order. -ORDER_ID = "OP0032" +ORDER_ID = "OP00013" API_KEY = os.environ["ORDERPORTAL_API_KEY"] diff --git a/api_scripts/submit_order.py b/api_scripts/submit_order.py index 4264537..fa7245f 100644 --- a/api_scripts/submit_order.py +++ b/api_scripts/submit_order.py @@ -1,6 +1,6 @@ """API example script: Submit the order. -NOTE: You need to change several upper-case variables. +NOTE: You need to review/change the upper-case variables. NOTE: The third-party 'requests' module is used. """ diff --git a/orderportal/__init__.py b/orderportal/__init__.py index 5940b7d..29dd044 100644 --- a/orderportal/__init__.py +++ b/orderportal/__init__.py @@ -10,7 +10,7 @@ import pycountry -__version__ = "11.3.7" +__version__ = "11.4.0" class Constants: diff --git a/orderportal/documentation.md b/orderportal/documentation.md index ab1b31c..62203e2 100644 --- a/orderportal/documentation.md +++ b/orderportal/documentation.md @@ -719,8 +719,9 @@ status transitions. ### API Add order report -A report for an order can be added by doing a PUT to the order -report URI with the report content file as request body. +A report for an order can be added by doing a POST to the order +report URI with a request body containing the name of the report +and the contents of the report file. The content type (MIME type) of the data is recorded with the report. If it is `text/html` or `text/plain`, the content will be @@ -732,6 +733,31 @@ For an example add report script, see [add_report.py](https://github.com/pekrau/OrderPortal/blob/master/api_scripts/add_report.py "!"). +### API Edit order report + +A report for an order can be edited by doing a POST to the order +report URI containing the report UUID, and a request body containing +the name of the report and the contents of the report file. + +The content type (MIME type) of the data is recorded with the +report. If it is `text/html` or `text/plain`, the content will be +display in-line in the user's browser. Otherwise the content will be +downloaded as a file to the user's browser when the report button is +clicked. + +For an example edit report script, see +[edit_report.py](https://github.com/pekrau/OrderPortal/blob/master/api_scripts/edit_report.py "!"). + + +### API Delete order report + +A report for an order can be deleted by doing a DELETE to the order +report URI containing the report UUID. No request body should be used. + +For an example delete report script, see +[delete_report.py](https://github.com/pekrau/OrderPortal/blob/master/api_scripts/delete_report.py "!"). + + ## CLI The Command Line Interface (CLI) allows system various maintenance @@ -740,6 +766,8 @@ on the command line of the machine which hosts the OrderPortal instance. This means that only users with accounts of sufficient privilege on this machine can use it. + $ python3 cli.py --help + # Backup @@ -813,7 +841,7 @@ The current installation procedure is described in the `README.md` for the The implementation of Anubis is based on the following design decisions: -- The back-end is written in Python using [tornado](https://pypi.org/project/tornado/ "!"). +- The back-end is written in Python 3 using [tornado](https://pypi.org/project/tornado/ "!"). - The back-end generates HTML for display using the tornado template system. - The front-end uses [Bootstrap](https://getbootstrap.com/docs/3.4/ "!"). - The back-end uses the No-SQL database [CouchDB](https://couchdb.apache.org/ "!"). diff --git a/orderportal/report.py b/orderportal/report.py index 061d60d..69956b4 100644 --- a/orderportal/report.py +++ b/orderportal/report.py @@ -416,6 +416,9 @@ def get(self, iuid): self.write(self.get_report_json(report)) def post(self, iuid): + if self.get_argument("_http_method", None) == "delete": + self.delete(iuid) + return try: self.check_staff() except ValueError as error: @@ -444,6 +447,19 @@ def post(self, iuid): raise tornado.web.HTTPError(400, reason=str(error)) self.write(self.get_report_json(report, self.get_order(report["order"]))) + def delete(self, iuid): + try: + self.check_staff() + except ValueError as error: + raise tornado.web.HTTPError(403, reason=str(error)) + try: + report = self.get_report(iuid) + except ValueError as error: + raise tornado.web.HTTPError(404, reason=str(error)) + self.delete_logs(report["_id"]) + self.db.delete(report) + self.set_status(204) # Empty content. + class ReportEdit(ReportMixin, RequestHandler): "Edit a report for an order." From 8e3c7c6407886f4c317f2f75c8cc53b027f9dd86 Mon Sep 17 00:00:00 2001 From: Per Kraulis Date: Sat, 28 Oct 2023 17:27:59 +0200 Subject: [PATCH 2/2] Minor cleanup. --- orderportal/report.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/orderportal/report.py b/orderportal/report.py index 69956b4..4c1eb7d 100644 --- a/orderportal/report.py +++ b/orderportal/report.py @@ -416,9 +416,6 @@ def get(self, iuid): self.write(self.get_report_json(report)) def post(self, iuid): - if self.get_argument("_http_method", None) == "delete": - self.delete(iuid) - return try: self.check_staff() except ValueError as error: