|
@@ -1,10 +1,14 @@
|
|
|
import sys
|
|
|
+import base64
|
|
|
+try:
|
|
|
+ import cPickle as pickle
|
|
|
+except ImportError:
|
|
|
+ import pickle
|
|
|
|
|
|
-from flask import current_app
|
|
|
from flaskbb.extensions import db, cache
|
|
|
|
|
|
|
|
|
-def convert_to(value, value_type="text"):
|
|
|
+def normalize_to(value, value_type, reverse=False):
|
|
|
"""Converts a value to a specified value type.
|
|
|
Available value types are: string, int, boolean, list.
|
|
|
A boolean type is handled as 0 for false and 1 for true.
|
|
@@ -12,50 +16,39 @@ def convert_to(value, value_type="text"):
|
|
|
|
|
|
:param value: The value which should be converted.
|
|
|
:param value_type: The value_type.
|
|
|
+ :param reverse: If the value should be converted back
|
|
|
"""
|
|
|
- print "converting..."
|
|
|
- if value_type == "text":
|
|
|
- print "text %s" % value
|
|
|
- if isinstance(value, str):
|
|
|
- return value
|
|
|
- else:
|
|
|
- try:
|
|
|
- value = str(value)
|
|
|
- except ValueError:
|
|
|
- raise ValueError("Couldn't convert value to {}"
|
|
|
- .format(value_type))
|
|
|
- return value
|
|
|
-
|
|
|
- elif value_type == "int":
|
|
|
- print "int %s" % value
|
|
|
- try:
|
|
|
- value = int(value)
|
|
|
- except ValueError:
|
|
|
- raise ValueError("Couldn't convert value to {}"
|
|
|
- .format(value_type))
|
|
|
- print type(value)
|
|
|
+ if reverse:
|
|
|
+ if value_type == 'array':
|
|
|
+ return value.split(',')
|
|
|
+ if value_type == 'integer':
|
|
|
+ return int(value)
|
|
|
+ if value_type == 'float':
|
|
|
+ return float(value)
|
|
|
+ if value_type == 'boolean':
|
|
|
+ return value == "1"
|
|
|
+
|
|
|
+ # text
|
|
|
+ return value
|
|
|
+ else:
|
|
|
+ if value_type == 'array':
|
|
|
+ return ",".join(value)
|
|
|
+ if value_type == 'integer':
|
|
|
+ return int(value)
|
|
|
+ if value_type == 'float':
|
|
|
+ return float(value)
|
|
|
+ if value_type == 'boolean':
|
|
|
+ return 1 if value else 0
|
|
|
+
|
|
|
+ # text
|
|
|
return value
|
|
|
-
|
|
|
- elif value_type == "boolean":
|
|
|
- try:
|
|
|
- value = int(value)
|
|
|
-
|
|
|
- if value < 0 or value > 1:
|
|
|
- raise ValueError("Value is not a boolean!. Value must be "
|
|
|
- "between 0 and 1.")
|
|
|
- else:
|
|
|
- return value
|
|
|
-
|
|
|
- except ValueError:
|
|
|
- raise ValueError("Couldn't convert value to {}"
|
|
|
- .format(value_type))
|
|
|
|
|
|
|
|
|
class SettingsGroup(db.Model):
|
|
|
__tablename__ = "settingsgroup"
|
|
|
|
|
|
key = db.Column(db.String, primary_key=True)
|
|
|
- value = db.Column(db.String)
|
|
|
+ name = db.Column(db.String)
|
|
|
description = db.Column(db.String)
|
|
|
|
|
|
|
|
@@ -63,7 +56,7 @@ class Setting(db.Model):
|
|
|
__tablename__ = "settings"
|
|
|
|
|
|
key = db.Column(db.String, primary_key=True)
|
|
|
- value = db.Column(db.String)
|
|
|
+ _value = db.Column("value", db.String)
|
|
|
group = db.Column(db.String, db.ForeignKey('settingsgroup', use_alter=True,
|
|
|
name="fk_settingsgroup"))
|
|
|
|
|
@@ -73,23 +66,47 @@ class Setting(db.Model):
|
|
|
# The description (displayed in the form)
|
|
|
description = db.Column(db.String)
|
|
|
|
|
|
- # Available types: string, int, boolean
|
|
|
+ # Available types: string, integer, boolean, array, float
|
|
|
value_type = db.Column(db.String)
|
|
|
|
|
|
# Available types: text, choice, yesno
|
|
|
input_type = db.Column(db.String)
|
|
|
|
|
|
# Extra attributes like, validation things (min, max length...)
|
|
|
- extra = db.Column(db.String)
|
|
|
+ _extra = db.Column("extra", db.String)
|
|
|
|
|
|
- def save(self):
|
|
|
- db.session.add(self)
|
|
|
- db.session.commit()
|
|
|
+ settings_group = db.relationship("SettingsGroup", lazy="dynamic",
|
|
|
+ backref="setting")
|
|
|
+
|
|
|
+ @property
|
|
|
+ def value(self):
|
|
|
+ return normalize_to(self._value, self.value_type)
|
|
|
+
|
|
|
+ @value.setter
|
|
|
+ def value(self, value):
|
|
|
+ self._value = normalize_to(value, self.value_type, reverse=True)
|
|
|
+
|
|
|
+ @property
|
|
|
+ def extra(self):
|
|
|
+ return pickle.loads(base64.decodestring(self._extra))
|
|
|
+
|
|
|
+ @extra.setter
|
|
|
+ def extra(self, extra):
|
|
|
+ self._extra = base64.encodestring(
|
|
|
+ pickle.dumps((extra), pickle.HIGHEST_PROTOCOL)
|
|
|
+ )
|
|
|
+
|
|
|
+ def get_field(self):
|
|
|
+ pass
|
|
|
|
|
|
@classmethod
|
|
|
@cache.memoize(timeout=sys.maxint)
|
|
|
def get_all(cls):
|
|
|
pass
|
|
|
|
|
|
+ def save(self):
|
|
|
+ db.session.add(self)
|
|
|
+ db.session.commit()
|
|
|
+
|
|
|
def invalidate_cache(self):
|
|
|
cache.delete_memoized(self.get_all, self)
|