]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/settings/base.py
OSQA-829, resolves a unicode problem when answering question, use smart_unicode for...
[osqa.git] / forum / settings / base.py
index f640a7c1f850356895d8be9f1b1ec03cd0b50189..cc706bcd49285082824b96862ed04c4c263263ee 100644 (file)
@@ -1,14 +1,19 @@
 import django.dispatch
 import django.dispatch
-from django.utils.encoding import force_unicode
+from django.utils.encoding import force_unicode, smart_unicode
+from datetime import datetime, timedelta
+import logging
+
+TMP_MINICACHE_SECONDS = 5
 
 class SettingSet(list):
 
 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.name = name
         self.title = title
         self.description = description
         self.weight = weight
         self.markdown = markdown
-        
+        self.can_preview = can_preview
+
 
 class BaseSetting(object):
     @classmethod
 
 class BaseSetting(object):
     @classmethod
@@ -23,7 +28,11 @@ class BaseSetting(object):
         self.default = default
         self.field_context = field_context or {}
 
         self.default = default
         self.field_context = field_context or {}
 
+        self._temp = None
+
         if set is not None:
         if set is not None:
+            self.set = set
+            
             if not set.name in Setting.sets:
                 Setting.sets[set.name] = set
 
             if not set.name in Setting.sets:
                 Setting.sets[set.name] = set
 
@@ -33,22 +42,33 @@ class BaseSetting(object):
         return str(self.value)
 
     def __unicode__(self):
         return str(self.value)
 
     def __unicode__(self):
-        return unicode(self.value)
+        return smart_unicode(self.value)
 
     @property
     def value(self):
 
     @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)
         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)
 
     def set_value(self, new_value):
         new_value = self._parse(new_value)
+        self._temp = None
         self.save(new_value)
 
     def save(self, value):
         self.save(new_value)
 
     def save(self, value):
@@ -56,8 +76,11 @@ class BaseSetting(object):
 
         try:
             kv = KeyValue.objects.get(key=self.name)
 
         try:
             kv = KeyValue.objects.get(key=self.name)
-        except:
+        except KeyValue.DoesNotExist:
             kv = KeyValue(key=self.name)
             kv = KeyValue(key=self.name)
+        except Exception, e:
+            logging.error("Error saving setting to database (%s): %s" % (self.name, str(e)))
+            return
 
         kv.value = value
         kv.save()
 
         kv.value = value
         kv.save()
@@ -66,10 +89,16 @@ class BaseSetting(object):
         self.set_value(self.default)
 
     def _parse(self, value):
         self.set_value(self.default)
 
     def _parse(self, value):
-        try:
-            return self.base_type(value)
-        except:
-            return value
+        if not isinstance(value, self.base_type):
+            try:
+                return self.base_type(value)
+            except:
+                pass
+        return value
+
+class AnyTypeSetting(BaseSetting):
+     def _parse(self, value):
+        return value
 
 
 class Setting(object):
 
 
 class Setting(object):
@@ -77,6 +106,9 @@ class Setting(object):
     sets = {}
 
     def __new__(cls, name, default, set=None, field_context=None):
     sets = {}
 
     def __new__(cls, name, default, set=None, field_context=None):
+        if default is None:
+            return AnyTypeSetting(name, default, set, field_context)
+            
         deftype = type(default)
 
         if deftype in Setting.emulators:
         deftype = type(default)
 
         if deftype in Setting.emulators:
@@ -92,5 +124,3 @@ class Setting(object):
             Setting.emulators[deftype] = emul
 
         return emul(name, default, set, field_context)
             Setting.emulators[deftype] = emul
 
         return emul(name, default, set, field_context)
-
-