From 62aff85ae612163d1c947d35348e34184ef8143a Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 7 Oct 2019 14:12:30 +0100 Subject: [PATCH] Show existing depicts on item page. --- app.py | 33 ++++++++++++++++++++++++++++++++- static/js/item.js | 3 ++- templates/item.html | 20 ++++++++++++++++++-- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 989d155..a7ae69d 100755 --- a/app.py +++ b/app.py @@ -498,11 +498,41 @@ def get_description_from_page(html): return twitter_description +def existing_depicts_from_entity(entity): + if 'P180' not in entity['claims']: + return [] + existing = [] + for claim in entity['claims']['P180']: + item_id = claim['mainsnak']['datavalue']['value']['numeric-id'] + + item = DepictsItem.query.get(item_id) + if item: + d = { + 'label': item.label, + 'description': item.description, + 'qid': item.qid, + 'count': item.count, + 'existing': True, + } + else: + qid = f'Q{item_id}' + d = { + 'label': 'not in db', + 'description': '', + 'qid': qid, + 'count': 0, + 'existing': True, + } + existing.append(d) + return existing + @app.route("/item/Q") def item_page(item_id): qid = f'Q{item_id}' item = painting.Painting(qid) - entity = mediawiki.get_entity_with_cache(qid) + entity = mediawiki.get_entity_with_cache(qid, refresh=True) + + existing_depicts = existing_depicts_from_entity(entity) width = 800 image_filename = item.image_filename @@ -600,6 +630,7 @@ def item_page(item_id): label=label, label_languages=label_languages, show_translation_links=show_translation_links, + existing_depicts=existing_depicts, image=image, other=other, # hits=hits, diff --git a/static/js/item.js b/static/js/item.js index 2466f6c..497eed5 100644 --- a/static/js/item.js +++ b/static/js/item.js @@ -8,10 +8,11 @@ var app = new Vue({ searchTerms: '', hits: [], new_depicts: [], + existing_depicts: existing_depicts, }, methods: { remove(index) { - this.$delete(this.new_depicts, index); + this.$delete(this.new_depicts, index); }, add_depicts(hit) { this.new_depicts.push(hit); diff --git a/templates/item.html b/templates/item.html index 28a5575..1633169 100644 --- a/templates/item.html +++ b/templates/item.html @@ -4,7 +4,8 @@ {% block style %} {% endblock %} @@ -104,6 +105,20 @@
{% raw %}
+
+
this painting has {{ existing_depicts.length }} existing depicts statement
+
+ +
+
+ {{ hit.label }} + ({{ hit.qid }}) +   + {{ hit.description }} + — {{ hit.count }} paintings +
+
+

what can you see in this painting?

@@ -118,7 +133,7 @@ remove — {{ hit.count }} existing paintings ({{ hit.qid }}) - view on Wikidata + [wikidata]
{{ hit.description }}
@@ -162,6 +177,7 @@ {% block script %}