Add language information to label on item page.

This commit is contained in:
Edward Betts 2019-09-30 10:19:12 +01:00
parent 6eb79ccfd5
commit 514c55cd6e
3 changed files with 62 additions and 3 deletions

41
app.py
View file

@ -5,13 +5,15 @@ from depicts import (utils, wdqs, commons, mediawiki, painting, saam, database,
dia, rijksmuseum, npg, museodelprado, barnesfoundation,
wd_catalog)
from depicts.pager import Pagination, init_pager
from depicts.model import DepictsItem, DepictsItemAltLabel, Edit, PaintingItem
from depicts.model import (DepictsItem, DepictsItemAltLabel, Edit, PaintingItem,
Language)
from depicts.error_mail import setup_error_mail
from requests_oauthlib import OAuth1Session
from urllib.parse import urlencode
from werkzeug.exceptions import InternalServerError
from werkzeug.debug.tbtools import get_current_traceback
from sqlalchemy import func, distinct
from collections import defaultdict
import requests.exceptions
import requests
import lxml.html
@ -124,6 +126,10 @@ select distinct ?item where {
}
'''
@app.teardown_appcontext
def shutdown_session(exception=None):
database.session.remove()
@app.errorhandler(InternalServerError)
def exception_handler(e):
tb = get_current_traceback()
@ -411,7 +417,8 @@ def item_page(item_id):
image = image_with_cache(qid, image_filename, width)
# hits = item.run_query()
label = get_entity_label(entity)
label_and_language = get_entity_label_and_language(entity)
label = label_and_language['label']
other = get_other(item.entity)
painting_item = PaintingItem.query.get(item_id)
@ -465,6 +472,8 @@ def item_page(item_id):
except requests.exceptions.ReadTimeout:
pass
label_languages = label_and_language['languages']
show_translation_links = all(lang.code != 'en' for lang in label_languages)
return render_template('item.html',
qid=qid,
item_id=item_id,
@ -476,6 +485,8 @@ def item_page(item_id):
entity=item.entity,
username=get_username(),
label=label,
label_languages=label_languages,
show_translation_links=show_translation_links,
image=image,
other=other,
# hits=hits,
@ -489,6 +500,32 @@ def get_entity_label(entity):
if len(label_values) == 1:
return list(label_values)[0]
def get_languages(codes):
return Language.query.filter(Language.wikimedia_language_code.in_(codes))
def get_entity_label_and_language(entity):
'''
Look for a useful label and return it with a list of languages that have that label.
If the entity has a label in English return it.
Otherwise check if all languages have the same label, if so then return it.
'''
group_by_label = defaultdict(set)
for language, l in entity['labels'].items():
group_by_label[l['value']].add(language)
if 'en' in entity['labels']:
label = entity['labels']['en']['value']
return {'label': label,
'languages': get_languages(group_by_label[label])}
if len(group_by_label) == 1:
label, languages = list(group_by_label.items())[0]
return {'label': label,
'languages': get_languages(languages)}
def get_labels(keys, name=None):
keys = sorted(keys, key=lambda i: int(i[1:]))
if name is None:

View file

@ -2,7 +2,7 @@ from sqlalchemy.ext.declarative import declarative_base
from .database import session, now_utc
from sqlalchemy.schema import Column, ForeignKey
from sqlalchemy.types import Integer, String, DateTime
from sqlalchemy.orm import column_property, relationship
from sqlalchemy.orm import column_property, relationship, synonym
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.sql.expression import cast
from sqlalchemy.dialects import postgresql
@ -43,6 +43,20 @@ class PaintingItem(Base):
entity = Column(postgresql.JSON)
qid = column_property('Q' + cast(item_id, String))
class Language(Base):
__tablename__ = 'language'
item_id = Column(Integer, primary_key=True, autoincrement=False)
wikimedia_language_code = Column(String, index=True, unique=True)
en_label = Column(String, nullable=False)
code = synonym('wikimedia_language_code')
label = synonym('en_label')
@classmethod
def get_by_code(cls, code):
return cls.query.filter_by(wikimedia_language_code=code).one()
class Edit(Base):
__tablename__ = 'edit'
username = Column(String, primary_key=True)

View file

@ -13,6 +13,14 @@
<div class="d-flex">
<div class="p-2 flex-fill">
<h1>{{ self.title() }}</h1>
<p>Label from:
{% for lang in label_languages %}
{{ lang.label }} ({{ lang.code }})
{% if show_translation_links %}
<a href="https://translate.google.com/#view=home&op=translate&sl={{lang.code}}&tl=en&text={{label}}" target="translation">[translate]</a>
{% endif %}
{% endfor %}
</p>
<p>
<a href="https://www.wikidata.org/wiki/{{ qid }}">view this painting on Wikidata</a>