Permalink
Browse files

Merge branch 'master' of git://github.com/coderholic/django-cities

  • Loading branch information...
Qarterd committed Apr 5, 2012
2 parents 60566eb + 07c406b commit 8c1f51c96f60bf3f508c33c9947ea12f56b0879c
Showing with 54 additions and 72 deletions.
  1. +13 −4 cities/conf.py
  2. +38 −61 cities/models.py
  3. +3 −7 setup.py
View
@@ -104,16 +104,24 @@ def create_settings():
res = type('',(),{})
res.files = files.copy()
- res.files.update(django_settings.CITIES_FILES)
+ if hasattr(django_settings, "CITIES_FILES"):
+ res.files.update(django_settings.CITIES_FILES)
- locales = django_settings.CITIES_LOCALES[:]
+ if hasattr(django_settings, "CITIES_LOCALES"):
+ locales = django_settings.CITIES_LOCALES[:]
+ else:
+ locales = ['en', 'und']
+
try:
locales.remove('LANGUAGES')
locales += [e[0] for e in django_settings.LANGUAGES]
except: pass
res.locales = set([e.lower() for e in locales])
- res.postal_codes = set([e.upper() for e in django_settings.CITIES_POSTAL_CODES])
+ if hasattr(django_settings, "CITIES_POSTAL_CODES"):
+ res.postal_codes = set([e.upper() for e in django_settings.CITIES_POSTAL_CODES])
+ else:
+ res.postal_codes = set()
return res
@@ -127,4 +135,5 @@ def create_plugins():
[settings.plugins[hook].append(obj) for hook in plugin_hooks if hasattr(obj,hook)]
settings = create_settings()
-create_plugins()
+if hasattr(django_settings, "CITIES_PLUGINS"):
+ create_plugins()
View
@@ -3,107 +3,88 @@
from django.contrib.gis.geos import Point
from conf import settings
from util import create_model, un_camel
-
+
__all__ = ['Point','Country','Region','City','District','geo_alt_names','postal_codes']
-class Country(models.Model):
+class Place(models.Model):
name = models.CharField(max_length=200, db_index=True, verbose_name="ascii name")
slug = models.CharField(max_length=200)
+
+ objects = models.GeoManager()
+
+ @property
+ def hierarchy(self):
+ """Get hierarchy, root first"""
+ list = self.parent.hierarchy if self.parent else []
+ list.append(self)
+ return list
+
+ class Meta:
+ abstract = True
+
+ def get_absolute_url(self):
+ return "/".join([place.slug for place in self.hierarchy])
+
+class Country(Place):
code = models.CharField(max_length=2, db_index=True)
population = models.IntegerField()
continent = models.CharField(max_length=2)
tld = models.CharField(max_length=5)
- objects = models.GeoManager()
-
+
class Meta:
ordering = ['name']
verbose_name_plural = "countries"
@property
def parent(self):
return None
-
- @property
- def hierarchy(self):
- """Get hierarchy, root first"""
- return [self]
-
+
def __unicode__(self):
return force_unicode(self.name)
-class Region(models.Model):
- name = models.CharField(max_length=200, db_index=True, verbose_name="ascii name")
+class Region(Place):
name_std = models.CharField(max_length=200, db_index=True, verbose_name="standard name")
- slug = models.CharField(max_length=200)
code = models.CharField(max_length=200, db_index=True)
level = models.IntegerField(db_index=True, verbose_name="admin level") # Level 0 has no parent region
region_parent = models.ForeignKey('self', null=True, blank=True, related_name='region_children')
country = models.ForeignKey(Country)
- objects = models.GeoManager()
@property
def parent(self):
"""Returns parent region if available, otherwise country"""
- return self.region_parent if self.region_parent else self.country
-
- @property
- def hierarchy(self):
- """Get hierarchy, root first"""
- list = self.parent.hierarchy
- list.append(self)
- return list
-
+ return self.region_parent or self.country
+
def __unicode__(self):
- return u'{}, {}'.format(force_unicode(self.name_std), self.parent)
-
-class City(models.Model):
- name = models.CharField(max_length=200, db_index=True, verbose_name="ascii name")
+ return u'{}, {}'.format(force_unicode(self.name_std), self.parent)
+
+class City(Place):
name_std = models.CharField(max_length=200, db_index=True, verbose_name="standard name")
- slug = models.CharField(max_length=200)
region = models.ForeignKey(Region, null=True, blank=True)
country = models.ForeignKey(Country)
location = models.PointField()
population = models.IntegerField()
- objects = models.GeoManager()
class Meta:
verbose_name_plural = "cities"
-
+
@property
def parent(self):
"""Returns region if available, otherwise country"""
return self.region if self.region else self.country
-
- @property
- def hierarchy(self):
- """Get hierarchy, root first"""
- list = self.parent.hierarchy
- list.append(self)
- return list
-
+
def __unicode__(self):
return u'{}, {}'.format(force_unicode(self.name_std), self.parent)
-class District(models.Model):
- name = models.CharField(max_length=200, db_index=True, verbose_name="ascii name")
+class District(Place):
name_std = models.CharField(max_length=200, db_index=True, verbose_name="standard name")
- slug = models.CharField(max_length=200)
city = models.ForeignKey(City)
location = models.PointField()
population = models.IntegerField()
- objects = models.GeoManager()
-
+
@property
def parent(self):
return self.city
-
- @property
- def hierarchy(self):
- """Get hierarchy, root first"""
- list = self.parent.hierarchy
- list.append(self)
- return list
-
+
def __unicode__(self):
return u'{}, {}'.format(force_unicode(self.name_std), self.parent)
@@ -117,7 +98,7 @@ def get_preferred(self, default=None, **kwargs):
except self.model.DoesNotExist:
try: return self.filter(**kwargs)[0]
except IndexError: return default
-
+
def create_geo_alt_names(geo_type):
geo_alt_names = {}
for locale in settings.locales:
@@ -127,7 +108,7 @@ def create_geo_alt_names(geo_type):
name = name,
fields = {
'geo': models.ForeignKey(geo_type, # Related geo type
- related_name = 'alt_names_' + locale),
+ related_name = 'alt_names_' + locale),
'name': models.CharField(max_length=200, db_index=True), # Alternate name
'is_preferred': models.BooleanField(), # True if this alternate name is an official / preferred name
'is_short': models.BooleanField(), # True if this is a short name like 'California' for 'State of California'
@@ -148,21 +129,19 @@ def create_geo_alt_names(geo_type):
for type in [Country, Region, City, District]:
geo_alt_names[type] = create_geo_alt_names(type)
-
def create_postal_codes():
-
@property
def parent(self):
"""Returns region if available, otherwise country"""
return self.region if self.region else self.country
-
+
@property
def hierarchy(self):
"""Get hierarchy, root first"""
list = self.parent.hierarchy
list.append(self)
return list
-
+
@property
def names(self):
"""Get a hierarchy of non-null names, root first"""
@@ -173,12 +152,12 @@ def names(self):
force_unicode(self.region_2_name),
force_unicode(self.name),
] if e]
-
+
@property
def name_full(self):
"""Get full name including hierarchy"""
return u', '.join(reversed(self.names))
-
+
postal_codes = {}
for country in settings.postal_codes:
name_format = "{}" + country
@@ -214,5 +193,3 @@ def name_full(self):
return postal_codes
postal_codes = create_postal_codes()
-
-
View
@@ -1,20 +1,16 @@
from setuptools import setup, find_packages
import os
-# Utility function to read the README file.
-# Used for the long_description. It's nice, because now 1) we have a top level
-# README file and 2) it's easier to type in the README file than to put a raw
-# string in below ...
def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()
setup(
name='django-cities',
version='0.19',
description='Place models and data for Django apps',
- author='Dan Carter (original by Ben Dowling)',
- author_email='carterd@gmail.com',
- url='https://github.com/Kometes/django-cities',
+ author='Ben Dowling',
+ author_email='ben.m.dowling@gmail.com',
+ url='https://github.com/coderholic/django-cities',
packages=find_packages(),
include_package_data=True,
zip_safe=False,

0 comments on commit 8c1f51c

Please sign in to comment.