from django.contrib.gis.db import models

from photologue.models import Photo

from category.models import Category


class Country(models.Model):
    objects = models.GeoManager()
    
    name = models.CharField(max_length=50)
    country_code = models.CharField(
        max_length=2,
        unique=True,
        db_index=True,
    )
    border = models.MultiPolygonField(
        srid=4326,
        geography=True,
        null=True,
        blank=True
    )

    class Meta:
        verbose_name_plural = 'Countries'
        ordering = ('name',)

    def __unicode__(self):
        return self.name
        

class City(models.Model):
    objects = models.GeoManager()
    
    name = models.CharField(
        max_length=128,
        db_index=True
    )
    coordinates = models.PointField(
        srid=4326,
        geography=True,
        null=True,
        blank=True
    )
    country = models.ForeignKey(Country)
    
    class Meta:
        verbose_name_plural = 'Cities'
        ordering = ('name',)
    
    def __unicode__(self):
        return "%s (%s)" % (self.name, self.country.country_code)


class Location(models.Model):
    objects = models.GeoManager()
    
    name = models.CharField(
        max_length=128,
        db_index=True
    )
    # geography is True because we need to deal with global coordinates
    coordinates = models.PointField(
        srid=4326,
        geography=True,
        null=True,
        blank=True
    )
    country = models.ForeignKey(
        Country,
    )
    city = models.ForeignKey(
        City,
        null=True,
        blank=True
    )
    description = models.TextField(
        max_length=1024,
        null=True,
        blank=True
    )
    photo = models.ForeignKey(
        Photo,
        null=True,
        blank=True
    )
    category = models.ForeignKey(
        Category,
        null=True,
        blank=True
    )
    
    def __unicode__(self):
        return self.name


# must override the default manager with GeoManager to allow for queries on related objects
#Category.add_to_class('objects', models.GeoManager())
#ImageModel.add_to_class('objects', models.GeoManager())

@@ -327,24 +327,96 @@
 True,%0A    )%0A
+    # geography is True because we need to deal with global coordinates%0A
     border =
@@ -438,24 +438,24 @@
 lygonField(%0A
-
         srid
@@ -680,16 +680,581 @@
       %0A%0A
+class Region(models.Model):%0A    objects = models.GeoManager()%0A    %0A    name = models.CharField(max_length=128)%0A    # uses fips10-4 2-digit region codes%0A    code = models.CharField(%0A        max_length=2,%0A        unique=True,%0A        db_index=True%0A    )%0A    border = models.MultiPolygonField(%0A        srid=4326,%0A        geography=True,%0A        null=True,%0A        blank=True%0A    )%0A    country = models.ForeignKey(Country)%0A    %0A    class Meta:%0A        ordering = ('name',)%0A%0A    def __unicode__(self):%0A        return %22%25s (%25s)%22 %25 (self.name, self.country.country_code)%0A%0A%0A
 class Ci
@@ -1507,32 +1507,124 @@
 lank=True%0A    )%0A
+    region = models.ForeignKey(%0A        Region,%0A        null=True,%0A        blank=True%0A    )%0A
     country = mo
@@ -1761,32 +1761,161 @@
 nicode__(self):%0A
+        if self.region is not None:%0A            return %22%25s - %25s (%25s)%22 %25 (self.name, self.region.name, self.country.country_code)%0A
         return %22
@@ -2120,80 +2120,8 @@
   )%0A
-    # geography is True because we need to deal with global coordinates%0A
     
@@ -2276,30 +2276,15 @@
 Key(
-%0A        
 Country
-,%0A    
 )%0A  
@@ -2700,32 +2700,182 @@
         
-return self.name
+if self.city is not None:%0A            return %22%25s - %25s (%25s)%22 %25 (self.name, self.city.name, self.country.name)%0A        return %22%25s (%25s)%22 %25 (self.name, self.country.name)
 %0A%0A%0A# mus

add a region model