From: Tomas Babej Date: Fri, 6 Feb 2015 19:33:47 +0000 (+0100) Subject: SerializingObject: Add normalizers for normalizing user input X-Git-Url: https://git.madduck.net/etc/taskwarrior.git/commitdiff_plain/5421d65128402657771440f1f667631d56d44fe8?ds=sidebyside SerializingObject: Add normalizers for normalizing user input --- diff --git a/tasklib/task.py b/tasklib/task.py index a273815..3097f4c 100644 --- a/tasklib/task.py +++ b/tasklib/task.py @@ -53,6 +53,18 @@ class SerializingObject(object): lambda x: x if x is not None else '') return dehydrate_func(value) + def _normalize(self, key, value): + """ + Use normalize_ methods to normalize user input. Any user + input will be normalized at the moment it is used as filter, + or entered as a value of Task attribute. + """ + + normalize_func = getattr(self, 'normalize_{0}'.format(key), + lambda x: x) + + return normalize_func(value) + def timestamp_serializer(self, date): if not date: return '' @@ -309,8 +321,9 @@ class Task(TaskResource): # __init__ methods, that would be confusing # Rather unfortunate syntax due to python2.6 comaptiblity - self._load_data(dict((key, self._serialize(key, value)) - for (key, value) in six.iteritems(kwargs))) + self._data = dict((key, self._normalize(key, value)) + for (key, value) in six.iteritems(kwargs)) + self._original_data = copy.deepcopy(self._data) def __unicode__(self): return self['description']