Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions omeroweb/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,24 @@
GET Groups that an Experimenter is in, using omero-marshal to generate json
"""

api_annotations = re_path(
r"^v(?P<api_version>%s)/m/annotations/$" % versions,
views.AnnotationsView.as_view(),
name="api_annotations",
)
"""
GET Annotations, using omero-marshal to generate json
"""

api_namedannotations = re_path(
r"^v(?P<api_version>%s)/m/(?P<ann_type>file|map|tag|long|timestamp|comment|boolean|double|xml|term)annotations/$" % versions,
views.AnnotationsView.as_view(),
name="api_namedannotations",
)
"""
GET Annotations, using omero-marshal to generate json
"""

urlpatterns = [
api_versions,
api_base,
Expand Down Expand Up @@ -443,4 +461,6 @@
api_groups,
api_group,
api_experimenter_groups,
api_annotations,
api_namedannotations,
]
99 changes: 98 additions & 1 deletion omeroweb/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from django.middleware import csrf
from django.utils.decorators import method_decorator
from django.urls import reverse
from requests import request
from . import api_settings

import traceback
Expand Down Expand Up @@ -86,6 +87,7 @@ def api_base(request, api_version=None, **kwargs):
"url:screens": build_url(request, "api_screens", v),
"url:plates": build_url(request, "api_plates", v),
"url:rois": build_url(request, "api_rois", v),
"url:annotations": build_url(request, "api_annotations", v),
"url:token": build_url(request, "api_token", v),
"url:servers": build_url(request, "api_servers", v),
"url:login": build_url(request, "api_login", v),
Expand Down Expand Up @@ -441,6 +443,10 @@ class ExperimenterGroupView(ObjectView):
class ObjectsView(ApiView):
"""Base class for listing objects."""

def get_omero_type(self, request):
"""Allow dynamic omero type, e.g. for AnnotationsView."""
return self.OMERO_TYPE

def get_opts(self, request, **kwargs):
"""Return an options dict based on request parameters."""
try:
Expand Down Expand Up @@ -468,7 +474,8 @@ def get(self, request, conn=None, **kwargs):
group = getIntOrDefault(request, "group", -1)
normalize = request.GET.get("normalize", False) == "true"
# Get the data
marshalled = query_objects(conn, self.OMERO_TYPE, group, opts, normalize)
marshalled = query_objects(conn, self.get_omero_type(request),
group, opts, normalize)
for m in marshalled["data"]:
self.add_data(m, request, conn, self.urls, **kwargs)
return marshalled
Expand Down Expand Up @@ -787,6 +794,96 @@ def add_data(self, marshalled, request, conn, urls=None, **kwargs):
return marshalled


class AnnotationsView(ObjectsView):
"""Handles GET for /annotations/ to list available Annotations."""

OMERO_TYPE = "Annotation"

def get_opts(self, request, **kwargs):
"""Add extra parameters to the opts dict."""
opts = super(AnnotationsView, self).get_opts(request, **kwargs)

# All annotatable objects
otypes = [
"Annotation",
"Channel",
"Dataset",
"Detector",
"Dichroic",
"Experimenter",
"ExperimenterGroup",
"Fileset",
"Filter",
"Folder",
"Image",
"Instrument",
"LightPath",
"LightSource",
"Namespace",
"Node",
"Objective",
"OriginalFile",
"PlaneInfo",
"PlateAcquisition",
"Plate",
"Project",
"Reagent",
"Roi",
"Screen",
"Session",
"Shape",
"Well",
]
request_otypes = {}
for key in otypes:
# parent_type is case-insensitive...
# but the JSON api expects lower-case
key = key.lower()
ids = request.GET.getlist(key)
if len(ids) > 0:
request_otypes[key] = [int(i) for i in ids]
# Check that only ONE parent type is specified
if len(request_otypes) > 1:
raise BadRequestError(
"Can only filter by one parent type at a time. "
"Found: %s" % ", ".join(request_otypes.keys())
)
elif len(request_otypes) == 1:
opts["parent_type"] = list(request_otypes.keys())[0]
opts["parent_ids"] = request_otypes[opts["parent_type"]]

if request.GET.get("ns") is not None:
opts["ns"] = request.GET.get("ns")

return opts

def get(self, request, conn=None, **kwargs):
"""Override get() to allow filtering by annotation type."""

# set self.OMERO_TYPE, then call super().get() to get the list of Annotations
# E.g. conn.getObjects("TagAnnotation") - not actually case-sensitive

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the tests of test_show in https://merge-ci.openmicroscopy.org/jenkins/job/OMERO-test-integration/lastCompletedBuild/testReport/ might be failing because of this change - the timing of the commit and the timing of the tests starting to fail matches.

# We support /tagannotations/
ann_type = kwargs.get("ann_type", None)
# OR /annotations/?type=tag
if ann_type is None:
ann_type = request.GET.get("type")
if ann_type in (
"file",
"map",
"tag",
"long",
"timestamp",
"comment",
"boolean",
"double",
"xml",
"term",
):
self.OMERO_TYPE = ann_type.capitalize() + "Annotation"
elif ann_type is not None:
raise BadRequestError("Invalid annotation type: %s" % ann_type)
return super(AnnotationsView, self).get(request, conn, **kwargs)

class ExperimentersView(ObjectsView):
"""Handles GET for /experimenters/ to list Experimenters."""

Expand Down
Loading