Move some more code.

This commit is contained in:
Edward Betts 2023-09-25 15:20:01 +01:00
parent 7760ed0b58
commit 3493b2b81d
2 changed files with 16 additions and 32 deletions

11
confarchive/utils.py Normal file
View file

@ -0,0 +1,11 @@
"""Utility functions."""
def drop_start(s: str, start: str) -> str:
"""Remove text from the start of a string."""
return s[len(start) :] if s.startswith(start) else s
def plural(num: int, label: str) -> str:
"""Make plural version of label as appropriate."""
return f'{num:,d} {label}{"s" if num != 1 else ""}'

37
main.py
View file

@ -6,7 +6,7 @@ import flask
from sqlalchemy import func, or_, update from sqlalchemy import func, or_, update
from werkzeug.wrappers import Response from werkzeug.wrappers import Response
from confarchive import database, model, wikidata, query from confarchive import database, model, wikidata, query, utils
app = flask.Flask(__name__) app = flask.Flask(__name__)
app.debug = True app.debug = True
@ -15,11 +15,6 @@ app.config.from_object("config.default")
database.init_app(app) database.init_app(app)
def drop_start(s: str, start: str) -> str:
"""Remove text from the start of a string."""
return s[len(start) :] if s.startswith(start) else s
@app.route("/person/<int:person_id>", methods=["GET", "POST"]) @app.route("/person/<int:person_id>", methods=["GET", "POST"])
def person(person_id: int) -> str | Response: def person(person_id: int) -> str | Response:
item = model.Person.query.get(person_id) item = model.Person.query.get(person_id)
@ -33,7 +28,7 @@ def person(person_id: int) -> str | Response:
if "P18" in wd_item["claims"]: if "P18" in wd_item["claims"]:
claim_p18 = wd_item["claims"]["P18"] claim_p18 = wd_item["claims"]["P18"]
wikidata_photo = [ wikidata_photo = [
drop_start(s["mainsnak"]["datavalue"]["value"], "-") utils.drop_start(s["mainsnak"]["datavalue"]["value"], "-")
for s in claim_p18 for s in claim_p18
] ]
for filename in wikidata_photo: for filename in wikidata_photo:
@ -77,7 +72,7 @@ def person(person_id: int) -> str | Response:
"person.html", "person.html",
item=item, item=item,
Event=model.Event, Event=model.Event,
plural=plural, plural=utils.plural,
wikidata_hits=wikidata_hits, wikidata_hits=wikidata_hits,
is_admin=check_admin_mode, is_admin=check_admin_mode,
) )
@ -206,10 +201,6 @@ def index() -> str:
return flask.render_template("index.html", items=q, count=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 ""}'
@app.route("/series") @app.route("/series")
def list_series() -> str: def list_series() -> str:
"""Page showing list of conference series.""" """Page showing list of conference series."""
@ -220,33 +211,15 @@ def list_series() -> str:
@app.route("/speakers") @app.route("/speakers")
def top_speakers_page() -> str: def top_speakers_page() -> str:
top = query.top_speakers().having(func.count() > 4)
"""Top speakers page.""" """Top speakers page."""
photos = [] top = query.top_speakers().having(func.count() > 4)
for person, count in top:
photo = person.photo_filename()
if photo:
photos.append((person, photo))
left_photos = photos[::2]
right_photos = photos[1::2]
photo_person_ids = [person.id for person, photo in photos]
left = photo_person_ids[::2]
right = photo_person_ids[1::2]
return flask.render_template( return flask.render_template(
"top_speakers.html", "top_speakers.html",
top_speakers=top, top_speakers=top,
speaker_counts=query.speaker_counts(), speaker_counts=query.speaker_counts(),
plural=plural, plural=utils.plural,
person_image_filename=person_image_filename, person_image_filename=person_image_filename,
# photo_person_ids=photo_person_ids,
left=left,
right=right,
left_photos=left_photos,
right_photos=right_photos,
) )