Permalink
Browse files

Coerce responses into a list of data or IDs instead of the raw implem…

…entation data.
  • Loading branch information...
1 parent 9dc0ad5 commit 25a48242011e96197ecae8a495022b63b110c227 @bretthoerner committed Aug 8, 2011
Showing with 12 additions and 3 deletions.
  1. +12 −3 timak/timelines.py
View
@@ -45,6 +45,12 @@ def _dict_to_list(self, d):
l.sort(key=lambda x: x['timestamp'], reverse=reverse)
return l
+ def _list_to_data(self, l):
+ """
+ Coerces a list of timeline objects into the data the user cares about.
+ """
+ return [o.get('data', None) or o.get('id') for o in l]
+
def _get_obj_and_data(self, key, write_merged=True):
"""
Returns RiakObject with proper vclock set and dictionary of merged entries.
@@ -77,13 +83,16 @@ def _get_obj_and_data(self, key, write_merged=True):
return obj, resolved_data
- def get(self, key):
+ def get(self, key, raw=False):
"""
Returns timeline as list.
"""
# TODO: Optimize this so we don't have to coerce
# list->dict->list for the common case.
- return self._dict_to_list(self._get_obj_and_data(key)[1])
+ result = self._dict_to_list(self._get_obj_and_data(key)[1])
+ if raw:
+ return result
+ return self._list_to_data(result)
def op(self, key, uniq_ident, obj_datetime, obj_data=None, action='add'):
now = self._datetime_to_js(datetime.datetime.utcnow())
@@ -107,7 +116,7 @@ def op(self, key, uniq_ident, obj_datetime, obj_data=None, action='add'):
timeline = self._dict_to_list(data)[:self.max_items]
obj.set_data(timeline)
obj.store()
- return timeline
+ return self._list_to_data(timeline)
def add(self, key, uniq_ident, obj_datetime, obj_data=None):
return self.op(key, uniq_ident, obj_datetime, obj_data=obj_data)

0 comments on commit 25a4824

Please sign in to comment.