]> git.ipfire.org Git - ipfire.org.git/commitdiff
mirrors: Divide list by country
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 13 Feb 2020 12:27:16 +0000 (12:27 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 13 Feb 2020 12:27:16 +0000 (12:27 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/mirrors.py
src/templates/mirrors/index.html
src/web/mirrors.py

index a49ff8588aa27aab63fd850bcc7fee73004e5b86..5c30a0b0ff20c955cad9257185709348f6d1aeab 100644 (file)
@@ -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
index 5ff7169dadcd52e93439184c4ef6a55e4cfc3222..ad358c70eefa3d98d1f025aa1d22ea66f8e04f4f 100644 (file)
@@ -3,22 +3,43 @@
 {% block title %}{{ _("Mirrors") }}{% end block %}
 
 {% block content %}
+       {% set total = sum((len(m) for c, m in mirrors.items())) %}
+
+       <h4 class="my-5 text-muted text-center">
+               {{ _("We are currently running %s mirror servers") % total }}
+       </h4>
+
+       {% set countries = sorted(mirrors, key=lambda c: c.name) %}
+
        <div class="row justify-content-center">
                <div class="col-12 col-md-6">
-                       <div class="list-group">
-                               {% for m in mirrors %}
-                                       <a href="/mirrors/{{ m.hostname }}" class="list-group-item list-group-item-action
-                                                       list-group-item-{% if m.state == "UP" %}success{% elif m.state == "DOWN" %}danger{% else %}warning{% end %}
-                                                       flex-column align-items-start">
-                                               <h5 class="mb-1">{{ m.hostname }}</h5>
-
-                                               <p class="mb-0 text-truncate text-muted">
-                                                       <span class="flag-icon flag-icon-{{ m.country_code.lower() }} small mr-1"></span> {{ m.country_name }}
-                                                       {% if m.owner %}&dash; {{ m.owner }}{% end %}                                                   
-                                               </p>
-                                       </a>
-                               {% end %}
-                       </div>
+                       {% for country in countries %}
+                               <a name="{{ country.alpha2 }}"></a>
+                               <div class="my-4 d-flex justify-content-between ">
+                                       <div>
+                                               <h4 class="mb-0">{{ country.name }}</h4>
+                                               <span class="small text-muted">
+                                                       {{ _("One Mirror", "%(num)s Mirrors", len(mirrors[country])) % { "num" : len(mirrors[country]) } }}
+                                               </span>
+                                       </div>
+
+                                       <h4 class="flag-icon flag-icon-{{ country.alpha2.lower() }}"></h4>
+                               </div>
+
+                               <div class="list-group">
+                                       {% for m in mirrors[country] %}
+                                               <a href="/mirrors/{{ m.hostname }}" class="list-group-item list-group-item-action
+                                                               list-group-item-{% if m.state == "UP" %}success{% elif m.state == "DOWN" %}danger{% else %}warning{% end %}
+                                                               flex-column align-items-start">
+                                                       <h5 class="mb-1">{{ m.hostname }}</h5>
+
+                                                       {% if m.owner %}
+                                                               <p class="mb-0 text-truncate text-muted">{{ m.owner }}</p>
+                                                       {% end %}
+                                               </a>
+                                       {% end %}
+                               </div>
+                       {% end %}
                </div>
        </div>
 {% end block %}
index 9a83ef4a2a49f0178f11ea2d8b5e28999c39fb1c..6c7c273e0b33cf7b1958ab266a477d37eaa4cfab 100644 (file)
@@ -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):