]> git.parisson.com Git - telemeta.git/commitdiff
add methods to resolve a location's country and continent
authorolivier <>
Tue, 19 Jan 2010 19:18:46 +0000 (19:18 +0000)
committerolivier <>
Tue, 19 Jan 2010 19:18:46 +0000 (19:18 +0000)
telemeta/models/crem.py
telemeta/tests/model_tests.py

index 6207824c10b396b509cee2785a1ca93a11080722..fee057205ba32418849f0f0a64904459bb7b8b63 100755 (executable)
@@ -426,6 +426,24 @@ class Location(ModelCore):
                                          db_column="current_name", null=True) 
     is_authoritative = models.BooleanField(default=0)
 
+    def _by_type(self, typename):
+        location = self
+        while location.type != typename:
+            relations = location.parent_relations
+            if relations:
+                location = relations.all()[0].parent_location
+            else:
+                location = None
+                break
+
+        return location
+
+    def country(self):
+        return self._by_type('country')
+
+    def continent(self):
+        return self._by_type('continent')
+
     class Meta(MetaCore):
         db_table = 'locations'
 
@@ -442,20 +460,20 @@ class LocationType(ModelCore):
 
 class LocationAlias(ModelCore):
     "Location other name"
-    location_name    = models.ForeignKey('Location', related_name="aliases",
+    location         = models.ForeignKey('Location', related_name="aliases",
                                           db_column="location_name", max_length=150)
     alias            = models.CharField(max_length=150)
     is_authoritative = models.BooleanField(default=0)
 
     class Meta(MetaCore):
         db_table = 'location_aliases'
-        unique_together = (('location_name', 'alias'),)
+        unique_together = (('location', 'alias'),)
     
 class LocationRelation(ModelCore):
     "Location family"
-    location_name        = models.ForeignKey('Location', related_name="parent_relations",
+    location             = models.ForeignKey('Location', related_name="parent_relations",
                                               db_column="location_name", max_length=150)
-    parent_location_name = models.ForeignKey('Location', related_name="child_relations",
+    parent_location      = models.ForeignKey('Location', related_name="child_relations",
                                               db_column="parent_location_name", null=True, max_length=150)
     is_authoritative     = models.BooleanField()
 
index 27d41517aa00da187bf585fd42666faef4110992..cc1f540cab4483eb30c8403a9fba7e56925d8ee9 100644 (file)
@@ -34,7 +34,7 @@
 #          David LIPSZYC <davidlipszyc@gmail.com>
 
 import unittest
-from telemeta.models import MediaCollection, MediaItem, Location, EthnicGroup, LocationType, User, Revision, RequiredFieldError
+from telemeta.models import *
 from datetime import datetime, timedelta
 
 class CollectionItemTestCase(unittest.TestCase):
@@ -56,6 +56,9 @@ class CollectionItemTestCase(unittest.TestCase):
         self.europe = Location.objects.create(name="Europe", type="continent", complete_type=self.continent)
         self.belgique = Location.objects.create(name="Belgique", type="country", complete_type=self.country)
 
+        LocationRelation.objects.create(location=self.paris, parent_location=self.france)
+        LocationRelation.objects.create(location=self.france, parent_location=self.europe)
+
         EthnicGroup.objects.all().delete()
         self.a = EthnicGroup.objects.create(name="a")
         self.b = EthnicGroup.objects.create(name="b")
@@ -259,3 +262,8 @@ class CollectionItemTestCase(unittest.TestCase):
         doc = self.item_4.to_dom()
         self.assertEquals(doc.getElementsByTagName('collection')[0].getAttribute('key'), str(self.persepolis.id))
 
+    def testLocationRelation(self):
+        self.assertEquals(self.france, self.item_1.location.country())
+        self.assertEquals(self.europe, self.item_1.location.continent())
+        self.assertEquals(self.france, self.item_2.location.country())
+        self.assertEquals(self.europe, self.item_2.location.continent())