aboutsummaryrefslogtreecommitdiffstats
path: root/python/fatcat_web
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2019-09-03 19:35:42 -0700
committerBryan Newbold <bnewbold@robocracy.org>2019-09-03 19:35:44 -0700
commitea3b8c33cf53b7e50cb353eac2c6f73f5cb75979 (patch)
treed441edde2ada1a7019f07bef9cfd20a9345b984a /python/fatcat_web
parent56e30786ccf326ff69deee9a0dcf86b032d3b1ae (diff)
downloadfatcat-ea3b8c33cf53b7e50cb353eac2c6f73f5cb75979.tar.gz
fatcat-ea3b8c33cf53b7e50cb353eac2c6f73f5cb75979.zip
include example releases on container landing page
Included making release search results an entity macro (in webface templates).
Diffstat (limited to 'python/fatcat_web')
-rw-r--r--python/fatcat_web/entity_helpers.py15
-rw-r--r--python/fatcat_web/search.py43
-rw-r--r--python/fatcat_web/templates/container_view.html6
-rw-r--r--python/fatcat_web/templates/entity_macros.html77
-rw-r--r--python/fatcat_web/templates/release_search.html77
5 files changed, 137 insertions, 81 deletions
diff --git a/python/fatcat_web/entity_helpers.py b/python/fatcat_web/entity_helpers.py
index b36307a1..d6a6eb26 100644
--- a/python/fatcat_web/entity_helpers.py
+++ b/python/fatcat_web/entity_helpers.py
@@ -3,7 +3,7 @@ from flask import abort
from fatcat_client.rest import ApiException
from fatcat_tools.transforms import *
from fatcat_web import app, api
-from fatcat_web.search import get_elastic_container_stats
+from fatcat_web.search import get_elastic_container_stats, get_elastic_container_random_releases
from fatcat_web.hacks import strip_extlink_xml, wayback_suffix
def enrich_container_entity(entity):
@@ -12,12 +12,13 @@ def enrich_container_entity(entity):
if entity.state == "active":
entity._es = container_to_elasticsearch(entity, force_bool=False)
entity._stats = None
- if entity.issnl:
- try:
- entity._stats = get_elastic_container_stats(entity.ident, issnl=entity.issnl)
- except Exception as e:
- app.log.error(e)
- pass
+ try:
+ entity._stats = get_elastic_container_stats(entity.ident, issnl=entity.issnl)
+ #if entity._stats['total'] > 0:
+ entity._random_releases = get_elastic_container_random_releases(entity.ident)
+ except Exception as e:
+ app.log.error(e)
+ pass
return entity
def enrich_creator_entity(entity):
diff --git a/python/fatcat_web/search.py b/python/fatcat_web/search.py
index 446a851a..94246329 100644
--- a/python/fatcat_web/search.py
+++ b/python/fatcat_web/search.py
@@ -6,6 +6,7 @@ the formal API)
TODO: ELASTICSEARCH_*_INDEX should probably be factored out and just hard-coded
"""
+import datetime
import requests
from flask import abort, flash
from fatcat_web import app
@@ -242,3 +243,45 @@ def get_elastic_container_stats(ident, issnl=None):
}
return stats
+
+def get_elastic_container_random_releases(ident, limit=5):
+ """
+ Returns a list of releases from the container.
+ """
+
+ assert limit > 0 and limit <= 100
+
+ query = {
+ "size": int(limit),
+ "sort": [
+ { "in_web": {"order": "desc"} },
+ { "release_date": {"order": "desc"} },
+ ],
+ "query": {
+ "bool": {
+ "must": [
+ { "term": { "container_id": ident } },
+ { "range": { "release_year": { "lte": datetime.datetime.today().year } } },
+ ],
+ },
+ },
+ }
+ resp = requests.get(
+ "{}/fatcat_release/_search".format(app.config['ELASTICSEARCH_BACKEND']),
+ json=query,
+ params=dict(request_cache="true"))
+ # TODO: abort()
+ #print(resp.json())
+ resp.raise_for_status()
+ resp = resp.json()
+ print(resp)
+ hits = [h['_source'] for h in resp['hits']['hits']]
+ for h in hits:
+ # Handle surrogate strings that elasticsearch returns sometimes,
+ # probably due to mangled data processing in some pipeline.
+ # "Crimes against Unicode"; production workaround
+ for key in h:
+ if type(h[key]) is str:
+ h[key] = h[key].encode('utf8', 'ignore').decode('utf8')
+
+ return hits
diff --git a/python/fatcat_web/templates/container_view.html b/python/fatcat_web/templates/container_view.html
index c2731db2..ac304e95 100644
--- a/python/fatcat_web/templates/container_view.html
+++ b/python/fatcat_web/templates/container_view.html
@@ -33,6 +33,12 @@
</div>
</div>
</form>
+
+{% if container._random_releases %}
+<h3>Example Publications</h3>
+{% for random_release in container._random_releases %}
+ {{ entity_macros.release_search_result_row(random_release) }}
+{% endfor %}
{% endif %}
</div>
diff --git a/python/fatcat_web/templates/entity_macros.html b/python/fatcat_web/templates/entity_macros.html
index 0e3943dd..6124630c 100644
--- a/python/fatcat_web/templates/entity_macros.html
+++ b/python/fatcat_web/templates/entity_macros.html
@@ -144,3 +144,80 @@
</div>
</form>
{%- endmacro %}
+
+{% macro release_search_result_row(paper) -%}
+<div>
+ <h4 style="margin-top: 1em; margin-bottom: 0px; font-size: 1.1em;">
+ <a href="/release/{{ paper.ident }}" style="color: #2224c7;">
+ {% if paper.title %}
+ {{ paper.title[:512] }}
+ {% if paper.title|length > 512 %}...{% endif %}
+ {% else %}
+ [blank]
+ {% endif %}
+ </a>
+ </h4>
+ {% if paper.best_pdf_url %}
+ <div style="float: right; padding: 4px;">
+ &nbsp;&nbsp;<a href="{{ paper.best_pdf_url }}" class="ui violet tag label"><i class="file icon"></i>fulltext</a>
+ </div>
+ {% endif %}
+ {#
+ <h5 style="margin-top: 4px; margin-bottom: 4px; font-size: 1em;">{{ ", ".join(paper.contrib_names[:12]) }}
+ {% if paper.contrib_names|length > 12 %}<i>(+{{ paper.contrib_names|length - 12 }} others)</i>{% endif %}
+ </h5>
+ #}
+ {% if paper.contrib_names %}
+ <div style="margin-top: 0px; margin-bottom: 0px; font-size: 1em;">
+ <b>
+ {{ ", ".join(paper.contrib_names[:12]) }}
+ {% if paper.contrib_names|length > 12 %}<i>(+{{ paper.contrib_names|length - 12 }} others)</i>{% endif %}
+ </b>
+ </div>
+ {% endif %}
+ {% if paper.release_year %}
+ {{ paper.release_year }}
+ {% endif %}
+ {% if paper.release_type %}
+ {% if paper.release_type in ("article-journal", "paper-conference") %}
+ <span class="ui black basic label small">{{ paper.release_type }}</span>
+ {% elif paper.release_type in ("book") %}
+ <span class="ui brown basic label small">{{ paper.release_type }}</span>
+ {% else %}
+ <span class="ui grey basic label small">{{ paper.release_type }}</span>
+ {% endif %}
+ {% endif %}
+ {% if paper.withdrawn_status %}
+ <span class="ui red label small">{{ paper.withdrawn_status }}</span>
+ {% endif %}
+ {% if paper.release_stage and paper.release_stage != "published" %}
+ <span class="ui pink basic label small">{{ paper.release_stage }}</span>
+ {% elif not paper.release_stage %}
+ <span class="ui red basic label small">unknown</span>
+ {% endif %}
+ {% if paper.container_name %}
+ {% if paper.container_id %}
+ <a href="/container/{{ paper.container_id }}" style="color: black;">{{ paper.container_name }}</a>
+ {% else %}
+ {{ paper.container_name }}
+ {% endif %}
+ {% if paper.container_is_oa %}<i class="icon unlock orange small"></i>{% endif %}
+ {% endif %}
+ {% if paper.doi or paper.pmid or paper.arxiv_id or paper.jstor_id %}
+ <br>
+ {% endif %}
+ {% if paper.doi %}
+ <a href="https://doi.org/{{paper.doi }}" style="color: green;">doi:{{ paper.doi }}</a> &nbsp;
+ {% endif %}
+ {% if paper.pmid %}
+ <a href="https://www.ncbi.nlm.nih.gov/pubmed/{{paper.pmid }}" style="color: green;">pmid:{{ paper.pmid }}</a> &nbsp;
+ {% endif %}
+ {% if paper.arxiv_id %}
+ <a href="https://arxiv.org/abs/{{paper.arxiv_id }}" style="color: green;">arXiv:{{ paper.arxiv_id }}</a> &nbsp;
+ {% endif %}
+ {% if False %} {# XXX: elastic release work grouping searches #}
+ <br>
+ <a href="/work/{{ paper.work_id }}"><i class="sitemap icon"></i> and 5 other versions of the same work!</a>
+ {% endif %}
+</div>
+{% endmacro %}
diff --git a/python/fatcat_web/templates/release_search.html b/python/fatcat_web/templates/release_search.html
index 59411c62..7d6b0443 100644
--- a/python/fatcat_web/templates/release_search.html
+++ b/python/fatcat_web/templates/release_search.html
@@ -1,3 +1,4 @@
+{% import "entity_macros.html" as entity_macros %}
{% extends "base.html" %}
{% block title %}
@@ -8,6 +9,7 @@
{% endif %}
{% endblock %}
+
{% block fullmain %}
<div class="ui vertical stripe segment" style="background-color: #EEE; padding-top: 4.5em;">
@@ -36,80 +38,7 @@
{% if found.results %}
<i>Showing top {{ found.count_returned }} out of {{ found.count_found }} results for: <code>{{ found.query.q }}</code></i>
{% for paper in found.results %}
-<div>
- <h4 style="margin-top: 1em; margin-bottom: 0px; font-size: 1.1em;">
- <a href="/release/{{ paper.ident }}" style="color: #2224c7;">
- {% if paper.title %}
- {{ paper.title[:512] }}
- {% if paper.title|length > 512 %}...{% endif %}
- {% else %}
- [blank]
- {% endif %}
- </a>
- </h4>
- {% if paper.best_pdf_url %}
- <div style="float: right; padding: 4px;">
- &nbsp;&nbsp;<a href="{{ paper.best_pdf_url }}" class="ui violet tag label"><i class="file icon"></i>fulltext</a>
- </div>
- {% endif %}
- {#
- <h5 style="margin-top: 4px; margin-bottom: 4px; font-size: 1em;">{{ ", ".join(paper.contrib_names[:12]) }}
- {% if paper.contrib_names|length > 12 %}<i>(+{{ paper.contrib_names|length - 12 }} others)</i>{% endif %}
- </h5>
- #}
- {% if paper.contrib_names %}
- <div style="margin-top: 0px; margin-bottom: 0px; font-size: 1em;">
- <b>
- {{ ", ".join(paper.contrib_names[:12]) }}
- {% if paper.contrib_names|length > 12 %}<i>(+{{ paper.contrib_names|length - 12 }} others)</i>{% endif %}
- </b>
- </div>
- {% endif %}
- {% if paper.release_year %}
- {{ paper.release_year }}
- {% endif %}
- {% if paper.release_type %}
- {% if paper.release_type in ("article-journal", "paper-conference") %}
- <span class="ui black basic label small">{{ paper.release_type }}</span>
- {% elif paper.release_type in ("book") %}
- <span class="ui brown basic label small">{{ paper.release_type }}</span>
- {% else %}
- <span class="ui grey basic label small">{{ paper.release_type }}</span>
- {% endif %}
- {% endif %}
- {% if paper.withdrawn_status %}
- <span class="ui red label small">{{ paper.withdrawn_status }}</span>
- {% endif %}
- {% if paper.release_stage and paper.release_stage != "published" %}
- <span class="ui pink basic label small">{{ paper.release_stage }}</span>
- {% elif not paper.release_stage %}
- <span class="ui red basic label small">unknown</span>
- {% endif %}
- {% if paper.container_name %}
- {% if paper.container_id %}
- <a href="/container/{{ paper.container_id }}" style="color: black;">{{ paper.container_name }}</a>
- {% else %}
- {{ paper.container_name }}
- {% endif %}
- {% if paper.container_is_oa %}<i class="icon unlock orange small"></i>{% endif %}
- {% endif %}
- {% if paper.doi or paper.pmid or paper.arxiv_id or paper.jstor_id %}
- <br>
- {% endif %}
- {% if paper.doi %}
- <a href="https://doi.org/{{paper.doi }}" style="color: green;">doi:{{ paper.doi }}</a> &nbsp;
- {% endif %}
- {% if paper.pmid %}
- <a href="https://www.ncbi.nlm.nih.gov/pubmed/{{paper.pmid }}" style="color: green;">pmid:{{ paper.pmid }}</a> &nbsp;
- {% endif %}
- {% if paper.arxiv_id %}
- <a href="https://arxiv.org/abs/{{paper.arxiv_id }}" style="color: green;">arXiv:{{ paper.arxiv_id }}</a> &nbsp;
- {% endif %}
- {% if False %} {# XXX: elastic release work grouping searches #}
- <br>
- <a href="/work/{{ paper.work_id }}"><i class="sitemap icon"></i> and 5 other versions of the same work!</a>
- {% endif %}
-</div>
+ {{ entity_macros.release_search_result_row(paper) }}
{% endfor %}
{% if found.results|length > 8 %}
<br>