From 20675141c340ea3d2d6d8305f8ba0950d3bf974c Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Wed, 25 Apr 2012 00:09:46 -0500 Subject: Add django_countries country code fields and population migrations This adds these columns and attempts to populate them with data from our existing country column data. Signed-off-by: Dan McGee --- mirrors/admin.py | 4 +- mirrors/migrations/0014_add_country_code_fields.py | 74 +++++++++++++++++ mirrors/migrations/0015_assign_country_codes.py | 93 ++++++++++++++++++++++ mirrors/models.py | 5 +- requirements.txt | 1 + requirements_prod.txt | 1 + settings.py | 1 + 7 files changed, 176 insertions(+), 3 deletions(-) create mode 100644 mirrors/migrations/0014_add_country_code_fields.py create mode 100644 mirrors/migrations/0015_assign_country_codes.py diff --git a/mirrors/admin.py b/mirrors/admin.py index e8c7e280..b7b9894c 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_old', 'active', 'public', + list_display = ('name', 'tier', 'country', 'active', 'public', 'isos', 'admin_email') - list_filter = ('tier', 'active', 'public', 'country_old') + list_filter = ('tier', 'active', 'public', 'country') search_fields = ('name',) inlines = [ MirrorUrlInlineAdmin, diff --git a/mirrors/migrations/0014_add_country_code_fields.py b/mirrors/migrations/0014_add_country_code_fields.py new file mode 100644 index 00000000..6248fc7e --- /dev/null +++ b/mirrors/migrations/0014_add_country_code_fields.py @@ -0,0 +1,74 @@ +# -*- 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.add_column('mirrors_mirror', 'country', + self.gf('django_countries.fields.CountryField')(default='', max_length=2, blank=True), + keep_default=False) + db.add_column('mirrors_mirrorurl', 'country', + self.gf('django_countries.fields.CountryField')(default='', max_length=2, blank=True), + keep_default=False) + + def backwards(self, orm): + db.delete_column('mirrors_mirror', 'country') + db.delete_column('mirrors_mirrorurl', 'country') + + models = { + 'mirrors.mirror': { + 'Meta': {'ordering': "('country', 'name')", 'object_name': 'Mirror'}, + 'active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'admin_email': ('django.db.models.fields.EmailField', [], {'max_length': '255', 'blank': 'True'}), + 'country': ('django_countries.fields.CountryField', [], {'max_length': '2', 'blank': 'True'}), + 'country_old': ('django.db.models.fields.CharField', [], {'max_length': '255', 'db_index': '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.CharField', [], {'default': "''", 'max_length': '255', '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', [], {'max_length': '2', 'blank': 'True'}), + 'country_old': ('mirrors.models.NullCharField', [], {'db_index': 'True', 'max_length': '255', 'null': 'True', '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/migrations/0015_assign_country_codes.py b/mirrors/migrations/0015_assign_country_codes.py new file mode 100644 index 00000000..1d16ada3 --- /dev/null +++ b/mirrors/migrations/0015_assign_country_codes.py @@ -0,0 +1,93 @@ +# -*- coding: utf-8 -*- +import datetime +from south.db import db +from south.v2 import DataMigration +from django.db import models + +from django_countries.countries import OFFICIAL_COUNTRIES + +class Migration(DataMigration): + + def forwards(self, orm): + reverse_map = dict((v, k) for k, v in OFFICIAL_COUNTRIES.items()) + # add a few special cases to the list that we know might exist + reverse_map['GREAT BRITAIN'] = 'GB' + reverse_map['KOREA'] = 'KR' + reverse_map['MACEDONIA'] = 'MK' + reverse_map['RUSSIA'] = 'RU' + reverse_map['SOUTH KOREA'] = 'KR' + reverse_map['TAIWAN'] = 'TW' + reverse_map['VIETNAM'] = 'VN' + + for country_name in orm.Mirror.objects.values_list( + 'country_old', flat=True).order_by().distinct(): + code = reverse_map.get(country_name.upper(), '') + orm.Mirror.objects.filter( + country_old=country_name).update(country=code) + + for country_name in orm.MirrorUrl.objects.filter( + country_old__isnull=False).values_list( + 'country_old', flat=True).order_by().distinct(): + code = reverse_map.get(country_name.upper(), '') + orm.MirrorUrl.objects.filter( + country_old=country_name).update(country=code) + + def backwards(self, orm): + orm.MirrorUrl.objects.all().update(country='') + orm.Mirror.objects.all().update(country='') + + models = { + 'mirrors.mirror': { + 'Meta': {'ordering': "('country', 'name')", 'object_name': 'Mirror'}, + 'active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'admin_email': ('django.db.models.fields.EmailField', [], {'max_length': '255', 'blank': 'True'}), + 'country': ('django_countries.fields.CountryField', [], {'max_length': '2', 'blank': 'True'}), + 'country_old': ('django.db.models.fields.CharField', [], {'max_length': '255', 'db_index': '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.CharField', [], {'default': "''", 'max_length': '255', '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', [], {'max_length': '2', 'blank': 'True'}), + 'country_old': ('mirrors.models.NullCharField', [], {'db_index': 'True', 'max_length': '255', 'null': 'True', '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'] + symmetrical = True diff --git a/mirrors/models.py b/mirrors/models.py index 46753fac..79968412 100644 --- a/mirrors/models.py +++ b/mirrors/models.py @@ -3,6 +3,7 @@ from urlparse import urlparse from django.db import models from django.core.exceptions import ValidationError +from django_countries import CountryField class NullCharField(models.CharField): description = "String (up to %(max_length)s), NULL if value is empty" @@ -25,6 +26,7 @@ class Mirror(models.Model): tier = models.SmallIntegerField(default=2, choices=TIER_CHOICES) upstream = models.ForeignKey('self', null=True, on_delete=models.SET_NULL) country_old = models.CharField(max_length=255, db_index=True) + country = CountryField(blank=True) admin_email = models.EmailField(max_length=255, blank=True) public = models.BooleanField(default=True) active = models.BooleanField(default=True) @@ -34,7 +36,7 @@ class Mirror(models.Model): notes = models.TextField(blank=True) class Meta: - ordering = ('country_old', 'name') + ordering = ('country', 'name') def __unicode__(self): return self.name @@ -70,6 +72,7 @@ class MirrorUrl(models.Model): mirror = models.ForeignKey(Mirror, related_name="urls") country_old = NullCharField(max_length=255, null=True, blank=True, db_index=True) + country = CountryField(blank=True) has_ipv4 = models.BooleanField("IPv4 capable", default=True, editable=False) has_ipv6 = models.BooleanField("IPv6 capable", default=False, diff --git a/requirements.txt b/requirements.txt index 1c1578cb..362a3fee 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ Django==1.4 Markdown==2.1.1 South==0.7.4 +django-countries==1.2 pgpdump==1.1 pytz>=2012b diff --git a/requirements_prod.txt b/requirements_prod.txt index e43437de..6ec70dd9 100644 --- a/requirements_prod.txt +++ b/requirements_prod.txt @@ -1,6 +1,7 @@ Django==1.4 Markdown==2.1.1 South==0.7.4 +django-countries==1.2 pgpdump==1.1 psycopg2==2.4.4 pyinotify==0.9.3 diff --git a/settings.py b/settings.py index a4800c92..52339c53 100644 --- a/settings.py +++ b/settings.py @@ -111,6 +111,7 @@ INSTALLED_APPS = ( 'django.contrib.markup', 'django.contrib.staticfiles', 'south', + 'django_countries', 'main', 'mirrors', -- cgit v1.2.3-55-g3dc8