Skip to content

Commit

Permalink
six: unicode smoothing
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanLukes committed Oct 28, 2014
1 parent 8a6b73c commit 9d41b54
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
5 changes: 4 additions & 1 deletion address/forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import absolute_import
import six

import urllib.request, urllib.error, urllib.parse
from django import forms
# from uni_form.helpers import *
Expand Down Expand Up @@ -30,7 +33,7 @@ def render(self, name, value, attrs=None, **kwargs):
ad = {}
elif isinstance(value, dict):
ad = value
elif isinstance(value, int):
elif isinstance(value, six.integer_types):
ad = Address.objects.get(pk=value)
ad = ad.as_dict()
else:
Expand Down
15 changes: 11 additions & 4 deletions address/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible

import urllib.request, urllib.error, urllib.parse
from django.db import models
from django.core.exceptions import ValidationError
Expand Down Expand Up @@ -106,6 +109,7 @@ def to_python(value):
##
## A country.
##
@python_2_unicode_compatible
class Country(models.Model):
name = models.CharField(max_length=40, unique=True, blank=True)
code = models.CharField(max_length=2, blank=True) # not unique as there are duplicates (IT)
Expand All @@ -114,12 +118,13 @@ class Meta:
verbose_name_plural = 'Countries'
ordering = ('name',)

def __unicode__(self):
def __str__(self):
return '%s'%(self.name or self.code)

##
## A state. Google refers to this as `administration_level_1`.
##
@python_2_unicode_compatible
class State(models.Model):
name = models.CharField(max_length=165, blank=True)
code = models.CharField(max_length=3, blank=True)
Expand All @@ -129,7 +134,7 @@ class Meta:
unique_together = ('name', 'country')
ordering = ('country', 'name')

def __unicode__(self):
def __str__(self):
txt = self.to_str()
country = '%s'%self.country
if country and txt:
Expand All @@ -143,6 +148,7 @@ def to_str(self):
##
## A locality (suburb).
##
@python_2_unicode_compatible
class Locality(models.Model):
name = models.CharField(max_length=165, blank=True)
postal_code = models.CharField(max_length=10, blank=True)
Expand All @@ -153,7 +159,7 @@ class Meta:
unique_together = ('name', 'state')
ordering = ('state', 'name')

def __unicode__(self):
def __str__(self):
txt = '%s'%self.name
state = self.state.to_str() if self.state else ''
if txt and state:
Expand All @@ -170,6 +176,7 @@ def __unicode__(self):
## An address. If for any reason we are unable to find a matching
## decomposed address we will store the raw address string in `raw`.
##
@python_2_unicode_compatible
class Address(models.Model):
street_number = models.CharField(max_length=20, blank=True)
route = models.CharField(max_length=100, blank=True)
Expand All @@ -184,7 +191,7 @@ class Meta:
ordering = ('locality', 'route', 'street_number')
unique_together = ('locality', 'route', 'street_number')

def __unicode__(self):
def __str__(self):
if self.formatted != '':
txt = '%s'%self.formatted
elif self.locality:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
packages=find_packages(),
include_package_data=True,
package_data={'': ['*.txt', '*.js', '*.html', '*.*']},
install_requires=['setuptools'],
install_requires=['setuptools', 'six'],
zip_safe=False,

)

0 comments on commit 9d41b54

Please sign in to comment.