From 5c488d8d5e4f2e9a44240b89e95e28ec63a569a3 Mon Sep 17 00:00:00 2001 From: Alok Saldanha Date: Fri, 2 Apr 2021 11:40:11 -0400 Subject: [PATCH] only include source path element in url when multiple sources --- cellxgene_gateway/cache_key.py | 13 ++++++------ cellxgene_gateway/filecrawl.py | 16 ++++++--------- cellxgene_gateway/flask_util.py | 20 ++++++++++++++++++- cellxgene_gateway/gateway.py | 5 +++-- cellxgene_gateway/templates/cache_status.html | 2 +- 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/cellxgene_gateway/cache_key.py b/cellxgene_gateway/cache_key.py index 975f3c8..7c9a013 100644 --- a/cellxgene_gateway/cache_key.py +++ b/cellxgene_gateway/cache_key.py @@ -15,8 +15,7 @@ # 3) somedir/dataset_annotations: an annotation directory. The corresponding h5ad must exist, but the directory may not. # in this case, descriptor == 'somedir/dataset_annotations', dataset == 'somedir/dataset.h5ad' -from flask.helpers import url_for - +from cellxgene_gateway import flask_util from cellxgene_gateway.items.item import Item from cellxgene_gateway.items.item_source import ItemSource, LookupResult @@ -41,12 +40,14 @@ class CacheKey: return self.source.get_local_path(self.annotation_item) def relaunch_url(self): - return url_for("do_relaunch", source=self.source_name, path=self.descriptor) + return flask_util.relaunch_url(self.descriptor, self.source_name) def gateway_basepath(self): - return ( - url_for("do_view", source_name=self.source_name, path=self.descriptor) + "/" - ) + return self.view_url + "/" + + @property + def view_url(self): + return flask_util.view_url(self.descriptor, self.source_name) @property def source_name(self): diff --git a/cellxgene_gateway/filecrawl.py b/cellxgene_gateway/filecrawl.py index c2d5a92..8a62818 100644 --- a/cellxgene_gateway/filecrawl.py +++ b/cellxgene_gateway/filecrawl.py @@ -10,23 +10,20 @@ import os import urllib.parse -from flask import url_for - -from cellxgene_gateway import env +from cellxgene_gateway import env, flask_util +from cellxgene_gateway.cache_key import CacheKey from cellxgene_gateway.dir_util import annotations_suffix, make_annotations, make_h5ad def render_annotations(item, item_source): - url = url_for( - "do_view", - path=item_source.get_annotations_subpath(item), - source_name=item_source.name, + url = flask_util.view_url( + item_source.get_annotations_subpath(item), item_source.name ) new_annotation = f"new" annotations = ( ", ".join( [ - f"{a.name}" + f"{a.name}" for a in item.annotations ] ) @@ -38,8 +35,7 @@ def render_annotations(item, item_source): def render_item(item, item_source): - url = url_for("do_view", path=item.descriptor, source_name=item_source.name) + "/" - item_string = f"
  • {item.name} {render_annotations(item, item_source)}
  • " + item_string = f"
  • {item.name} {render_annotations(item, item_source)}
  • " return item_string diff --git a/cellxgene_gateway/flask_util.py b/cellxgene_gateway/flask_util.py index 9132929..b594479 100644 --- a/cellxgene_gateway/flask_util.py +++ b/cellxgene_gateway/flask_util.py @@ -7,9 +7,27 @@ # OR CONDITIONS OF ANY KIND, either express or implied. See the License for # the specific language governing permissions and limitations under the License. -from flask import request +from flask import request, url_for def querystring(): qs = request.query_string.decode() return f"?{qs}" if len(qs) > 0 else "" + + +include_source_in_url = False + + +def url(endpoint, descriptor, source_name): + if include_source_in_url: + return url_for(endpoint, source_name=source_name, path=descriptor) + else: + return url_for(endpoint, path=descriptor) + + +def view_url(descriptor, source_name): + return url("do_view", descriptor, source_name) + + +def relaunch_url(descriptor, source_name): + return url("do_relaunch", descriptor, source_name) diff --git a/cellxgene_gateway/gateway.py b/cellxgene_gateway/gateway.py index e8b8b50..5b66c74 100644 --- a/cellxgene_gateway/gateway.py +++ b/cellxgene_gateway/gateway.py @@ -26,7 +26,7 @@ from flask_api import status from werkzeug.middleware.proxy_fix import ProxyFix from werkzeug.utils import secure_filename -from cellxgene_gateway import env +from cellxgene_gateway import env, flask_util from cellxgene_gateway.backend_cache import BackendCache from cellxgene_gateway.cache_entry import CacheEntryStatus from cellxgene_gateway.cache_key import CacheKey @@ -248,7 +248,7 @@ def do_relaunch(path): match.terminate() qs = request.query_string.decode() return redirect( - url_for("do_view", path=path) + (f"?{qs}" if len(qs) > 0 else ""), + key.view_url + (f"?{qs}" if len(qs) > 0 else ""), code=302, ) @@ -302,6 +302,7 @@ def main(): default_item_source = "local" if len(item_sources) == 0: raise Exception("Please specify CELLXGENE_DATA or CELLXGENE_BUCKET") + flask_util.include_source_in_url = len(item_sources) > 1 launch() diff --git a/cellxgene_gateway/templates/cache_status.html b/cellxgene_gateway/templates/cache_status.html index 5e2ff41..54e30c0 100644 --- a/cellxgene_gateway/templates/cache_status.html +++ b/cellxgene_gateway/templates/cache_status.html @@ -48,7 +48,7 @@ {{ entry.pid }} {{ entry.key.h5ad_item.descriptor }} + href="{{ entry.key.view_url }}">{{ entry.key.h5ad_item.descriptor }} {{ entry.key.annotation_descriptor }} {{ entry.source_name }}