Commit 0e23ce8d authored by Joel Collins's avatar Joel Collins
Browse files

Added web routes for tagging captures

parent 7a72b45b
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -18,6 +18,11 @@ def construct_blueprint(microscope_obj):
        '/capture/<capture_id>/metadata',
        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
    blueprint.add_url_rule(
        '/capture/<capture_id>/download/<filename>',
+107 −2
Original line number Diff line number Diff line
from openflexure_microscope.api.utilities import gen, get_bool, JsonPayload
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

@@ -224,7 +225,7 @@ class CaptureAPI(MicroscopeView):

        .. sourcecode:: http

          POST /camera/capture HTTP/1.1
          PUT /camera/capture/d0b2067abbb946f19351e075c5e7cd5b HTTP/1.1
          Accept: application/json

          {
@@ -375,3 +376,107 @@ class MetadataAPI(MicroscopeView):
        return Response(
            data,
            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)
 No newline at end of file
+10 −8
Original line number Diff line number Diff line
@@ -83,9 +83,10 @@ class CaptureObject(object):
        self.folder = folder

        # Dictionary for storing custom metadata
        self._metadata = {
            'tags': []
        }
        self._metadata = {}

        # List for storing tags
        self.tags = []

        # Initialise the capture stream
        self.initialise_capture(create_metadata_file=create_metadata_file)
@@ -203,12 +204,12 @@ class CaptureObject(object):

    # HANDLE TAGS
    def put_tag(self, tag: str):
        if not tag in self._metadata['tags']:
            self._metadata['tags'].append(tag)
        if not tag in self.tags:
            self.tags.append(tag)

    def delete_tag(self, tag: str):
        if tag in self._metadata['tags']:
            self._metadata['tags'] = [new_tag for new_tag in self._metadata['tags'] if new_tag != tag]
        if tag in self.tags:
            self.tags = [new_tag for new_tag in self.tags if new_tag != tag]

    # HANDLE METADATA

@@ -238,7 +239,8 @@ class CaptureObject(object):
            'filename': self.filename,
            'path': self.file,
            'time': self.timestring,
            'format': self.format
            'format': self.format,
            'tags': self.tags
        }

        # Add custom metadata to dictionary