# Path: cities/models.py
# class Country(BaseCountry):
#     class Meta(BaseCountry.Meta):
#         swappable = swapper.swappable_setting('cities', 'Country')
# 
# class Region(Place, SlugModel):
#     name_std = models.CharField(max_length=200, db_index=True, verbose_name="standard name")
#     code = models.CharField(max_length=200, db_index=True)
#     country = models.ForeignKey(swapper.get_model_name('cities', 'Country'),
#                                 related_name='regions',
#                                 on_delete=SET_NULL_OR_CASCADE)
# 
#     class Meta:
#         unique_together = (('country', 'name'),)
# 
#     @property
#     def parent(self):
#         return self.country
# 
#     def full_code(self):
#         return unicode_func(".".join([self.parent.code, self.code]))
# 
#     def slugify(self):
#         return '{}_({})'.format(
#             unicode_func(self.name),
#             unicode_func(self.full_code()))
# 
# class City(BaseCity):
#     class Meta(BaseCity.Meta):
#         swappable = swapper.swappable_setting('cities', 'City')
# 
# class District(Place, SlugModel):
#     slug_contains_id = True
# 
#     name_std = models.CharField(max_length=200, db_index=True, verbose_name="standard name")
#     code = models.CharField(blank=True, db_index=True, max_length=200, null=True)
#     location = PointField()
#     population = models.IntegerField()
#     city = models.ForeignKey(swapper.get_model_name('cities', 'City'),
#                              related_name='districts',
#                              on_delete=SET_NULL_OR_CASCADE)
# 
#     class Meta:
#         unique_together = (('city', 'name'),)
# 
#     @property
#     def parent(self):
#         return self.city
# 
#     def slugify(self):
#         if self.id:
#             return '{}-{}'.format(self.id, unicode_func(self.name))
#         return None
# 
# class PostalCode(Place, SlugModel):
#     slug_contains_id = True
# 
#     code = models.CharField(max_length=20)
#     location = PointField()
# 
#     country = models.ForeignKey(swapper.get_model_name('cities', 'Country'),
#                                 related_name='postal_codes',
#                                 on_delete=SET_NULL_OR_CASCADE)
# 
#     # Region names for each admin level, region may not exist in DB
#     region_name = models.CharField(max_length=100, db_index=True)
#     subregion_name = models.CharField(max_length=100, db_index=True)
#     district_name = models.CharField(max_length=100, db_index=True)
# 
#     region = models.ForeignKey(Region,
#                                blank=True,
#                                null=True,
#                                related_name='postal_codes',
#                                on_delete=SET_NULL_OR_CASCADE)
#     subregion = models.ForeignKey(Subregion,
#                                   blank=True,
#                                   null=True,
#                                   related_name='postal_codes',
#                                   on_delete=SET_NULL_OR_CASCADE)
#     city = models.ForeignKey(swapper.get_model_name('cities', 'City'),
#                              blank=True,
#                              null=True,
#                              related_name='postal_codes',
#                              on_delete=SET_NULL_OR_CASCADE)
#     district = models.ForeignKey(District,
#                                  blank=True,
#                                  null=True,
#                                  related_name='postal_codes',
#                                  on_delete=SET_NULL_OR_CASCADE)
# 
#     objects = GeoManager()
# 
#     class Meta:
#         unique_together = (
#             ('country', 'region', 'subregion', 'city', 'district', 'name', 'id', 'code'),
#             ('country', 'region_name', 'subregion_name', 'district_name', 'name', 'id', 'code'),
#         )
# 
#     @property
#     def parent(self):
#         return self.country
# 
#     @property
#     def name_full(self):
#         """Get full name including hierarchy"""
#         return force_text(', '.join(reversed(self.names)))
# 
#     @property
#     def names(self):
#         """Get a hierarchy of non-null names, root first"""
#         return [e for e in [
#             force_text(self.country),
#             force_text(self.region_name),
#             force_text(self.subregion_name),
#             force_text(self.district_name),
#             force_text(self.name),
#         ] if e]
# 
#     def __str__(self):
#         return force_text(self.code)
# 
#     def slugify(self):
#         if self.id:
#             return '{}-{}'.format(self.id, unicode_func(self.code))
#         return None


from django import VERSION as DJANGO_VERSION
from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic import ListView
from cities.models import (Country, Region, City, District, PostalCode)
        from django.conf.urls import patterns as django_patterns


class PlaceListView(ListView):
    template_name = "list.html"

    def get_queryset(self):
        if not self.args or not self.args[0]:
            self.place = None
            return Country.objects.all()
        args = self.args[0].split("/")

        country = Country.objects.get(slug=args[0])
        if len(args) == 1:
            self.place = country
            return Region.objects.filter(country=country).order_by('name')

        region = Region.objects.get(country=country, slug=args[1])
        if len(args) == 2:
            self.place = region
            return City.objects.filter(region=region).order_by('name')

        city = City.objects.get(region=region, slug=args[2])
        self.place = city
        return District.objects.filter(city=city).order_by('name')

    def get_context_data(self, **kwargs):
        context = super(PlaceListView, self).get_context_data(**kwargs)
        context['place'] = self.place

        if hasattr(self.place, 'location'):

# Path: cities/models.py
# class Country(BaseCountry):
#     class Meta(BaseCountry.Meta):
#         swappable = swapper.swappable_setting('cities', 'Country')
# 
# class Region(Place, SlugModel):
#     name_std = models.CharField(max_length=200, db_index=True, verbose_name="standard name")
#     code = models.CharField(max_length=200, db_index=True)
#     country = models.ForeignKey(swapper.get_model_name('cities', 'Country'),
#                                 related_name='regions',
#                                 on_delete=SET_NULL_OR_CASCADE)
# 
#     class Meta:
#         unique_together = (('country', 'name'),)
# 
#     @property
#     def parent(self):
#         return self.country
# 
#     def full_code(self):
#         return unicode_func(".".join([self.parent.code, self.code]))
# 
#     def slugify(self):
#         return '{}_({})'.format(
#             unicode_func(self.name),
#             unicode_func(self.full_code()))
# 
# class City(BaseCity):
#     class Meta(BaseCity.Meta):
#         swappable = swapper.swappable_setting('cities', 'City')
# 
# class District(Place, SlugModel):
#     slug_contains_id = True
# 
#     name_std = models.CharField(max_length=200, db_index=True, verbose_name="standard name")
#     code = models.CharField(blank=True, db_index=True, max_length=200, null=True)
#     location = PointField()
#     population = models.IntegerField()
#     city = models.ForeignKey(swapper.get_model_name('cities', 'City'),
#                              related_name='districts',
#                              on_delete=SET_NULL_OR_CASCADE)
# 
#     class Meta:
#         unique_together = (('city', 'name'),)
# 
#     @property
#     def parent(self):
#         return self.city
# 
#     def slugify(self):
#         if self.id:
#             return '{}-{}'.format(self.id, unicode_func(self.name))
#         return None
# 
# class PostalCode(Place, SlugModel):
#     slug_contains_id = True
# 
#     code = models.CharField(max_length=20)
#     location = PointField()
# 
#     country = models.ForeignKey(swapper.get_model_name('cities', 'Country'),
#                                 related_name='postal_codes',
#                                 on_delete=SET_NULL_OR_CASCADE)
# 
#     # Region names for each admin level, region may not exist in DB
#     region_name = models.CharField(max_length=100, db_index=True)
#     subregion_name = models.CharField(max_length=100, db_index=True)
#     district_name = models.CharField(max_length=100, db_index=True)
# 
#     region = models.ForeignKey(Region,
#                                blank=True,
#                                null=True,
#                                related_name='postal_codes',
#                                on_delete=SET_NULL_OR_CASCADE)
#     subregion = models.ForeignKey(Subregion,
#                                   blank=True,
#                                   null=True,
#                                   related_name='postal_codes',
#                                   on_delete=SET_NULL_OR_CASCADE)
#     city = models.ForeignKey(swapper.get_model_name('cities', 'City'),
#                              blank=True,
#                              null=True,
#                              related_name='postal_codes',
#                              on_delete=SET_NULL_OR_CASCADE)
#     district = models.ForeignKey(District,
#                                  blank=True,
#                                  null=True,
#                                  related_name='postal_codes',
#                                  on_delete=SET_NULL_OR_CASCADE)
# 
#     objects = GeoManager()
# 
#     class Meta:
#         unique_together = (
#             ('country', 'region', 'subregion', 'city', 'district', 'name', 'id', 'code'),
#             ('country', 'region_name', 'subregion_name', 'district_name', 'name', 'id', 'code'),
#         )
# 
#     @property
#     def parent(self):
#         return self.country
# 
#     @property
#     def name_full(self):
#         """Get full name including hierarchy"""
#         return force_text(', '.join(reversed(self.names)))
# 
#     @property
#     def names(self):
#         """Get a hierarchy of non-null names, root first"""
#         return [e for e in [
#             force_text(self.country),
#             force_text(self.region_name),
#             force_text(self.subregion_name),
#             force_text(self.district_name),
#             force_text(self.name),
#         ] if e]
# 
#     def __str__(self):
#         return force_text(self.code)
# 
#     def slugify(self):
#         if self.id:
#             return '{}-{}'.format(self.id, unicode_func(self.code))
#         return None


# Path: example/urls.py
from django import VERSION as DJANGO_VERSION
from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic import ListView
from cities.models import (Country, Region, City, District, PostalCode)
        from django.conf.urls import patterns as django_patterns



class PlaceListView(ListView):
    template_name = "list.html"

    def get_queryset(self):
        if not self.args or not self.args[0]:
            self.place = None
            return Country.objects.all()
        args = self.args[0].split("/")

        country = Country.objects.get(slug=args[0])
        if len(args) == 1:
            self.place = country
            return Region.objects.filter(country=country).order_by('name')

        region = Region.objects.get(country=country, slug=args[1])
        if len(args) == 2:
            self.place = region
            return City.objects.filter(region=region).order_by('name')

        city = City.objects.get(region=region, slug=args[2])
        self.place = city
        return District.objects.filter(city=city).order_by('name')

    def get_context_data(self, **kwargs):
        context = super(PlaceListView, self).get_context_data(**kwargs)
        context['place'] = self.place

        if hasattr(self.place, 'location'):

            context['nearby'] = City.objects.distance(self.place.location).exclude(id=self.place.id).order_by('distance')[:10]