Compare commits

...

9 commits

9 changed files with 18 additions and 40 deletions

9
app.py
View file

@ -23,7 +23,7 @@ from flask import (
session, session,
url_for, url_for,
) )
from requests_oauthlib import OAuth1Session from requests_oauthlib import OAuth1Session # type: ignore
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 assert endpoint and request.view_args is not None
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 = 800 width = 1200
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) # type: ignore artwork_item = Item.query.get(item_id)
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,7 +528,6 @@ 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 . import mediawiki from .mediawiki import get_entity, get_entity_with_cache
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 = mediawiki.get_entity_with_cache(qid) entity = 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 = mediawiki.get_entity(artist["id"]) artist_entity = 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,7 +8,6 @@ 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"
@ -200,30 +199,6 @@ 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,8 +112,10 @@ 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"]) if v else None return utils.format_time(v["time"], v["precision"])
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 institution: str | None
url: str url: str | None
ids: set[str] ids: set[str]
detail: list[dict[str, str]] detail: list[dict[str, str]]
description: str description: str

View file

@ -399,7 +399,6 @@ 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) assert isinstance(v, (str, int, dict))
return v return v
return None return None

View file

@ -1,3 +1,6 @@
{# 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">
@ -10,7 +13,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="sr-only">(current)</span></a></li> <li class="page-item active"><a class="page-link" href="{{ url_for_other_page(page) }}">{{ page }} <span class="visually-hidden">(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="sr-only">(current)</span>{% endif %}</a> <a class="nav-link" href="{{ url_for(name) }}">{{ label }}{% if name == active %} <span class="visually-hidden">(current)</span>{% endif %}</a>
</li> </li>
{% endmacro %} {% endmacro %}