From 7a9e9176cd989e2fa63b8e03f5258f50e6b58884 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 13 Feb 2020 12:27:16 +0000 Subject: [PATCH] mirrors: Divide list by country Signed-off-by: Michael Tremer --- src/backend/mirrors.py | 16 +++++++++++ src/templates/mirrors/index.html | 49 +++++++++++++++++++++++--------- src/web/mirrors.py | 6 +++- 3 files changed, 56 insertions(+), 15 deletions(-) diff --git a/src/backend/mirrors.py b/src/backend/mirrors.py index a49ff858..5c30a0b0 100644 --- a/src/backend/mirrors.py +++ b/src/backend/mirrors.py @@ -2,6 +2,7 @@ import datetime import logging +import iso3166 import math import os.path import random @@ -68,6 +69,17 @@ class Mirrors(Object): return self._get_mirror("SELECT * FROM mirrors \ WHERE hostname = %s", hostname) + def get_by_countries(self): + mirrors = {} + + for m in self: + try: + mirrors[m.country].append(m) + except KeyError: + mirrors[m.country] = [m] + + return mirrors + class Mirror(Object): def init(self, id, data=None): @@ -140,6 +152,10 @@ class Mirror(Object): if self.location: return self.location.longitude + @lazy_property + def country(self): + return iso3166.countries.get(self.country_code) + @property def country_code(self): return self.data.country_code diff --git a/src/templates/mirrors/index.html b/src/templates/mirrors/index.html index 5ff7169d..ad358c70 100644 --- a/src/templates/mirrors/index.html +++ b/src/templates/mirrors/index.html @@ -3,22 +3,43 @@ {% block title %}{{ _("Mirrors") }}{% end block %} {% block content %} + {% set total = sum((len(m) for c, m in mirrors.items())) %} + +

+ {{ _("We are currently running %s mirror servers") % total }} +

+ + {% set countries = sorted(mirrors, key=lambda c: c.name) %} +
- + {% for country in countries %} + +
+
+

{{ country.name }}

+ + {{ _("One Mirror", "%(num)s Mirrors", len(mirrors[country])) % { "num" : len(mirrors[country]) } }} + +
+ +

+
+ +
+ {% for m in mirrors[country] %} + +
{{ m.hostname }}
+ + {% if m.owner %} +

{{ m.owner }}

+ {% end %} +
+ {% end %} +
+ {% end %}
{% end block %} diff --git a/src/web/mirrors.py b/src/web/mirrors.py index 9a83ef4a..6c7c273e 100644 --- a/src/web/mirrors.py +++ b/src/web/mirrors.py @@ -6,7 +6,11 @@ from . import base class IndexHandler(base.BaseHandler): def get(self): - self.render("mirrors/index.html", mirrors=self.backend.mirrors) + mirrors = self.backend.mirrors.get_by_countries() + if not mirrors: + raise tornado.web.HTTPError(404) + + self.render("mirrors/index.html", mirrors=mirrors) class MirrorHandler(base.BaseHandler): -- 2.39.5