Browse Source

Improvements to DB settings implementation

Rafał Pitoń 11 years ago
parent
commit
ff5535567a
2 changed files with 10 additions and 5 deletions
  1. 1 1
      docs/developers/settings.rst
  2. 9 4
      misago/conf/dbsettings.py

+ 1 - 1
docs/developers/settings.rst

@@ -51,7 +51,7 @@ Each dict in ``settings`` tuple should define following keys:
 * **default_value** - if your setting should always have value, specify there fallabck value used if no user-defined value is available.
 * **is_lazy** - if setting value may too large to be always loaded into memory, you may make setting lazily loaded by defining this key with ``True`` value assigned.
 * **form_field** - What form field should be used to change this setting. Can be either "text", "textarea", "select", "radio", "yesno" or "checkbox". If not defined, "text" is used. "checkbox" should be used exclusively for multiple choices list.
-* **field_extra** - dict that defines extra attributes of form field. For "select", "radio" and "checkbox" fields this dict should contain "choices" key with tuple of tuples that will be used for choices in input. For "string" settings you can define "min_length" and "max_length" extra specifying minmal and maximal lenght of entered text. For integer settings you can specify minimal and maximal range in which value should fall by "min_value" and "max_value". "textarea" field supports extra "rows" setting that controls generated textarea rows attribute. "checkbox" field supports "min" and "max" values that control minimum and maximum required choices.
+* **field_extra** - dict that defines extra attributes of form field. For "select", "radio" and "checkbox" fields this dict should contain "choices" key with tuple of tuples that will be used for choices in input. For "string" settings you can define "min_length" and "max_length" extra specifying minmal and maximal lenght of entered text. For integer settings you can specify minimal and maximal range in which value should fall by "min_value" and "max_value". "textarea" field supports extra "rows" setting that controls generated textarea rows attribute. All text-based fields accept "required" setting. "checkbox" field supports "min" and "max" values that control minimum and maximum required choices.
 
 
 .. note::

+ 9 - 4
misago/conf/dbsettings.py

@@ -25,13 +25,13 @@ class DBSettings(object):
         for setting in Setting.objects.iterator():
             if setting.is_lazy:
                 data[setting.setting] = {
-                    'value': True if setting.value else None,
-                    'is_lazy': setting.is_lazy
+                    'value': bool(setting.value),
+                    'is_lazy': setting.is_lazy,
                 }
             else:
                 data[setting.setting] = {
                     'value': setting.value,
-                    'is_lazy': setting.is_lazy
+                    'is_lazy': setting.is_lazy,
                 }
         return data
 
@@ -40,7 +40,11 @@ class DBSettings(object):
 
         try:
             if self._settings[setting]['is_lazy']:
-                return Setting.objects.get(setting=setting).value
+                if not self._settings[setting].get('real_value'):
+                    real_value = Setting.objects.get(setting=setting).value
+                    self._settings[setting]['real_value'] = real_value
+                else:
+                    return self._settings[setting]['real_value']
             else:
                 raise ValueError("Setting %s is not lazy" % setting)
         except (KeyError, Setting.DoesNotExist):
@@ -60,6 +64,7 @@ class DBSettings(object):
         if not setting in self._overrides:
             self._overrides[setting] = self._settings[setting]['value']
         self._settings[setting]['value'] = new_value
+        self._settings[setting]['real_value'] = new_value
         return new_value
 
     def reset_settings(self):