From df5d0dab68204a95c666fa95dd12d0a601d87ce7 Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Mon, 8 Feb 2021 20:26:16 +0000 Subject: [PATCH 1/3] Add minimum scan dimension It doesn't make sense to allow fewer than 1 image in any dimension of a scan. Currently, supplying 0 gives a division by zero error. I've added min="1" to each control. --- .../tabContentComponents/captureComponents/paneCapture.vue | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/openflexure_microscope/api/static/src/components/tabContentComponents/captureComponents/paneCapture.vue b/openflexure_microscope/api/static/src/components/tabContentComponents/captureComponents/paneCapture.vue index 1de921e0..1c3a0c85 100644 --- a/openflexure_microscope/api/static/src/components/tabContentComponents/captureComponents/paneCapture.vue +++ b/openflexure_microscope/api/static/src/components/tabContentComponents/captureComponents/paneCapture.vue @@ -155,7 +155,7 @@ v-model="scanStepSize.z" class="uk-input uk-form-small" type="number" - name="inputPositionZx" + name="inputPositionZ" /> @@ -172,6 +172,7 @@ class="uk-input uk-form-small" type="number" name="inputPositionX" + min="1" /> @@ -186,6 +187,7 @@ class="uk-input uk-form-small" type="number" name="inputPositionY" + min="1" /> @@ -199,7 +201,8 @@ v-model="scanSteps.z" class="uk-input uk-form-small" type="number" - name="inputPositionZx" + name="inputPositionZ" + min="1" /> From 792e782968dc6000acacac98a8951c61417ea505 Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Tue, 9 Feb 2021 09:21:56 +0000 Subject: [PATCH 2/3] Validate scan shape on the server The scan API view now uses a schema with validation constraints to enforce a minimum size of 1 in any dimension. This requires better error handling in the app, which I've added. It also needs a fix in LabThings so the error propagates correctly --- openflexure_microscope/api/app.py | 15 ++++++++++++++- .../api/default_extensions/scan.py | 3 ++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/openflexure_microscope/api/app.py b/openflexure_microscope/api/app.py index 44659886..205e9333 100644 --- a/openflexure_microscope/api/app.py +++ b/openflexure_microscope/api/app.py @@ -23,7 +23,7 @@ import os from datetime import datetime import pkg_resources -from flask import abort, send_file +from flask import abort, send_file, jsonify from flask_cors import CORS, cross_origin from labthings import create_app from labthings.extensions import find_extensions @@ -105,6 +105,19 @@ app, labthing = create_app( # Enable CORS for some routes outside of LabThings cors: CORS = CORS(app) +# Enable correct handling of Marshmallow/Webargs validation errors +# Return validation errors as JSON +# (see https://webargs.readthedocs.io/en/latest/framework_support.html) +@app.errorhandler(422) +@app.errorhandler(400) +def handle_error(err): + headers = err.data.get("headers", None) + messages = err.data.get("messages", ["Invalid request."]) + if headers: + return jsonify({"errors": messages}), err.code, headers + else: + return jsonify({"errors": messages}), err.code + # Use custom JSON encoder labthing.json_encoder = JSONEncoder app.json_encoder = JSONEncoder diff --git a/openflexure_microscope/api/default_extensions/scan.py b/openflexure_microscope/api/default_extensions/scan.py index 5a2365c4..e05f7352 100644 --- a/openflexure_microscope/api/default_extensions/scan.py +++ b/openflexure_microscope/api/default_extensions/scan.py @@ -2,6 +2,7 @@ import datetime import logging import time import uuid +import marshmallow from functools import reduce from typing import Dict, List, Optional, Tuple @@ -361,7 +362,7 @@ class ScanExtension(BaseExtension): class TileScanArgs(FullCaptureArgs): namemode = fields.String(missing="coordinates", example="coordinates") - grid = fields.List(fields.Integer, missing=[3, 3, 3], example=[3, 3, 3]) + grid = fields.List(fields.Integer(validate=marshmallow.validate.Range(min=1)), missing=[3, 3, 3], example=[3, 3, 3], ) style = fields.String(missing="raster") autofocus_dz = fields.Integer(missing=50) fast_autofocus = fields.Boolean(missing=False) From f2a4b976b9a93dca8a639e8694e6643fb3cfe121 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 13 May 2021 10:43:50 +0100 Subject: [PATCH 3/3] Fix whitespace --- openflexure_microscope/api/app.py | 3 ++- openflexure_microscope/api/default_extensions/scan.py | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/openflexure_microscope/api/app.py b/openflexure_microscope/api/app.py index 205e9333..c27bc90d 100644 --- a/openflexure_microscope/api/app.py +++ b/openflexure_microscope/api/app.py @@ -106,7 +106,7 @@ app, labthing = create_app( cors: CORS = CORS(app) # Enable correct handling of Marshmallow/Webargs validation errors -# Return validation errors as JSON +# Return validation errors as JSON # (see https://webargs.readthedocs.io/en/latest/framework_support.html) @app.errorhandler(422) @app.errorhandler(400) @@ -118,6 +118,7 @@ def handle_error(err): else: return jsonify({"errors": messages}), err.code + # Use custom JSON encoder labthing.json_encoder = JSONEncoder app.json_encoder = JSONEncoder diff --git a/openflexure_microscope/api/default_extensions/scan.py b/openflexure_microscope/api/default_extensions/scan.py index e05f7352..782ad8f4 100644 --- a/openflexure_microscope/api/default_extensions/scan.py +++ b/openflexure_microscope/api/default_extensions/scan.py @@ -362,7 +362,11 @@ class ScanExtension(BaseExtension): class TileScanArgs(FullCaptureArgs): namemode = fields.String(missing="coordinates", example="coordinates") - grid = fields.List(fields.Integer(validate=marshmallow.validate.Range(min=1)), missing=[3, 3, 3], example=[3, 3, 3], ) + grid = fields.List( + fields.Integer(validate=marshmallow.validate.Range(min=1)), + missing=[3, 3, 3], + example=[3, 3, 3], + ) style = fields.String(missing="raster") autofocus_dz = fields.Integer(missing=50) fast_autofocus = fields.Boolean(missing=False)