Commits

iparkcorp committed cd5ba12

Add property country to GeoObject
Fix cached property

Comments (0)

Files changed (2)

geoobjects/models.py

 from django.core.exceptions import ObjectDoesNotExist
 from django.core.cache import cache
 from django.core.exceptions import FieldError
+from django.utils.functional import cached_property
+
 
 __author__ = 'vadim'
 
                 ob = None
         return ob
 
-    @property
+    @cached_property
     def obj(self):
         key_cache = "geo_obj_" + str(self.obj_id)
         ob = cache.get(key_cache)
             cache.set(key_cache, ob)
         return ob
 
-    @property
+    @cached_property
     def parent(self):
         try:
             return GeobazaObjects.objects.get(pk=self.parent_id)
         except ObjectDoesNotExist:
             return None
 
-    @property
+    @cached_property
     def child(self):
         return GeobazaObjects.objects.all().filter(parent_id=self.id)
 
     def to_dict(self, lang='en'):
         return {self.id: self.name(lang)}
 
-    @property
+    @cached_property
     def path(self):
         return self.get_path()
 
+    @cached_property
+    def country(self):
+        for obj in self.path:
+            if obj.type == 'country':
+                return obj
+        return None
+
     def name(self, lang='en'):
         return u', '.join([path.name_ru if lang == 'ru' and path.name_ru else path.name_en
                            for path in self.get_path(False)])
 
-    @property
+    @cached_property
     def dump_path(self):
         path = self.path
         if not path:  # or (hasattr(path, 'is_special') and path.is_special):
         ret = DELIM.join([str(obj.id) for obj in path])
         return DELIM + ret + DELIM
 
-    @property
+    @cached_property
     def dump_key(self):
         return DELIM + str(self.id) + DELIM
 
 
 setup(
     name='geoobjects',
-    version='0.2.5.7',
+    version='0.2.5.8',
     packages=['geoobjects', 'geoobjects.management', 'geoobjects.management.commands'],
     scripts=['scripts/geoobjects_import_mysql.sh'],
     install_requires=['django'],