Add conference country filter
This commit is contained in:
parent
442ae98463
commit
38dbb2b4e7
3 changed files with 178 additions and 1 deletions
75
web_view.py
75
web_view.py
|
|
@ -400,6 +400,64 @@ def build_conference_list() -> list[StrDict]:
|
|||
return items
|
||||
|
||||
|
||||
def conference_country_code(conf: StrDict) -> str | None:
|
||||
"""Return normalized alpha-2 country code for a conference."""
|
||||
country = conf.get("country")
|
||||
if not isinstance(country, str):
|
||||
return None
|
||||
|
||||
code = country.strip().lower()
|
||||
return code if len(code) == 2 else None
|
||||
|
||||
|
||||
def normalize_country_filter(value: str | None) -> str | None:
|
||||
"""Normalize and validate a country query parameter."""
|
||||
if not value:
|
||||
return None
|
||||
|
||||
code = value.strip().lower()
|
||||
if len(code) != 2:
|
||||
return None
|
||||
|
||||
return code if agenda.get_country(code) else None
|
||||
|
||||
|
||||
def filter_conferences_by_country(
|
||||
items: list[StrDict], country_code: str | None
|
||||
) -> list[StrDict]:
|
||||
"""Filter conferences by country when a country code is provided."""
|
||||
if not country_code:
|
||||
return items
|
||||
|
||||
return [item for item in items if conference_country_code(item) == country_code]
|
||||
|
||||
|
||||
def conference_country_options(items: list[StrDict]) -> list[StrDict]:
|
||||
"""Return country options for the conference country filter."""
|
||||
counts: defaultdict[str, int] = defaultdict(int)
|
||||
for item in items:
|
||||
code = conference_country_code(item)
|
||||
if code and agenda.get_country(code):
|
||||
counts[code] += 1
|
||||
|
||||
options: list[StrDict] = []
|
||||
for code, count in counts.items():
|
||||
country = agenda.get_country(code)
|
||||
if not country:
|
||||
continue
|
||||
options.append(
|
||||
{
|
||||
"code": code,
|
||||
"name": country.name,
|
||||
"flag": country.flag,
|
||||
"count": count,
|
||||
}
|
||||
)
|
||||
|
||||
options.sort(key=lambda item: str(item["name"]))
|
||||
return options
|
||||
|
||||
|
||||
def build_conference_series_list() -> list[StrDict]:
|
||||
"""Build conference series list with conference counts."""
|
||||
data_dir = app.config["PERSONAL_DATA"]
|
||||
|
|
@ -591,6 +649,11 @@ def conference_list() -> str:
|
|||
"""Page showing a list of conferences."""
|
||||
today = date.today()
|
||||
items = build_conference_list()
|
||||
country_filter = normalize_country_filter(flask.request.args.get("country"))
|
||||
country_options = conference_country_options(
|
||||
[conf for conf in items if conf["latest_date"] >= today]
|
||||
)
|
||||
items = filter_conferences_by_country(items, country_filter)
|
||||
|
||||
current = [
|
||||
conf
|
||||
|
|
@ -612,6 +675,8 @@ def conference_list() -> str:
|
|||
timeline=timeline,
|
||||
today=today,
|
||||
get_country=agenda.get_country,
|
||||
selected_country=country_filter,
|
||||
country_options=country_options,
|
||||
fx_rate=agenda.fx.get_rates(app.config),
|
||||
)
|
||||
|
||||
|
|
@ -620,11 +685,19 @@ def conference_list() -> str:
|
|||
def past_conference_list() -> str:
|
||||
"""Page showing a list of conferences."""
|
||||
today = date.today()
|
||||
items = build_conference_list()
|
||||
country_filter = normalize_country_filter(flask.request.args.get("country"))
|
||||
country_options = conference_country_options(
|
||||
[conf for conf in items if conf["latest_date"] < today]
|
||||
)
|
||||
items = filter_conferences_by_country(items, country_filter)
|
||||
return flask.render_template(
|
||||
"conference_list.html",
|
||||
past=[conf for conf in build_conference_list() if conf["latest_date"] < today],
|
||||
past=[conf for conf in items if conf["latest_date"] < today],
|
||||
today=today,
|
||||
get_country=agenda.get_country,
|
||||
selected_country=country_filter,
|
||||
country_options=country_options,
|
||||
fx_rate=agenda.fx.get_rates(app.config),
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue