From 526e291f71b4f7018708de61b9e698b0ee9d894c Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Fri, 11 Feb 2022 12:25:52 -0800 Subject: containers: iterate on browse interface, showing year/volume/issue --- python/fatcat_web/routes.py | 42 ++++++------ python/fatcat_web/search.py | 29 +++++---- .../templates/container_view_browse.html | 74 ++++++++++++++++------ 3 files changed, 94 insertions(+), 51 deletions(-) (limited to 'python') diff --git a/python/fatcat_web/routes.py b/python/fatcat_web/routes.py index 6e3e9b57..2f29b9e6 100644 --- a/python/fatcat_web/routes.py +++ b/python/fatcat_web/routes.py @@ -60,7 +60,7 @@ from fatcat_web.search import ( ReleaseQuery, do_container_search, do_release_search, - get_elastic_container_browse_year_volume, + get_elastic_container_browse_year_volume_issue, get_elastic_container_histogram_legacy, get_elastic_container_preservation_by_volume, get_elastic_container_random_releases, @@ -357,11 +357,11 @@ def container_view_browse(ident: str) -> AnyResponse: return render_template("deleted_entity.html", entity_type="container", entity=entity) query_sort: Optional[List[str]] - if request.args.get('year') and 'volume' in request.args and 'issue' in request.args: + if request.args.get("year") and "volume" in request.args and "issue" in request.args: # year, volume, issue specified; browse-by-page - year = int(request.args.get('year')) - volume = request.args.get('volume', '') - issue = request.args.get('issue', '') + year = int(request.args.get("year")) + volume = request.args.get("volume", "") + issue = request.args.get("issue", "") if volume: volume = f'volume:"{volume}"' else: @@ -370,27 +370,33 @@ def container_view_browse(ident: str) -> AnyResponse: issue = f'issue:"{issue}"' else: issue = "!issue:*" - query_string = f'year:{year} {volume} {issue}' + query_string = f"year:{year} {volume} {issue}" query_sort = ["first_page", "release_date"] - elif request.args.get('year') and 'volume' in request.args: + elif request.args.get("year") and "volume" in request.args: # year, volume specified (no issue); browse-by-page - year = int(request.args.get('year')) - volume = request.args.get('volume', '') + year = int(request.args.get("year")) + volume = request.args.get("volume", "") if volume: volume = f'volume:"{volume}"' else: volume = "!volume:*" - query_string = f'year:{year} {volume}' + query_string = f"year:{year} {volume}" query_sort = ["issue", "first_page", "release_date"] - elif request.args.get('year'): + elif request.args.get("year"): # year specified, not anything else; browse-by-date - year = int(request.args.get('year')) + year = int(request.args.get("year")) query_string = f"year:{year}" query_sort = ["release_date"] else: - entity._browse_volume_year = get_elastic_container_browse_year_volume(entity.ident) + entity._browse_volume_year = get_elastic_container_browse_year_volume_issue( + entity.ident + ) + print(entity._browse_volume_year) return render_template( - "container_view_browse.html", entity_type="container", entity=entity, editgroup_id=None + "container_view_browse.html", + entity_type="container", + entity=entity, + editgroup_id=None, ) print(query_string) @@ -421,11 +427,11 @@ def container_view_browse(ident: str) -> AnyResponse: ) # HACK: re-sort by first page *numerically* - if found.results and query_sort and 'first_page' in query_sort: + if found.results and query_sort and "first_page" in query_sort: for doc in found.results: - if doc.get('first_page') and doc['first_page'].isdigit(): - doc['first_page'] = int(doc['first_page']) - found.results = sorted(found.results, key=lambda d: d.get('first_page') or 99999999) + if doc.get("first_page") and doc["first_page"].isdigit(): + doc["first_page"] = int(doc["first_page"]) + found.results = sorted(found.results, key=lambda d: d.get("first_page") or 99999999) return render_template( "container_view_browse.html", diff --git a/python/fatcat_web/search.py b/python/fatcat_web/search.py index 3f4a216c..e79fcd8d 100644 --- a/python/fatcat_web/search.py +++ b/python/fatcat_web/search.py @@ -247,13 +247,9 @@ def get_elastic_container_random_releases(ident: str, limit: int = 5) -> List[Di return results -def get_elastic_container_browse_year_volume(ident: str) -> Dict[int, Dict[str, Any]]: +def get_elastic_container_browse_year_volume_issue(ident: str) -> Dict[int, Dict[str, Any]]: """ - Returns a set of histogram buckets: - - container_ident: str - years{} - volumes{} + Returns a set of histogram buckets, by year (int), volume (str), issue (str) """ search = Search(using=app.es_client, index=app.config["ELASTICSEARCH_RELEASE_INDEX"]) @@ -284,9 +280,14 @@ def get_elastic_container_browse_year_volume(ident: str) -> Dict[int, Dict[str, "terms": { "field": "volume", "missing_bucket": True, - # TODO: es-public-proxy support? - # "order": "asc", - # "missing_order": "last", + }, + } + }, + { + "issue": { + "terms": { + "field": "issue", + "missing_bucket": True, }, } }, @@ -304,10 +305,12 @@ def get_elastic_container_browse_year_volume(ident: str) -> Dict[int, Dict[str, for year in year_nums: year_dicts[year] = {} for row in buckets: - year_dicts[int(row["key"]["year"])][row["key"]["volume"] or "000_unknown"] = int( - row["doc_count"] - ) - # return sorted(year_dicts.values(), key=lambda x: x["year"]) + year = int(row["key"]["year"]) + volume = row["key"]["volume"] or "000_unknown" + issue = row["key"]["issue"] or "000_unknown" + if not volume in year_dicts[year]: + year_dicts[year][volume] = {} + year_dicts[year][volume][issue] = int(row["doc_count"]) return year_dicts diff --git a/python/fatcat_web/templates/container_view_browse.html b/python/fatcat_web/templates/container_view_browse.html index aa88b666..f6a74a2d 100644 --- a/python/fatcat_web/templates/container_view_browse.html +++ b/python/fatcat_web/templates/container_view_browse.html @@ -4,25 +4,50 @@ {% import "entity_macros.html" as entity_macros %} {% extends "entity_base.html" %} -{% macro browse_year_volume_table(entity, data) %} +{% macro browse_year_volume_issue_table(entity, data) %} + + + + {% for year in data.keys()|sort|reverse %} + {% set year_loop = loop %} {% for volume in data[year].keys()|sort|reverse %} - - {% if loop.first %} - - {% endif %} - {% if volume == '000_unknown' %} - - - + {% set volume_loop = loop %} + {% for issue in data[year][volume].keys()|sort|reverse %} + {% set issue_loop = loop %} + + {% if volume_loop.first and issue_loop.first %} + {% set year_rowspan = data[year].values()|map('length')|sum %} + + {% endif %} + + {% if issue_loop.first %} + + {% endif %} + + + + + + {% endfor %} {% endfor %} {% endfor %} @@ -30,15 +55,24 @@ {% endmacro %} {% macro browse_releases(found) %} +

+ Browsing: + {% if request.args.volume %}Volume {{ request.args.volume }}  {% endif %} + {% if request.args.issue %}Issue {{ request.args.issue }}  {% endif %} + {% if request.args.year %}Year {{ request.args.year }}  {% endif %} +

+
{% for release_doc in found.results %}
- {% if release_doc.pages %} - {{ release_doc.pages }} - {# + {% if request.args.volume %} + {% if release_doc.pages %} + {{ release_doc.pages }} + {% else %} + {# blank #} + {% endif %} {% elif release_doc.release_date %} {{ release_doc.release_date }} - #} {% endif %}
@@ -55,7 +89,7 @@ {% elif entity._browse_volume_year %}

Contents by Year and Volume

- {{ browse_year_volume_table(entity, entity._browse_volume_year) }} + {{ browse_year_volume_issue_table(entity, entity._browse_volume_year) }}
{% endif %} -- cgit v1.2.3
Year + Volume + Issue + Indexed Content +
- {{ year }} - {% if data[year]|length > 1 %}n/a{% endif %}{{ data[year][volume] }} releases - {% else %} - Vol. {{ volume }}{{ data[year][volume] }} releases - {% endif %} -
+ {{ year }} + + {% if volume != '000_unknown' %} + Vol. {{ volume }} + {% endif %} + + {% if issue != '000_unknown' %} + Issue {{ issue }} + {% endif %} + + {{ data[year][volume][issue] }} releases +