Improvements

This commit is contained in:
Edward Betts 2023-09-21 04:59:17 +01:00
parent 77f2baea38
commit 9f3a7995a1
9 changed files with 532 additions and 110 deletions

100
main.py
View file

@ -10,7 +10,7 @@ from typing import cast
import flask
import requests
import sqlalchemy
from sqlalchemy import func, update
from sqlalchemy import func, or_, update
from werkzeug.wrappers import Response
from confarchive import database, model
@ -50,7 +50,7 @@ def wikidata_search(q: str) -> list[dict[str, typing.Any]]:
data = r.json()
time.sleep(1)
return cast(dict[str, typing.Any], data["query"]["search"])
return cast(list[dict[str, typing.Any]], data["query"]["search"])
def wikidata_get_item(qid: str) -> typing.Any:
@ -58,6 +58,7 @@ def wikidata_get_item(qid: str) -> typing.Any:
if os.path.exists(cache_filename):
item = json.load(open(cache_filename))
else:
print(qid)
params: dict[str, str | int] = {
"action": "wbgetentities",
"ids": qid,
@ -78,7 +79,7 @@ def top_speakers() -> sqlalchemy.orm.query.Query:
.join(model.ConferencePerson)
.group_by(model.Person)
.order_by(func.count().desc())
.having(func.count() > 5)
.having(func.count() > 3)
)
return q
@ -118,7 +119,9 @@ def person(person_id: int) -> str | Response:
flask.url_for(flask.request.endpoint, person_id=person_id)
)
return flask.render_template("person.html", item=item, Event=model.Event)
return flask.render_template(
"person.html", item=item, Event=model.Event, plural=plural
)
@app.route("/event/<int:event_id>")
@ -132,7 +135,9 @@ def conference_page(short_name: str) -> str:
item = model.Conference.query.filter_by(short_name=short_name).one_or_none()
if item is None:
flask.abort(404)
return flask.render_template("conference.html", item=item)
return flask.render_template(
"conference.html", item=item, person_image_filename=person_image_filename
)
@app.route("/people")
@ -157,9 +162,16 @@ def merge() -> str | Response:
merge_to_id = min(item_ids)
other_ids = [i for i in item_ids if i != merge_to_id]
name_from_person_id = flask.request.form["name"]
print(other_ids, "->", merge_to_id)
with database.session.begin():
if merge_to_id != name_from_person_id:
merge_to = model.Person.query.get(merge_to_id)
name_from_person = model.Person.query.get(name_from_person_id)
merge_to.name = name_from_person.name
print("update ConferencePerson")
database.session.execute(
update(model.ConferencePerson)
@ -224,15 +236,37 @@ def index() -> str:
"conference": model.Conference.query.count(),
"event": model.Event.query.count(),
"person": model.Person.query.count(),
"country": model.Country.query.count(),
"venue": model.Venue.query.count(),
}
return flask.render_template("index.html", items=q, count=count)
def plural(num: int, label: str) -> str:
return f'{num:,d} {label}{"s" if num != 1 else ""}'
def speaker_counts():
sql = """
select num, count(*)
from (select person_id, count(*) as num from conference_person group by person_id) a
group by num
order by num
"""
return database.session.execute(sql)
@app.route("/speakers")
def top_speakers_page() -> str:
"""Top speakers page."""
return flask.render_template("top_speakers.html", top_speakers=top_speakers())
return flask.render_template(
"top_speakers.html",
top_speakers=top_speakers(),
speaker_counts=speaker_counts(),
plural=plural,
)
@app.route("/country")
@ -264,11 +298,11 @@ def link_to_wikidata() -> str:
for person, num in top_speakers2():
if person.wikidata_qid:
continue
search_hits = wikidata_search(f'"{person.name}"')
search_hits = wikidata_search(person.name)
if not search_hits:
continue
if len(search_hits) > 10:
if len(search_hits) > 14:
continue
hits = []
@ -299,5 +333,55 @@ def link_to_wikidata() -> str:
return flask.render_template("wikidata.html", items=items)
@app.route("/search")
def search_everything() -> str:
search_for = flask.request.args["q"]
if not search_for:
return flask.render_template("search_everything.html")
search_for = search_for.strip()
like = f"%{search_for}%"
people = model.Person.query.filter(model.Person.name.ilike(like)).order_by(
model.Person.name
)
events = model.Event.query.filter(
or_(model.Event.abstract.ilike(like), model.Event.description.ilike(like))
).order_by(model.Event.event_date)
return flask.render_template(
"search_everything.html", people=people, events=events, search_for=search_for
)
@app.route("/person/<int:person_id>/delete", methods=["POST"])
def delete_person(person_id: int) -> str | Response:
item = model.Person.query.get(person_id)
for cp in item.conferences_association:
database.session.delete(cp)
for ep in item.events_association:
database.session.delete(ep)
database.session.delete(item)
database.session.commit()
return flask.redirect(flask.url_for("index"))
def person_image_filename(person_id):
person = model.Person.query.get(person_id)
return os.path.join("wikidata_photo", "thumb", person.wikidata_photo[0])
for filename in person.wikidata_photo:
face_crop = "face_1_" + filename
full = os.path.join("static", "wikidata_photo", "face_cropped", face_crop)
if os.path.exists(full):
return os.path.join("wikidata_photo", "face_cropped", face_crop)
return os.path.join("wikidata_photo", "thumb", person.wikidata_photo[0])
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5002)