Commits

Vadim Statishin committed a206c4a

Fix cached GeoObjects

Comments (0)

Files changed (2)

geoobjects/models.py

 
     @cached_property
     def obj(self):
-        key_cache = "geo_obj_" + str(self.obj_id)
-        ob = cache.get(key_cache)
-        if ob is None:
-            try:
-                ob = GeobazaObjects.objects.get(pk=self.obj_id)
-            except ObjectDoesNotExist:
-                ob = GeobazaObjects.objects.get(pk=DEFAULT_GEO_LOCATION)
-            cache.set(key_cache, ob, CACHE_TIMEOUT)
+        ob = GeobazaObjects.get_obj(self.obj_id)
+        if not ob:
+            ob = GeobazaObjects.get_obj(DEFAULT_GEO_LOCATION)
         return ob
 
 
         return u"%s:%s" % (self.type, self.name_en)
 
     @classmethod
-    def get_obj(cls, obj_id):
-        key_cache = "geo_obj_%d" % obj_id
+    def get_obj(cls, obj_id, ):
+        if obj_id == 0 or obj_id == '0':
+            return None
+        key_cache = "geo_obj_" + str(obj_id)
         ob = cache.get(key_cache)
         if ob is None:
-            ob = GeobazaObjects.objects.get(pk=obj_id)
+            try:
+                ob = GeobazaObjects.objects.get(pk=obj_id)
+            except ObjectDoesNotExist:
+                ob = None
             cache.set(key_cache, ob, CACHE_TIMEOUT)
         return ob
 
     @cached_property
     def parent(self):
-        try:
-            return GeobazaObjects.objects.get(pk=self.parent_id)
-        except ObjectDoesNotExist:
-            return None
+        return GeobazaObjects.get_obj(self.parent_id)
 
     @cached_property
     def child(self):
-        return GeobazaObjects.objects.all().filter(parent_id=self.id)
+        key_cache = "geo_obj_child_%d" % self.id
+        objs = cache.get(key_cache)
+        if objs is None:
+            objs = list(GeobazaObjects.objects.all().filter(parent_id=self.id))
+            cache.set(key_cache, objs, CACHE_TIMEOUT)
+        return objs
 
     def get_path(self, reverse=False):
         path = [self]
 
 setup(
     name='geoobjects',
-    version='0.2.5.13',
+    version='0.2.5.14',
     packages=['geoobjects', 'geoobjects.management', 'geoobjects.management.commands'],
     scripts=['scripts/geoobjects_import_mysql.sh'],
     install_requires=['django'],