Browse Source

Tweaks to Country model

* Added some docstrings because I kept having to look up details
  of the implementation
* Removed index on the alpha3 and numeric code because we never use
  those fields for lookups
* Turned the numeric field into a CharField as suggested
master
Maik Hoepfel 11 years ago
parent
commit
d4243c50de

+ 1
- 0
docs/source/releases/v0.8.rst View File

@@ -454,6 +454,7 @@ Migrations
454 454
 * Address:
455 455
 
456 456
     - ``0011`` - ``AbstractAddress.search_text`` turned into a ``TextField``.
457
+    - ``0012`` - ``AbstractCountry``: Removed two unused indexes & turns numeric code into ``CharField``
457 458
 
458 459
 * Catalogue:
459 460
 

+ 27
- 13
oscar/apps/address/abstract_models.py View File

@@ -376,24 +376,30 @@ class AbstractAddress(models.Model):
376 376
 class AbstractCountry(models.Model):
377 377
     """
378 378
     International Organization for Standardization (ISO) 3166-1 Country list.
379
+
380
+    The field names are a bit awkward, but kept for backwards compatibility.
381
+    pycountry's syntax of alpha2, alpha3, name and official_name seems sane.
379 382
     """
380
-    iso_3166_1_a2 = models.CharField(_('ISO 3166-1 alpha-2'), max_length=2,
381
-                                     primary_key=True)
382
-    iso_3166_1_a3 = models.CharField(_('ISO 3166-1 alpha-3'), max_length=3,
383
-                                     blank=True, db_index=True)
384
-    # This should have been a CharField as it needs to be padded with zeros to
385
-    # be 3 digits.  Access via the numeric_code instead.
386
-    iso_3166_1_numeric = models.PositiveSmallIntegerField(
387
-        _('ISO 3166-1 numeric'), null=True, db_index=True)
388
-    name = models.CharField(_('Official name (CAPS)'), max_length=128)
383
+    iso_3166_1_a2 = models.CharField(
384
+        _('ISO 3166-1 alpha-2'), max_length=2, primary_key=True)
385
+    iso_3166_1_a3 = models.CharField(
386
+        _('ISO 3166-1 alpha-3'), max_length=3, blank=True)
387
+    iso_3166_1_numeric = models.CharField(
388
+        _('ISO 3166-1 numeric'), blank=True, max_length=3)
389
+
390
+    #: The commonly used name
391
+    #: e.g. 'United Kingdom'
389 392
     printable_name = models.CharField(_('Country name'), max_length=128)
393
+    #: The full official name of a country
394
+    #: e.g. 'United Kingdom of Great Britain and Northern Ireland'
395
+    name = models.CharField(_('Official name'), max_length=128)
390 396
 
391 397
     display_order = models.PositiveSmallIntegerField(
392 398
         _("Display order"), default=0, db_index=True,
393 399
         help_text=_('Higher the number, higher the country in the list.'))
394 400
 
395
-    is_shipping_country = models.BooleanField(_("Is Shipping Country"),
396
-                                              default=False, db_index=True)
401
+    is_shipping_country = models.BooleanField(
402
+        _("Is shipping country"), default=False, db_index=True)
397 403
 
398 404
     class Meta:
399 405
         abstract = True
@@ -407,13 +413,21 @@ class AbstractCountry(models.Model):
407 413
     @property
408 414
     def code(self):
409 415
         """
410
-        Shorthand for the ISO 3166 code
416
+        Shorthand for the ISO 3166 Alpha-2 code
411 417
         """
412 418
         return self.iso_3166_1_a2
413 419
 
414 420
     @property
415 421
     def numeric_code(self):
416
-        return u"%.03d" % self.iso_3166_1_numeric
422
+        """
423
+        Shorthand for the ISO 3166 numeric code.
424
+
425
+        iso_3166_1_numeric used to wrongly be a integer field, but has to be
426
+        padded with leading zeroes. It's since been converted to a char field,
427
+        but the database might still contain non-padded strings. That's why
428
+        the padding is kept.
429
+        """
430
+        return u"%.03d" % int(self.iso_3166_1_numeric)
417 431
 
418 432
 
419 433
 class AbstractShippingAddress(AbstractAddress):

+ 106
- 0
oscar/apps/address/migrations/0012_auto__del_index_country_iso_3166_1_a3__chg_field_country_iso_3166_1_nu.py View File

@@ -0,0 +1,106 @@
1
+# -*- coding: utf-8 -*-
2
+from south.utils import datetime_utils as datetime
3
+from south.db import db
4
+from south.v2 import SchemaMigration
5
+from django.db import models
6
+
7
+from oscar.core.compat import AUTH_USER_MODEL, AUTH_USER_MODEL_NAME
8
+
9
+
10
+class Migration(SchemaMigration):
11
+
12
+    def forwards(self, orm):
13
+        # Removing index on 'Country', fields ['iso_3166_1_a3']
14
+        db.delete_index(u'address_country', ['iso_3166_1_a3'])
15
+
16
+
17
+        # Changing field 'Country.iso_3166_1_numeric'
18
+        db.alter_column(u'address_country', 'iso_3166_1_numeric', self.gf('django.db.models.fields.CharField')(default='', max_length=3))
19
+        # Removing index on 'Country', fields ['iso_3166_1_numeric']
20
+        db.delete_index(u'address_country', ['iso_3166_1_numeric'])
21
+
22
+
23
+    def backwards(self, orm):
24
+        # Adding index on 'Country', fields ['iso_3166_1_numeric']
25
+        db.create_index(u'address_country', ['iso_3166_1_numeric'])
26
+
27
+        # Adding index on 'Country', fields ['iso_3166_1_a3']
28
+        db.create_index(u'address_country', ['iso_3166_1_a3'])
29
+
30
+
31
+        # Changing field 'Country.iso_3166_1_numeric'
32
+        db.alter_column(u'address_country', 'iso_3166_1_numeric', self.gf('django.db.models.fields.PositiveSmallIntegerField')(null=True))
33
+
34
+    models = {
35
+        u'address.country': {
36
+            'Meta': {'ordering': "('-display_order', 'name')", 'object_name': 'Country'},
37
+            'display_order': ('django.db.models.fields.PositiveSmallIntegerField', [], {'default': '0', 'db_index': 'True'}),
38
+            'is_shipping_country': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
39
+            'iso_3166_1_a2': ('django.db.models.fields.CharField', [], {'max_length': '2', 'primary_key': 'True'}),
40
+            'iso_3166_1_a3': ('django.db.models.fields.CharField', [], {'max_length': '3', 'blank': 'True'}),
41
+            'iso_3166_1_numeric': ('django.db.models.fields.CharField', [], {'max_length': '3', 'blank': 'True'}),
42
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
43
+            'printable_name': ('django.db.models.fields.CharField', [], {'max_length': '128'})
44
+        },
45
+        u'address.useraddress': {
46
+            'Meta': {'ordering': "['-num_orders']", 'unique_together': "(('user', 'hash'),)", 'object_name': 'UserAddress'},
47
+            'country': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['address.Country']"}),
48
+            'date_created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
49
+            'first_name': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
50
+            'hash': ('django.db.models.fields.CharField', [], {'max_length': '255', 'db_index': 'True'}),
51
+            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
52
+            'is_default_for_billing': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
53
+            'is_default_for_shipping': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
54
+            'last_name': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
55
+            'line1': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
56
+            'line2': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
57
+            'line3': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
58
+            'line4': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
59
+            'notes': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
60
+            'num_orders': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
61
+            'phone_number': ('oscar.models.fields.PhoneNumberField', [], {'max_length': '128', 'blank': 'True'}),
62
+            'postcode': ('oscar.models.fields.UppercaseCharField', [], {'max_length': '64', 'blank': 'True'}),
63
+            'search_text': ('django.db.models.fields.TextField', [], {}),
64
+            'state': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
65
+            'title': ('django.db.models.fields.CharField', [], {'max_length': '64', 'blank': 'True'}),
66
+            'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'addresses'", 'to': u"orm['{0}']".format(AUTH_USER_MODEL)})
67
+        },
68
+        u'auth.group': {
69
+            'Meta': {'object_name': 'Group'},
70
+            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
71
+            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
72
+            'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
73
+        },
74
+        u'auth.permission': {
75
+            'Meta': {'ordering': "(u'content_type__app_label', u'content_type__model', u'codename')", 'unique_together': "((u'content_type', u'codename'),)", 'object_name': 'Permission'},
76
+            'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
77
+            'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contenttypes.ContentType']"}),
78
+            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
79
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
80
+        },
81
+        AUTH_USER_MODEL: {
82
+            'Meta': {'object_name': AUTH_USER_MODEL_NAME},
83
+            'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
84
+            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
85
+            'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
86
+            'groups': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "u'user_set'", 'blank': 'True', 'to': u"orm['auth.Group']"}),
87
+            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
88
+            'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
89
+            'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
90
+            'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
91
+            'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
92
+            'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
93
+            'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
94
+            'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "u'user_set'", 'blank': 'True', 'to': u"orm['auth.Permission']"}),
95
+            'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
96
+        },
97
+        u'contenttypes.contenttype': {
98
+            'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
99
+            'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
100
+            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
101
+            'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
102
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
103
+        }
104
+    }
105
+
106
+    complete_apps = ['address']

Loading…
Cancel
Save