Compare commits

..

No commits in common. "3b69855c25db4751588b8a64ddcef0b9904646a8" and "4446cbed6ef56346857048603d53106fdf1f8c0d" have entirely different histories.

9 changed files with 40 additions and 18 deletions

9
app.py
View file

@ -23,7 +23,7 @@ from flask import (
session, session,
url_for, url_for,
) )
from requests_oauthlib import OAuth1Session # type: ignore from requests_oauthlib import OAuth1Session
from sqlalchemy import distinct, func from sqlalchemy import distinct, func
from sqlalchemy.orm import aliased from sqlalchemy.orm import aliased
from sqlalchemy.sql.expression import desc from sqlalchemy.sql.expression import desc
@ -119,7 +119,7 @@ def shutdown_session(exception: Exception | None = None) -> None:
def set_url_args(endpoint: str | None = None, **new_args: str) -> str: def set_url_args(endpoint: str | None = None, **new_args: str) -> str:
if endpoint is None: if endpoint is None:
endpoint = request.endpoint endpoint = request.endpoint
assert endpoint and request.view_args is not None assert endpoint and request.view_args
args = request.view_args.copy() args = request.view_args.copy()
args.update(request.args) args.update(request.args)
args.update(new_args) args.update(new_args)
@ -475,7 +475,7 @@ def item_page(item_id: int) -> str | Response:
existing_depicts = existing_depicts_from_entity(entity) existing_depicts = existing_depicts_from_entity(entity)
width = 1200 width = 800
image_filename = item.image_filename image_filename = item.image_filename
if image_filename: if image_filename:
image = image_with_cache(qid, image_filename, width) image = image_with_cache(qid, image_filename, width)
@ -496,7 +496,7 @@ def item_page(item_id: int) -> str | Response:
label_languages = label_and_language["languages"] if label_and_language else [] label_languages = label_and_language["languages"] if label_and_language else []
show_translation_links = all(lang.code != "en" for lang in label_languages) show_translation_links = all(lang.code != "en" for lang in label_languages)
artwork_item = Item.query.get(item_id) artwork_item = Item.query.get(item_id) # type: ignore
if artwork_item is None: if artwork_item is None:
if not wdqs.is_artificial_physical_object(qid): if not wdqs.is_artificial_physical_object(qid):
return render_template( return render_template(
@ -528,6 +528,7 @@ def item_page(item_id: int) -> str | Response:
catalog = wd_catalog.get_catalog_from_artwork(entity) catalog = wd_catalog.get_catalog_from_artwork(entity)
if not catalog.get("institution"): if not catalog.get("institution"):
institution = get_institution(entity, other) institution = get_institution(entity, other)
assert institution
catalog["institution"] = institution catalog["institution"] = institution
return render_template( return render_template(

View file

@ -1,6 +1,6 @@
"""Class to represent artwork.""" """Class to represent artwork."""
from .mediawiki import get_entity, get_entity_with_cache from . import mediawiki
from .type import Claims, Entity, Sitelinks from .type import Claims, Entity, Sitelinks
@ -12,7 +12,7 @@ class Artwork:
def __init__(self, qid: str) -> None: def __init__(self, qid: str) -> None:
"""Init.""" """Init."""
entity = get_entity_with_cache(qid) entity = mediawiki.get_entity_with_cache(qid)
assert entity assert entity
self.entity = entity self.entity = entity
self.item_id = int(qid[1:]) self.item_id = int(qid[1:])
@ -48,7 +48,7 @@ class Artwork:
self.artist_entities = [] self.artist_entities = []
for artist in self.artists_claim: for artist in self.artists_claim:
artist_entity = get_entity(artist["id"]) artist_entity = mediawiki.get_entity(artist["id"])
assert artist_entity assert artist_entity
self.artist_entities.append(artist_entity) self.artist_entities.append(artist_entity)

View file

@ -8,6 +8,7 @@ import typing
import requests import requests
from . import utils from . import utils
from .category import Category
from .type import CallParams, Entity from .type import CallParams, Entity
wikidata_url = "https://www.wikidata.org/w/api.php" wikidata_url = "https://www.wikidata.org/w/api.php"
@ -199,6 +200,30 @@ def host_from_site(site: str) -> str:
return hosts[site] return hosts[site]
def process_cats(cats: list[dict[str, str]], site: str) -> list[Category]:
"""Process categories."""
return [Category(cat["title"], site) for cat in cats]
def get_categories(titles: list[str], site: str) -> list[tuple[str, list[Category]]]:
"""Get categories for pages with given titles."""
params: CallParams = {
"prop": "categories",
"clshow": "!hidden",
"cllimit": "max",
}
from_wiki = mediawiki_query(titles, params, site)
title_and_cats = []
for i in from_wiki:
if "categories" not in i:
continue
cats = process_cats(i["categories"], site)
if not cats:
continue
title_and_cats.append((i["title"], cats))
return title_and_cats
def get_history(title: str, site: str) -> list[Page]: def get_history(title: str, site: str) -> list[Page]:
"""Get history of a page.""" """Get history of a page."""
params: CallParams = { params: CallParams = {

View file

@ -112,10 +112,8 @@ class Item(Base):
@property @property
def date(self) -> str | None: def date(self) -> str | None:
v = wikibase.first_datavalue(typing.cast(Entity, self.entity), "P571") v = wikibase.first_datavalue(typing.cast(Entity, self.entity), "P571")
if not v:
return None
assert isinstance(v, dict) assert isinstance(v, dict)
return utils.format_time(v["time"], v["precision"]) return utils.format_time(v["time"], v["precision"]) if v else None
class Triple(Base): class Triple(Base):

View file

@ -28,8 +28,8 @@ class Entity(TypedDict, total=False):
class CatalogDict(TypedDict, total=False): class CatalogDict(TypedDict, total=False):
"""Catalog record from institution web site.""" """Catalog record from institution web site."""
institution: str | None institution: str
url: str | None url: str
ids: set[str] ids: set[str]
detail: list[dict[str, str]] detail: list[dict[str, str]]
description: str description: str

View file

@ -399,6 +399,7 @@ def get_catalog_from_artwork(entity: Entity) -> CatalogDict:
catalog_detail.append(detail) catalog_detail.append(detail)
url = wikibase.first_datavalue(entity, "P973") url = wikibase.first_datavalue(entity, "P973")
assert isinstance(url, str)
catalog: CatalogDict = { catalog: CatalogDict = {
"url": url, "url": url,
"detail": catalog_detail, "detail": catalog_detail,

View file

@ -13,7 +13,7 @@ def first_datavalue(
mainsnak = entity["claims"][pid][0]["mainsnak"] mainsnak = entity["claims"][pid][0]["mainsnak"]
if "datavalue" in mainsnak: if "datavalue" in mainsnak:
v = mainsnak["datavalue"]["value"] v = mainsnak["datavalue"]["value"]
assert isinstance(v, (str, int, dict)) assert isinstance(v, str | int)
return v return v
return None return None

View file

@ -1,6 +1,3 @@
{# vim: set ft=htmljinja
#}
{% macro render_pagination(pagination) %} {% macro render_pagination(pagination) %}
{% if pagination.pages > 1 %} {% if pagination.pages > 1 %}
<nav aria-label="Page navigation example"> <nav aria-label="Page navigation example">
@ -13,7 +10,7 @@
{% if page != pagination.page %} {% if page != pagination.page %}
<li class="page-item"><a class="page-link" href="{{ url_for_other_page(page) }}">{{ page }}</a></li> <li class="page-item"><a class="page-link" href="{{ url_for_other_page(page) }}">{{ page }}</a></li>
{% else %} {% else %}
<li class="page-item active"><a class="page-link" href="{{ url_for_other_page(page) }}">{{ page }} <span class="visually-hidden">(current)</span></a></li> <li class="page-item active"><a class="page-link" href="{{ url_for_other_page(page) }}">{{ page }} <span class="sr-only">(current)</span></a></li>
{% endif %} {% endif %}
{% else %} {% else %}
<li><span class="ellipsis"></span></li> <li><span class="ellipsis"></span></li>

View file

@ -1,6 +1,6 @@
{% macro nav_item(name, label) %} {% macro nav_item(name, label) %}
<li class="nav-item{% if name == active %} active{% endif %}"> <li class="nav-item{% if name == active %} active{% endif %}">
<a class="nav-link" href="{{ url_for(name) }}">{{ label }}{% if name == active %} <span class="visually-hidden">(current)</span>{% endif %}</a> <a class="nav-link" href="{{ url_for(name) }}">{{ label }}{% if name == active %} <span class="sr-only">(current)</span>{% endif %}</a>
</li> </li>
{% endmacro %} {% endmacro %}