def __getattr__(self, name): """This function tries to get the given attribute of the item if it can not be found using the usual way to get attributes. In this case we will split the attribute name by "." and try to get the attribute along the "." separated attribute name.""" data = getattr(self, 'data') if data: # In some cases it is needed to be able to trigger getting the # exapanded value without calling the get_value method. This can # be achieved by accessing the attribute with a special name. # Lets say you want to get the expanded value for `foo`. You get # this by asking for `foo__e_x_p_a_n_d` if name.endswith("__e_x_p_a_n_d"): return self.get_value(name.replace("__e_x_p_a_n_d", ""), expand=True) elif name.find(".") < 0: json_data = json.loads(getattr(self, 'data')) if name in json_data: json_value = json_data[name] # Poor mans data type conversion. if isinstance(json_value, basestring): # Try to convert the value into a date object if it # looks like a date. if re_date.match(json_value): return to_date(json_value) return json_value return get_raw_value(self, name)
def deserialize(value, datatype): """Very simple helper function which returns a python version of the given serialized value.""" if datatype in ["varchar", "text"]: return value elif value in ["", None]: return None elif datatype == "integer": return converters.to_integer(value) elif datatype == "float": return converters.to_float(value) elif datatype == "datetime": # Interval fields are implemented as DATETIME # See http://docs.sqlalchemy.org/en/latest/core/type_basics.html#sqlalchemy.types.Interval # Check if we have a interval here iv = re.compile(u"^\d{1,2}:\d{1,2}:\d{1,2}") if iv.match(value): t = datetime.datetime.strptime(value, "%H:%M:%S") return datetime.timedelta(hours=t.hour, minutes=t.minute, seconds=t.second) # We need the configured timezone to convert the datetime into # the correct timezone. if get_current_registry().settings: timezone = get_current_registry().settings.get("app.timezone") else: timezone = None return converters.to_datetime(value, locale=None, timezone=timezone) elif datatype == "date": return converters.to_date(value) elif re_char_match.match(datatype): # UUID return value elif datatype == "blob": return base64.b64decode(value) elif datatype == "boolean": # In case of imports from a JSON file the value is already of # type boolean. if isinstance(value, bool): return value else: converters.to_boolean(value) else: raise TypeError("{} is not supported".format(datatype))
def _to_python(self, value): from formbar.converters import to_date return to_date(value, self._form._locale)