]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/settings/base.py
removing unnecessery line causing IndentationError in the log file.
[osqa.git] / forum / settings / base.py
index 398ed11907d7b33c214d168eb5036ffa2900ef40..7436f46593b4e2813f292b91662ed21323a15a9b 100644 (file)
@@ -1,14 +1,19 @@
 import django.dispatch
 from django.utils.encoding import force_unicode
+from datetime import datetime, timedelta
+import logging
+
+TMP_MINICACHE_SECONDS = 5
 
 class SettingSet(list):
-    def __init__(self, name, title, description, weight=1000, markdown=False):
+    def __init__(self, name, title, description, weight=1000, markdown=False, can_preview=False):
         self.name = name
         self.title = title
         self.description = description
         self.weight = weight
         self.markdown = markdown
-        
+        self.can_preview = can_preview
+
 
 class BaseSetting(object):
     @classmethod
@@ -23,7 +28,11 @@ class BaseSetting(object):
         self.default = default
         self.field_context = field_context or {}
 
+        self._temp = None
+
         if set is not None:
+            self.set = set
+            
             if not set.name in Setting.sets:
                 Setting.sets[set.name] = set
 
@@ -37,18 +46,29 @@ class BaseSetting(object):
 
     @property
     def value(self):
+        if self._temp:
+            v, exp = self._temp
+            if exp + timedelta(seconds=TMP_MINICACHE_SECONDS) > datetime.now():
+                return v
+
         from forum.models import KeyValue
 
         try:
             kv = KeyValue.objects.get(key=self.name)
-        except:
-            kv = KeyValue(key=self.name, value=self._parse(self.default))
-            kv.save()
-
-        return kv.value
+            v = kv.value
+            self._temp = (v, datetime.now() + timedelta(seconds=TMP_MINICACHE_SECONDS))
+            return v
+        except KeyValue.DoesNotExist:
+            self._temp = (self.default, datetime.now() + timedelta(seconds=TMP_MINICACHE_SECONDS))
+            self.save(self.default)
+        except Exception, e:
+            logging.error("Error retrieving setting from database (%s): %s" % (self.name, str(e)))
+            
+        return self.default
 
     def set_value(self, new_value):
         new_value = self._parse(new_value)
+        self._temp = None
         self.save(new_value)
 
     def save(self, value):
@@ -56,8 +76,11 @@ class BaseSetting(object):
 
         try:
             kv = KeyValue.objects.get(key=self.name)
-        except:
+        except KeyValue.DoesNotExist:
             kv = KeyValue(key=self.name)
+        except Exception, e:
+            logging.error("Error savin setting to database (%s): %s" % (self.name, str(e)))
+            return
 
         kv.value = value
         kv.save()