From 8bef62bacca5b02f584018f3f639d937ad1c39c1 Mon Sep 17 00:00:00 2001 From: olivier <> Date: Tue, 19 Jan 2010 19:18:46 +0000 Subject: [PATCH] add methods to resolve a location's country and continent --- telemeta/models/crem.py | 26 ++++++++++++++++++++++---- telemeta/tests/model_tests.py | 10 +++++++++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/telemeta/models/crem.py b/telemeta/models/crem.py index 6207824c..fee05720 100755 --- a/telemeta/models/crem.py +++ b/telemeta/models/crem.py @@ -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() diff --git a/telemeta/tests/model_tests.py b/telemeta/tests/model_tests.py index 27d41517..cc1f540c 100644 --- a/telemeta/tests/model_tests.py +++ b/telemeta/tests/model_tests.py @@ -34,7 +34,7 @@ # David LIPSZYC 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()) -- 2.39.5