Added web routes for tagging captures

This commit is contained in:
Joel Collins 2019-01-22 12:09:42 +00:00
parent 7a72b45b29
commit 0e23ce8d0b
3 changed files with 122 additions and 10 deletions

View file

@ -18,6 +18,11 @@ def construct_blueprint(microscope_obj):
'/capture/<capture_id>/metadata', '/capture/<capture_id>/metadata',
view_func=capture.MetadataRedirectAPI.as_view('metadata_download_redirect', microscope=microscope_obj)) view_func=capture.MetadataRedirectAPI.as_view('metadata_download_redirect', microscope=microscope_obj))
# Tag routes
blueprint.add_url_rule(
'/capture/<capture_id>/tags',
view_func=capture.TagsAPI.as_view('capture_tags', microscope=microscope_obj))
# Capture routes # Capture routes
blueprint.add_url_rule( blueprint.add_url_rule(
'/capture/<capture_id>/download/<filename>', '/capture/<capture_id>/download/<filename>',

View file

@ -1,5 +1,6 @@
from openflexure_microscope.api.utilities import gen, get_bool, JsonPayload from openflexure_microscope.api.utilities import gen, get_bool, JsonPayload
from openflexure_microscope.api.v1.views import MicroscopeView from openflexure_microscope.api.v1.views import MicroscopeView
from openflexure_microscope.utilities import filter_dict
from flask import Response, Blueprint, jsonify, request, abort, url_for, redirect, send_file from flask import Response, Blueprint, jsonify, request, abort, url_for, redirect, send_file
@ -224,7 +225,7 @@ class CaptureAPI(MicroscopeView):
.. sourcecode:: http .. sourcecode:: http
POST /camera/capture HTTP/1.1 PUT /camera/capture/d0b2067abbb946f19351e075c5e7cd5b HTTP/1.1
Accept: application/json Accept: application/json
{ {
@ -374,4 +375,108 @@ class MetadataAPI(MicroscopeView):
return Response( return Response(
data, data,
mimetype="text/yaml") mimetype="text/yaml")
class TagsAPI(MicroscopeView):
def get(self, capture_id):
"""
Return tag list for a capture.
.. :quickref: Capture; Show capture tags
**Example request**:
.. sourcecode:: http
GET /camera/capture/d0b2067abbb946f19351e075c5e7cd5b/tags HTTP/1.1
Accept: text/yaml
:>header Accept: text/yaml
:>header Content-Type: text/yaml
:status 200: capture data found
:status 404: no capture found with that id
"""
capture_obj = self.microscope.camera.image_from_id(capture_id)
if not capture_obj or not capture_obj.state['available']:
return abort(404) # 404 Not Found
metadata_tags = filter_dict(capture_obj.state, ('metadata', 'tags'))
return jsonify(metadata_tags)
def put(self, capture_id):
"""
Add tags to the capture
.. :quickref: Capture; Update capture tags
**Example request**:
.. sourcecode:: http
PUT /camera/capture/d0b2067abbb946f19351e075c5e7cd5b/tags HTTP/1.1
Accept: application/json
["tests", "mytag", "someothertag"]
:>header Accept: application/json
:<header Content-Type: application/json
:status 200: metadata updated
"""
capture_obj = self.microscope.camera.image_from_id(capture_id)
if not capture_obj or not capture_obj.state['available']:
return abort(404) # 404 Not Found
data_dict = JsonPayload(request).json
if type(data_dict) != list:
return abort(400)
for tag in data_dict:
capture_obj.put_tag(str(tag))
metadata_tags = filter_dict(capture_obj.state, ('metadata', 'tags'))
return jsonify(metadata_tags)
def delete(self, capture_id):
"""
Delete tags from the capture
.. :quickref: Capture; Delete tags.
**Example request**:
.. sourcecode:: http
DELETE /camera/capture/d0b2067abbb946f19351e075c5e7cd5b/tags HTTP/1.1
Accept: application/json
["tests", "mytag"]
:>header Accept: application/json
:<header Content-Type: application/json
:status 200: metadata updated
"""
capture_obj = self.microscope.camera.image_from_id(capture_id)
if not capture_obj:
return abort(404) # 404 Not Found
data_dict = JsonPayload(request).json
if type(data_dict) != list:
return abort(400)
for tag in data_dict:
capture_obj.delete_tag(str(tag))
metadata_tags = filter_dict(capture_obj.state, ('metadata', 'tags'))
return jsonify(metadata_tags)

View file

@ -83,9 +83,10 @@ class CaptureObject(object):
self.folder = folder self.folder = folder
# Dictionary for storing custom metadata # Dictionary for storing custom metadata
self._metadata = { self._metadata = {}
'tags': []
} # List for storing tags
self.tags = []
# Initialise the capture stream # Initialise the capture stream
self.initialise_capture(create_metadata_file=create_metadata_file) self.initialise_capture(create_metadata_file=create_metadata_file)
@ -203,12 +204,12 @@ class CaptureObject(object):
# HANDLE TAGS # HANDLE TAGS
def put_tag(self, tag: str): def put_tag(self, tag: str):
if not tag in self._metadata['tags']: if not tag in self.tags:
self._metadata['tags'].append(tag) self.tags.append(tag)
def delete_tag(self, tag: str): def delete_tag(self, tag: str):
if tag in self._metadata['tags']: if tag in self.tags:
self._metadata['tags'] = [new_tag for new_tag in self._metadata['tags'] if new_tag != tag] self.tags = [new_tag for new_tag in self.tags if new_tag != tag]
# HANDLE METADATA # HANDLE METADATA
@ -238,7 +239,8 @@ class CaptureObject(object):
'filename': self.filename, 'filename': self.filename,
'path': self.file, 'path': self.file,
'time': self.timestring, 'time': self.timestring,
'format': self.format 'format': self.format,
'tags': self.tags
} }
# Add custom metadata to dictionary # Add custom metadata to dictionary