diff --git a/app.py b/app.py index 81c2eef..25245a6 100755 --- a/app.py +++ b/app.py @@ -23,7 +23,7 @@ from flask import ( session, url_for, ) -from requests_oauthlib import OAuth1Session # type: ignore +from requests_oauthlib import OAuth1Session from sqlalchemy import distinct, func from sqlalchemy.orm import aliased 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: if endpoint is None: endpoint = request.endpoint - assert endpoint and request.view_args is not None + assert endpoint and request.view_args args = request.view_args.copy() args.update(request.args) args.update(new_args) @@ -475,7 +475,7 @@ def item_page(item_id: int) -> str | Response: existing_depicts = existing_depicts_from_entity(entity) - width = 1200 + width = 800 image_filename = item.image_filename if image_filename: 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 [] 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 not wdqs.is_artificial_physical_object(qid): return render_template( @@ -528,6 +528,7 @@ def item_page(item_id: int) -> str | Response: catalog = wd_catalog.get_catalog_from_artwork(entity) if not catalog.get("institution"): institution = get_institution(entity, other) + assert institution catalog["institution"] = institution return render_template( diff --git a/depicts/artwork.py b/depicts/artwork.py index 8a629ba..88ee450 100644 --- a/depicts/artwork.py +++ b/depicts/artwork.py @@ -1,6 +1,6 @@ """Class to represent artwork.""" -from .mediawiki import get_entity, get_entity_with_cache +from . import mediawiki from .type import Claims, Entity, Sitelinks @@ -12,7 +12,7 @@ class Artwork: def __init__(self, qid: str) -> None: """Init.""" - entity = get_entity_with_cache(qid) + entity = mediawiki.get_entity_with_cache(qid) assert entity self.entity = entity self.item_id = int(qid[1:]) @@ -48,7 +48,7 @@ class Artwork: self.artist_entities = [] for artist in self.artists_claim: - artist_entity = get_entity(artist["id"]) + artist_entity = mediawiki.get_entity(artist["id"]) assert artist_entity self.artist_entities.append(artist_entity) diff --git a/depicts/mediawiki.py b/depicts/mediawiki.py index 4331bc7..d6fc761 100644 --- a/depicts/mediawiki.py +++ b/depicts/mediawiki.py @@ -8,6 +8,7 @@ import typing import requests from . import utils +from .category import Category from .type import CallParams, Entity wikidata_url = "https://www.wikidata.org/w/api.php" @@ -199,6 +200,30 @@ def host_from_site(site: str) -> str: 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]: """Get history of a page.""" params: CallParams = { diff --git a/depicts/model.py b/depicts/model.py index 835412b..01912b6 100644 --- a/depicts/model.py +++ b/depicts/model.py @@ -112,10 +112,8 @@ class Item(Base): @property def date(self) -> str | None: v = wikibase.first_datavalue(typing.cast(Entity, self.entity), "P571") - if not v: - return None 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): diff --git a/depicts/type.py b/depicts/type.py index 0ce71b2..3418f47 100644 --- a/depicts/type.py +++ b/depicts/type.py @@ -28,8 +28,8 @@ class Entity(TypedDict, total=False): class CatalogDict(TypedDict, total=False): """Catalog record from institution web site.""" - institution: str | None - url: str | None + institution: str + url: str ids: set[str] detail: list[dict[str, str]] description: str diff --git a/depicts/wd_catalog.py b/depicts/wd_catalog.py index ca5b781..22bfa8a 100644 --- a/depicts/wd_catalog.py +++ b/depicts/wd_catalog.py @@ -399,6 +399,7 @@ def get_catalog_from_artwork(entity: Entity) -> CatalogDict: catalog_detail.append(detail) url = wikibase.first_datavalue(entity, "P973") + assert isinstance(url, str) catalog: CatalogDict = { "url": url, "detail": catalog_detail, diff --git a/depicts/wikibase.py b/depicts/wikibase.py index cc81d05..c7c1a52 100644 --- a/depicts/wikibase.py +++ b/depicts/wikibase.py @@ -13,7 +13,7 @@ def first_datavalue( mainsnak = entity["claims"][pid][0]["mainsnak"] if "datavalue" in mainsnak: v = mainsnak["datavalue"]["value"] - assert isinstance(v, (str, int, dict)) + assert isinstance(v, str | int) return v return None diff --git a/templates/macro.html b/templates/macro.html index 9e2e2d1..faf8686 100644 --- a/templates/macro.html +++ b/templates/macro.html @@ -1,6 +1,3 @@ -{# vim: set ft=htmljinja -#} - {% macro render_pagination(pagination) %} {% if pagination.pages > 1 %}