From 6f0ae6746baea657ee6d7c21ac0813a04f825443 Mon Sep 17 00:00:00 2001
From: Dan McGee <dan@archlinux.org>
Date: Mon, 14 Jan 2013 01:00:11 -0600
Subject: Drop country column from mirror table

We now always look for this information at the URL level, not the mirror
level. This simplifies quite a bit of code in and around the mirror
views.

Signed-off-by: Dan McGee <dan@archlinux.org>
---
 mirrors/admin.py                                   |  4 +-
 .../0020_auto__del_field_mirror_country.py         | 70 ++++++++++++++++++++++
 mirrors/models.py                                  |  7 +--
 mirrors/utils.py                                   |  7 +--
 mirrors/views.py                                   | 15 ++---
 public/views.py                                    |  2 +-
 templates/mirrors/mirror_details.html              | 12 ++--
 templates/mirrors/mirrorlist.txt                   |  2 +-
 templates/mirrors/mirrorlist_status.txt            |  2 +-
 templates/mirrors/mirrors.html                     |  4 +-
 templates/mirrors/status_table.html                |  2 +-
 templates/public/download.html                     |  2 +-
 12 files changed, 93 insertions(+), 36 deletions(-)
 create mode 100644 mirrors/migrations/0020_auto__del_field_mirror_country.py

diff --git a/mirrors/admin.py b/mirrors/admin.py
index 65fff368..eaa38391 100644
--- a/mirrors/admin.py
+++ b/mirrors/admin.py
@@ -62,9 +62,9 @@ class MirrorAdminForm(forms.ModelForm):
 
 class MirrorAdmin(admin.ModelAdmin):
     form = MirrorAdminForm
-    list_display = ('name', 'tier', 'country', 'active', 'public',
+    list_display = ('name', 'tier', 'active', 'public',
             'isos', 'admin_email', 'alternate_email')
-    list_filter = ('tier', 'active', 'public', 'country')
+    list_filter = ('tier', 'active', 'public')
     search_fields = ('name', 'admin_email', 'alternate_email')
     inlines = [
             MirrorUrlInlineAdmin,
diff --git a/mirrors/migrations/0020_auto__del_field_mirror_country.py b/mirrors/migrations/0020_auto__del_field_mirror_country.py
new file mode 100644
index 00000000..c2220a50
--- /dev/null
+++ b/mirrors/migrations/0020_auto__del_field_mirror_country.py
@@ -0,0 +1,70 @@
+# -*- coding: utf-8 -*-
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+
+class Migration(SchemaMigration):
+
+    def forwards(self, orm):
+        db.delete_column('mirrors_mirror', 'country')
+
+
+    def backwards(self, orm):
+        db.add_column('mirrors_mirror', 'country',
+                      self.gf('django_countries.fields.CountryField')(blank=True, default='', max_length=2, db_index=True),
+                      keep_default=False)
+
+
+    models = {
+        'mirrors.mirror': {
+            'Meta': {'ordering': "('name',)", 'object_name': 'Mirror'},
+            'active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'admin_email': ('django.db.models.fields.EmailField', [], {'max_length': '255', 'blank': 'True'}),
+            'alternate_email': ('django.db.models.fields.EmailField', [], {'max_length': '255', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'isos': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'}),
+            'notes': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+            'public': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'rsync_password': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '50', 'blank': 'True'}),
+            'rsync_user': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '50', 'blank': 'True'}),
+            'tier': ('django.db.models.fields.SmallIntegerField', [], {'default': '2'}),
+            'upstream': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['mirrors.Mirror']", 'null': 'True', 'on_delete': 'models.SET_NULL'})
+        },
+        'mirrors.mirrorlog': {
+            'Meta': {'object_name': 'MirrorLog'},
+            'check_time': ('django.db.models.fields.DateTimeField', [], {'db_index': 'True'}),
+            'duration': ('django.db.models.fields.FloatField', [], {'null': 'True'}),
+            'error': ('django.db.models.fields.TextField', [], {'default': "''", 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'is_success': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'last_sync': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
+            'url': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'logs'", 'to': "orm['mirrors.MirrorUrl']"})
+        },
+        'mirrors.mirrorprotocol': {
+            'Meta': {'ordering': "('protocol',)", 'object_name': 'MirrorProtocol'},
+            'default': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'is_download': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'protocol': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '10'})
+        },
+        'mirrors.mirrorrsync': {
+            'Meta': {'object_name': 'MirrorRsync'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'ip': ('django.db.models.fields.CharField', [], {'max_length': '24'}),
+            'mirror': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'rsync_ips'", 'to': "orm['mirrors.Mirror']"})
+        },
+        'mirrors.mirrorurl': {
+            'Meta': {'object_name': 'MirrorUrl'},
+            'country': ('django_countries.fields.CountryField', [], {'db_index': 'True', 'max_length': '2', 'blank': 'True'}),
+            'has_ipv4': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'has_ipv6': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'mirror': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'urls'", 'to': "orm['mirrors.Mirror']"}),
+            'protocol': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'urls'", 'on_delete': 'models.PROTECT', 'to': "orm['mirrors.MirrorProtocol']"}),
+            'url': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'})
+        }
+    }
+
+    complete_apps = ['mirrors']
diff --git a/mirrors/models.py b/mirrors/models.py
index 0179d5bf..ca421d13 100644
--- a/mirrors/models.py
+++ b/mirrors/models.py
@@ -17,7 +17,6 @@ class Mirror(models.Model):
     name = models.CharField(max_length=255, unique=True)
     tier = models.SmallIntegerField(default=2, choices=TIER_CHOICES)
     upstream = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
-    country = CountryField(blank=True, db_index=True)
     admin_email = models.EmailField(max_length=255, blank=True)
     alternate_email = models.EmailField(max_length=255, blank=True)
     public = models.BooleanField(default=True)
@@ -28,7 +27,7 @@ class Mirror(models.Model):
     notes = models.TextField(blank=True)
 
     class Meta:
-        ordering = ('country', 'name')
+        ordering = ('name',)
 
     def __unicode__(self):
         return self.name
@@ -75,10 +74,6 @@ class MirrorUrl(models.Model):
     def hostname(self):
         return urlparse(self.url).hostname
 
-    @property
-    def real_country(self):
-        return self.country or self.mirror.country
-
     def clean(self):
         try:
             # Auto-map the protocol field by looking at the URL
diff --git a/mirrors/utils.py b/mirrors/utils.py
index 1d560021..3ab176b3 100644
--- a/mirrors/utils.py
+++ b/mirrors/utils.py
@@ -115,7 +115,7 @@ def get_mirror_errors(cutoff=DEFAULT_CUTOFF, mirror_ids=None):
             is_success=False, check_time__gte=cutoff_time,
             url__mirror__active=True, url__mirror__public=True).values(
             'url__url', 'url__country', 'url__protocol__protocol',
-            'url__mirror__country', 'url__mirror__tier', 'error').annotate(
+            'url__mirror__tier', 'error').annotate(
             error_count=Count('error'), last_occurred=Max('check_time')
             ).order_by('-last_occurred', '-error_count')
 
@@ -124,8 +124,7 @@ def get_mirror_errors(cutoff=DEFAULT_CUTOFF, mirror_ids=None):
 
     errors = list(errors)
     for err in errors:
-        ctry_code = err['url__country'] or err['url__mirror__country']
-        err['country'] = Country(ctry_code)
+        err['country'] = Country(err['url__country'])
     return errors
 
 
@@ -152,7 +151,7 @@ def get_mirror_url_for_download(cutoff=DEFAULT_CUTOFF):
     mirror_urls = MirrorUrl.objects.filter(
             mirror__public=True, mirror__active=True, protocol__default=True)
     # look first for a country-agnostic URL, then fall back to any HTTP URL
-    filtered_urls = mirror_urls.filter(mirror__country='')[:1]
+    filtered_urls = mirror_urls.filter(country='')[:1]
     if not filtered_urls:
         filtered_urls = mirror_urls[:1]
     if not filtered_urls:
diff --git a/mirrors/views.py b/mirrors/views.py
index d3867802..545e3557 100644
--- a/mirrors/views.py
+++ b/mirrors/views.py
@@ -42,9 +42,6 @@ class MirrorlistForm(forms.Form):
 
     def get_countries(self):
         country_codes = set()
-        country_codes.update(Mirror.objects.filter(active=True).exclude(
-                country='').values_list(
-                'country', flat=True).order_by().distinct())
         country_codes.update(MirrorUrl.objects.filter(
                 mirror__active=True).exclude(country='').values_list(
                 'country', flat=True).order_by().distinct())
@@ -81,7 +78,7 @@ def generate_mirrorlist(request):
 
 
 def default_protocol_filter(original_urls):
-    key_func = attrgetter('real_country')
+    key_func = attrgetter('country')
     sorted_urls = sorted(original_urls, key=key_func)
     urls = []
     for _, group in groupby(sorted_urls, key=key_func):
@@ -119,8 +116,7 @@ def find_mirrors(request, countries=None, protocols=None, use_status=False,
             protocol__in=protocols,
             mirror__public=True, mirror__active=True)
     if countries and 'all' not in countries:
-        qset = qset.filter(Q(country__in=countries) |
-                Q(mirror__country__in=countries))
+        qset = qset.filter(country__in=countries)
 
     ip_version = Q()
     if ipv4_supported:
@@ -135,7 +131,7 @@ def find_mirrors(request, countries=None, protocols=None, use_status=False,
         urls = qset
 
     if not use_status:
-        sort_key = attrgetter('real_country.name', 'mirror.name', 'url')
+        sort_key = attrgetter('country.name', 'mirror.name', 'url')
         urls = sorted(urls, key=sort_key)
         template = 'mirrors/mirrorlist.txt'
     else:
@@ -158,7 +154,7 @@ def find_mirrors_simple(request, protocol):
 
 
 def mirrors(request):
-    mirror_list = Mirror.objects.select_related().order_by('tier', 'country')
+    mirror_list = Mirror.objects.select_related().order_by('tier', 'name')
     protos = MirrorUrl.objects.values_list(
             'mirror_id', 'protocol__protocol').order_by(
             'mirror__id', 'protocol__protocol').distinct()
@@ -254,8 +250,7 @@ class MirrorStatusJSONEncoder(DjangoJSONEncoder):
             return list(obj)
         if isinstance(obj, MirrorUrl):
             data = {attr: getattr(obj, attr) for attr in self.url_attributes}
-            # get any override on the country attribute first
-            country = obj.real_country
+            country = obj.country
             data['country'] = unicode(country.name)
             data['country_code'] = country.code
             return data
diff --git a/public/views.py b/public/views.py
index 65b0c31f..e15f5b89 100644
--- a/public/views.py
+++ b/public/views.py
@@ -85,7 +85,7 @@ def download(request):
     mirror_urls = MirrorUrl.objects.select_related('mirror').filter(
             protocol__default=True,
             mirror__public=True, mirror__active=True, mirror__isos=True)
-    sort_by = attrgetter('real_country.name', 'mirror.name')
+    sort_by = attrgetter('country.name', 'mirror.name')
     mirror_urls = sorted(mirror_urls, key=sort_by)
     context = {
         'release': release,
diff --git a/templates/mirrors/mirror_details.html b/templates/mirrors/mirror_details.html
index 132557cd..a56123ff 100644
--- a/templates/mirrors/mirror_details.html
+++ b/templates/mirrors/mirror_details.html
@@ -18,10 +18,6 @@
             <th>Tier:</th>
             <td>{{ mirror.get_tier_display }}</td>
         </tr>
-        <tr>
-            <th>Country:</th>
-            <td>{% if mirror.country %}<img src="{{ mirror.country.flag }}" alt=""/> {% endif %}{{ mirror.country.name|default:'Worldwide' }}</td>
-        </tr>
         <tr>
             <th>Has ISOs:</th>
             <td>{{ mirror.isos|yesno|capfirst }}</td>
@@ -77,6 +73,8 @@
         <thead>
             <tr>
                 <th>Mirror URL</th>
+                <th>Protocol</th>
+                <th>Country</th>
                 <th>IPv4</th>
                 <th>IPv6</th>
                 <th>Last Sync</th>
@@ -91,6 +89,8 @@
             {% for m_url in urls %}
             <tr class="{% cycle 'odd' 'even' %}">
                 <td>{% if m_url.protocol.is_download %}<a href="{{ m_url.url }}">{{ m_url.url }}</a>{% else %}{{ m_url.url }}{% endif %}</td>
+                <td>{{ m_url.protocol }}</td>
+                <td class="country">{% if m_url.country %}<img src="{{ m_url.country.flag }}" alt=""/> {% endif %}{{ m_url.country.name }}</td>
                 <td>{{ m_url.has_ipv4|yesno|capfirst }}</td>
                 <td>{{ m_url.has_ipv6|yesno|capfirst }}</td>
                 <td>{{ m_url.last_sync|date:'Y-m-d H:i'|default:'unknown' }}</td>
@@ -115,8 +115,8 @@
 <script type="text/javascript">
 $(document).ready(function() {
     $("#available_urls:has(tbody tr)").tablesorter(
-        {widgets: ['zebra'], sortList: [[0,0]],
-        headers: { 6: { sorter: 'mostlydigit' }, 7: { sorter: 'mostlydigit' }, 8: { sorter: 'mostlydigit' } } });
+        {widgets: ['zebra'], sortList: [[1,0], [2,0]],
+        headers: { 8: { sorter: 'mostlydigit' }, 9: { sorter: 'mostlydigit' }, 10: { sorter: 'mostlydigit' } } });
 });
 $(document).ready(function() {
     mirror_status("#visualize-mirror", "./json/");
diff --git a/templates/mirrors/mirrorlist.txt b/templates/mirrors/mirrorlist.txt
index d3dd6e4e..a7f8e2af 100644
--- a/templates/mirrors/mirrorlist.txt
+++ b/templates/mirrors/mirrorlist.txt
@@ -8,6 +8,6 @@ content right, and then go back later to fix it all up.
 ## Generated on {% now "Y-m-d" %}
 ##{% for mirror_url in mirror_urls %}{% ifchanged %}
 
-## {{ mirror_url.real_country.name|default:'Worldwide' }}{% endifchanged %}
+## {{ mirror_url.country.name|default:'Worldwide' }}{% endifchanged %}
 #Server = {{ mirror_url.url}}$repo/os/$arch{% endfor %}
 {% endautoescape %}
diff --git a/templates/mirrors/mirrorlist_status.txt b/templates/mirrors/mirrorlist_status.txt
index 523794b2..575d19f7 100644
--- a/templates/mirrors/mirrorlist_status.txt
+++ b/templates/mirrors/mirrorlist_status.txt
@@ -9,6 +9,6 @@ content right, and then go back later to fix it all up.
 ## Generated on {% now "Y-m-d" %}
 ##
 {% for mirror_url in mirror_urls %}
-## Score: {{ mirror_url.score|floatformat:1|default:'unknown' }}, {{ mirror_url.real_country.name|default:'Worldwide' }}
+## Score: {{ mirror_url.score|floatformat:1|default:'unknown' }}, {{ mirror_url.country.name|default:'Worldwide' }}
 #Server = {{ mirror_url.url}}$repo/os/$arch{% endfor %}
 {% endautoescape %}
diff --git a/templates/mirrors/mirrors.html b/templates/mirrors/mirrors.html
index c83d0d43..458b693f 100644
--- a/templates/mirrors/mirrors.html
+++ b/templates/mirrors/mirrors.html
@@ -10,7 +10,6 @@
             <tr>
                 <th>Server</th>
                 <th>Tier</th>
-                <th>Country</th>
                 <th>ISOs</th>
                 <th>Protocols</th>
                 {% if user.is_authenticated %}
@@ -27,7 +26,6 @@
                 <td><a href="{{ mirror.get_absolute_url }}"
                         title="Mirror details for {{ mirror.name }}">{{ mirror.name }}</a></td>
                 <td>{{ mirror.get_tier_display }}</td>
-                <td>{% if mirror.country %}<img src="{{ mirror.country.flag }}" alt=""/> {% endif %}{{ mirror.country.name }}</td>
                 <td>{{ mirror.isos|yesno|capfirst }}</td>
                 <td class="wrap">{{ mirror.protocols|join:", " }}</td>
                 {% if user.is_authenticated %}
@@ -45,7 +43,7 @@
 <script type="text/javascript" src="{% static "archweb.js" %}"></script>
 <script type="text/javascript">
 $(document).ready(function() {
-    $(".results").tablesorter({widgets: ['zebra'], sortList: [[1,0], [2,0]]});
+    $(".results").tablesorter({widgets: ['zebra'], sortList: [[1,0], [0,0]]});
 });
 </script>
 {% endblock %}
diff --git a/templates/mirrors/status_table.html b/templates/mirrors/status_table.html
index 1961d222..c7394de6 100644
--- a/templates/mirrors/status_table.html
+++ b/templates/mirrors/status_table.html
@@ -17,7 +17,7 @@
         {% spaceless %}<tr class="{% cycle 'odd' 'even' %}">
             <td>{{ m_url.url }}</td>
             <td>{{ m_url.protocol }}</td>
-            <td class="country">{% if m_url.real_country %}<img src="{{ m_url.real_country.flag }}" alt=""/> {% endif %}{{ m_url.real_country.name }}</td>
+            <td class="country">{% if m_url.country %}<img src="{{ m_url.country.flag }}" alt=""/> {% endif %}{{ m_url.country.name }}</td>
             <td>{{ m_url.completion_pct|percentage:1 }}</td>
             <td>{{ m_url.delay|duration|default:'unknown' }}</td>
             <td>{{ m_url.duration_avg|floatformat:2 }}</td>
diff --git a/templates/public/download.html b/templates/public/download.html
index 3005ffb3..0c96fcef 100644
--- a/templates/public/download.html
+++ b/templates/public/download.html
@@ -81,7 +81,7 @@
 
     {% cache 600 download-mirrors %}
     <div id="download-mirrors">
-        {% regroup mirror_urls by real_country as grouped_urls %}
+        {% regroup mirror_urls by country as grouped_urls %}
         {% for country in grouped_urls %}
         {% if country.grouper %}<h5><img src="{{ country.grouper.flag }}" alt=""/> {{ country.grouper.name }}</h5>
         {% else %}<h5>Worldwide</h5>{% endif %}
-- 
cgit v1.2.3-2-g168b