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,
url_for,
)
from requests_oauthlib import OAuth1Session
from requests_oauthlib import OAuth1Session # type: ignore
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
assert endpoint and request.view_args is not None
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 = 800
width = 1200
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) # type: ignore
artwork_item = Item.query.get(item_id)
if artwork_item is None:
if not wdqs.is_artificial_physical_object(qid):
return render_template(
@ -528,7 +528,6 @@ 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(

View file

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

View file

@ -8,7 +8,6 @@ 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"
@ -200,30 +199,6 @@ 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 = {

View file

@ -112,8 +112,10 @@ 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"]) if v else None
return utils.format_time(v["time"], v["precision"])
class Triple(Base):

View file

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

View file

@ -399,7 +399,6 @@ 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,

View file

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

View file

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

View file

@ -1,6 +1,6 @@
{% macro nav_item(name, label) %}
<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>
{% endmacro %}