Skip to content

Commit

Permalink
Merge branch 'release/0.4.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Aubert committed Oct 9, 2013
2 parents 97d7e44 + d2aed1b commit 85842fc
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ env:
install:
- pip install -q Django==$DJANGO_VERSION
- python setup.py -q install
- pip install -r requirements.txt --use-mirrors
- pip install -r requirements/install.pip --use-mirrors
- pip install -r requirements/testing.pip --use-mirrors

script: django-admin.py test geo --settings=geo.test_settings
9 changes: 9 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
0.4.1
=================================

.. note:: This version is backward incompatible

* fixed bugs causing dumpdata/loaddata not to work
* add uuid also to currency


0.4
=================================

Expand Down
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ include AUTHORS
include LICENSE
include MANIFEST.in
include setup.py
include requirements.txt
include requirements/*.pip
recursive-include geo *.py
recursive-include geo/fixtures *.json
recursive-include geo/templates *.html
Expand Down
6 changes: 4 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ Examples
name ='Comune di Roma',
type=comune,
parent=roma_provincia)
city, __ = LocationType.objects.get_or_create(description='CITY')
roma, __ = Location.objects.get_or_create(country=italy,
name ='Roma',
type=Location.CITY,
type=city,
area=roma_comune)

Two levels, ::
Expand All @@ -60,9 +61,10 @@ Examples
name ='Columbia',
type=county,
parent=ny)
city, __ = LocationType.objects.get_or_create(description='CITY')
hudson, __ = Location.objects.get_or_create(country=us,
name ='Hudson',
type=Location.CITY,
type=city,
area=columbia,
is_administrative=True)

Expand Down
12 changes: 8 additions & 4 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ Two levels
::

italy = Country.objects.get(iso_code='IT')
city, __ = LocationType.objects.get_or_create(description='CITY')
roma_city, __ = Location.objects.get_or_create(country=italy,
name ='Roma',
type=Location.CITY)
type=city)


Three levels
Expand All @@ -32,9 +33,10 @@ Three levels
name ='Lazio',
type=regione)

city, __ = LocationType.objects.get_or_create(description='CITY')
roma_city, __ = Location.objects.get_or_create(country=italy,
name ='Roma',
type=Location.CITY,
type=city,
area=lazio)

Four levels
Expand Down Expand Up @@ -66,9 +68,10 @@ Four levels
name ='Columbia',
type=county,
parent=ny)
city, __ = LocationType.objects.get_or_create(description='CITY')
hudson, __ = Location.objects.get_or_create(country=us,
name ='Hudson',
type=Location.CITY,
type=city,
area=columbia,
is_administrative=True)

Expand Down Expand Up @@ -120,8 +123,9 @@ Five levels
name ='Comune di Roma',
type=comune,
parent=roma_provincia)
city, __ = LocationType.objects.get_or_create(description='CITY')
roma_city, __ = Location.objects.get_or_create(country=italy,
name ='Roma',
type=Location.CITY,
type=city,
area=roma_comune)

2 changes: 1 addition & 1 deletion geo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os

NAME = 'django-geo'
VERSION = __version__ = (0, 4, 0, 'final', 0)
VERSION = __version__ = (0, 4, 1, 'final', 0)
__author__ = 'sax'


Expand Down
114 changes: 114 additions & 0 deletions geo/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import random
import string
from itertools import cycle
import uuidfield
from django_sample_data.sample import text
from django_sample_data.api import nextname
from django_any import any_field, any_model
from django_any.contrib import any_model_with_defaults
from geo.models import Currency, Country, AdministrativeAreaType, AdministrativeArea, LocationType, Location


# Create two-character iso-codes
iso2_codes = []
for c1 in string.ascii_uppercase:
for c2 in string.ascii_uppercase:
iso2_codes.append('{}{}'.format(c1, c2))
random.shuffle(iso2_codes)
iso2_codes_iter = cycle(iso2_codes)

# Create three-character iso-codes
iso3_codes = []
for c1 in string.ascii_uppercase:
for c2 in string.ascii_uppercase:
for c3 in string.ascii_uppercase:
iso3_codes.append('{}{}{}'.format(c1, c2, c3))
random.shuffle(iso3_codes)
iso3_codes_iter = cycle(iso3_codes)

# Create iso-number with three digits
iso_numbers = range(100, 999)
random.shuffle(iso_numbers)
iso_numbers_iter = cycle(iso_numbers)


@any_field.register(uuidfield.UUIDField)
def uuid_field(field, **kwargs):
return uuidfield.UUIDField()._create_uuid()


def subargs(kwargs, prefix):
prefix = "%s__" % prefix
ret = {key[len(prefix):]: kwargs.pop(key) for key in kwargs.keys() if key.startswith(prefix)}
return ret


def currency_factory(**kwargs):
return any_model(Currency)


def country_factory(**kwargs):
iso_code = iso2_codes_iter.next()
currency = any_model(Currency, code=nextname(iso_code))

kwargs.setdefault('iso_code', iso_code)
kwargs.setdefault('num_code', iso_numbers_iter.next())
kwargs.setdefault('iso3_code', iso3_codes_iter.next())
kwargs.setdefault('name', nextname('Country'))
kwargs.setdefault('fullname', text(20))
kwargs.setdefault('currency', currency)

country = any_model(Country, **kwargs)
return country


def area_tree_factory(country):
parent = area_factory(country, name=nextname('RegionArea'), type__name=nextname('Region'))
area = area_factory(parent=parent, name=nextname('SubRegionArea'), type__name=nextname('SubRegion'))
location = location_factory(area=area)
return parent, area, location


def area_type_factory(country, **kwargs):
kwargs.setdefault('name', nextname('AdministrativeAreaType'))
kwargs.setdefault('parent', None)
return AdministrativeAreaType.objects.get_or_create(country=country, **kwargs)[0]


def area_factory(country=None, **kwargs):
if 'parent' in kwargs:
country = kwargs['parent'].country
type_kwargs = subargs(kwargs, 'type')
type_kwargs['country'] = country
type_kwargs['parent'] = kwargs['parent'].type
kwargs['type'] = area_type_factory(**type_kwargs)
else:
type_kwargs = subargs(kwargs, 'type')
type_kwargs['country'] = country
kwargs['type'] = area_type_factory(**type_kwargs)

kwargs.setdefault('parent', None)
kwargs.setdefault('name', nextname('AdministrativeArea'))

return AdministrativeArea.objects.get_or_create(country=country, **kwargs)[0]


def location_type_factory(**kwargs):
return any_model_with_defaults(LocationType, **kwargs)


def location_factory(**kwargs):
if 'area' in kwargs:
kwargs['country'] = kwargs['area'].country
else:
if not 'country' in kwargs:
kwargs['country'] = country_factory(**subargs(kwargs, 'country'))
area_kwargs = subargs(kwargs, 'area')
area_kwargs['country'] = kwargs['country']
kwargs['area'] = area_factory(**area_kwargs)
if 'type' not in kwargs:
kwargs['type'] = location_type_factory(**subargs(kwargs, 'type'))

kwargs.setdefault('name', nextname('Location'))
location = any_model_with_defaults(Location, **kwargs)
return location
23 changes: 17 additions & 6 deletions geo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@
from django.db.models.manager import Manager
from django.utils.translation import ugettext_lazy as _
import logging
from mptt.exceptions import InvalidMove
from mptt.managers import TreeManager
from mptt.models import MPTTModel, TreeForeignKey

logger = logging.getLogger("geo")


class CurrencyManager(Manager):
use_for_related_fields = True

def get_by_natural_key(self, uuid):
return self.get(uuid=uuid)


class Currency(models.Model):
uuid = UUIDField(auto=True, blank=False, version=1, help_text=_('unique id'))
code = models.CharField(max_length=5, unique=True, help_text="ISO 4217 code")
name = models.CharField(max_length=30)
symbol = models.CharField(max_length=5, blank=True, null=True)
objects = CurrencyManager()

class Meta:
app_label = 'geo'
Expand All @@ -30,6 +38,9 @@ class Meta:
def __unicode__(self):
return unicode("%s (%s)" % (self.code, self.name))

def natural_key(self):
return (self.uuid.hex, )


CONTINENTS = (
('AF', _('Africa')),
Expand Down Expand Up @@ -145,7 +156,7 @@ def __contains__(self, item):
return item.is_descendant_of(self)

def natural_key(self):
return (self.uuid, )
return (self.uuid.hex, )

def clean(self):
if self.parent == self:
Expand Down Expand Up @@ -187,7 +198,7 @@ def __unicode__(self):
return unicode(self.name)

def natural_key(self):
return (self.uuid, )
return (self.uuid.hex, )

def clean(self):
if self.parent == self:
Expand Down Expand Up @@ -224,14 +235,15 @@ class LocationType(models.Model):
"""
uuid = UUIDField(auto=True, blank=False, version=1, help_text=_('unique id'))
description = models.CharField(unique=True, max_length=100)
objects = LocationTypeManager()

class Meta:
verbose_name_plural = _('Location Types')
verbose_name = _('Location Type')
app_label = 'geo'

def natural_key(self):
return (self.uuid, )
return (self.uuid.hex, )


class LocationManager(models.Manager):
Expand Down Expand Up @@ -271,7 +283,6 @@ class Location(models.Model):
lat = models.DecimalField(max_digits=18, decimal_places=12, blank=True, null=True)
lng = models.DecimalField(max_digits=18, decimal_places=12, blank=True, null=True)
acc = models.IntegerField(choices=ACCURACY, default=NONE, blank=True, null=True, help_text="Define the level of accuracy of lat/lng infos")

objects = LocationManager()

class Meta:
Expand All @@ -286,7 +297,7 @@ def __unicode__(self):
return unicode(self.name)

def natural_key(self):
return (self.uuid, )
return (self.uuid.hex, )

def clean(self):
if self.area and self.area.country != self.country:
Expand Down
26 changes: 25 additions & 1 deletion geo/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.core.exceptions import ValidationError
from django.db.utils import IntegrityError
from django.test import TestCase
from django.test import TestCase, TransactionTestCase
from geo.models import Country, AdministrativeArea, Location


Expand Down Expand Up @@ -116,3 +116,27 @@ def test_administrativearea_natural_key(self):
area = AdministrativeArea.objects.get(country__iso_code='IT', parent__name='Lazio')
location_2 = AdministrativeArea.objects.get_by_natural_key(*area.natural_key())
self.assertEquals(area.pk, location_2.pk)


class TestNaturalKeys(TransactionTestCase):

def test_dump_and_load(self):
import os
from geo.fixtures import location_factory, currency_factory
from django.db import transaction
from django.core.management import call_command

transaction.commit_unless_managed()
transaction.enter_transaction_management()
transaction.managed()
location = location_factory()
currency = currency_factory()

with open('tmp_fixture.json', 'w') as fixture_file:
call_command('dumpdata', use_natural_keys=True, stdout=fixture_file)

transaction.rollback()
transaction.leave_transaction_management()

call_command('loaddata', 'tmp_fixture.json', use_natural_keys=True)
os.remove('tmp_fixture.json')
1 change: 1 addition & 0 deletions requirements.txt → requirements/install.pip
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
django-mptt>=0.5.4
django-uuidfield==0.4.0

2 changes: 2 additions & 0 deletions requirements/testing.pip
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
django-whatever==0.2.3
django-sample-data>=0.1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def scan_dir( target, packages=[], data_files=[] ):
packages=packages,
data_files=data_files,
platforms=['any'],
install_requires = ['django-mptt>=0.5.4'],
install_requires = ['django-mptt>=0.5.4', 'django-uuidfield==0.4.0'],
command_options={
'build_sphinx': {
'version': ('setup.py', app.VERSION),
Expand Down
9 changes: 8 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ changedir=tests
commands =
django-admin.py test geo --settings=geo.test_settings

[base]
deps =
-rrequirements/testing.pip

[testenv:d14]
basepython = python2.7
deps =
django==1.4.5
{[base]deps}

[testenv:d15]
basepython = python2.7
deps =
django==1.5.1
{[base]deps}

[testenv:p3d15]
basepython = python3.2
basepython = python3.3
deps =
django==1.5.1
{[base]deps}

0 comments on commit 85842fc

Please sign in to comment.