From 7a414a5d8814c1256d9fa4f338f52b6dec187c40 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Mon, 17 Feb 2025 11:43:23 +0000 Subject: [PATCH] mirrors: Group mirrors by country Signed-off-by: Michael Tremer --- src/templates/mirrors/index.html | 12 +++++++++--- src/web/base.py | 33 ++++++++++++++++---------------- src/web/filters.py | 17 ++++++++++++++++ 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/templates/mirrors/index.html b/src/templates/mirrors/index.html index f6ac4362..2d423885 100644 --- a/src/templates/mirrors/index.html +++ b/src/templates/mirrors/index.html @@ -25,9 +25,15 @@
-
- {{ MirrorList(mirrors) }} -
+ {% for country_code, mirrors in mirrors | groupby("country_code") %} +
+
+ {{ country_code | format_country_code }} +
+ + {{ MirrorList(mirrors) }} +
+ {% endfor %} {% if current_user and current_user.is_admin() %}
diff --git a/src/web/base.py b/src/web/base.py index 9540e135..c9d80399 100644 --- a/src/web/base.py +++ b/src/web/base.py @@ -305,24 +305,25 @@ class BaseHandler(tornado.web.RequestHandler): # Custom Filters env.filters |= { - "avatar_url" : filters.avatar_url, - "email_address" : filters.email_address, - "email_name" : filters.email_name, - "file_mode" : filters.file_mode, - "format_asn" : filters.format_asn, - "format_date" : filters.format_date, - "format_day" : filters.format_day, - "format_time" : filters.format_time, - "highlight" : filters.highlight, - "hostname" : filters.hostname, - "markdown" : filters._markdown, - "static_url" : filters.static_url, - "summary" : filters.summary, + "avatar_url" : filters.avatar_url, + "email_address" : filters.email_address, + "email_name" : filters.email_name, + "file_mode" : filters.file_mode, + "format_asn" : filters.format_asn, + "format_country_code" : filters.format_country_code, + "format_date" : filters.format_date, + "format_day" : filters.format_day, + "format_time" : filters.format_time, + "highlight" : filters.highlight, + "hostname" : filters.hostname, + "markdown" : filters._markdown, + "static_url" : filters.static_url, + "summary" : filters.summary, # Add some Python built-ins - "dir" : dir, - "enumerate" : enumerate, - "range" : range, + "dir" : dir, + "enumerate" : enumerate, + "range" : range, } return JinjaTemplateLoader(env) diff --git a/src/web/filters.py b/src/web/filters.py index ce349d85..4e81bdd5 100644 --- a/src/web/filters.py +++ b/src/web/filters.py @@ -18,6 +18,7 @@ # # ############################################################################### +import babel import babel.dates import datetime import email.utils @@ -65,6 +66,22 @@ def format_asn(ctx, asn): return "%s" % (o or asn) +@jinja2.pass_context +def format_country_code(ctx, country_code): + # Fetch locale + locale = ctx.get("locale") + + # Fetch the translation function + _ = locale.translate + + # Fetch the Babel locale + locale = babel.Locale(locale.code) + + try: + return locale.territories[country_code] + except KeyError: + return _("- Unknown Country -") + @jinja2.pass_context def format_date(ctx, *args, **kwargs): # Fetch locale -- 2.47.3