def update(type_of, storyid, taskid, direction): """ update the order get the actual position and change, then update the neighboor """ content = access_content(task='r') if type_of == 'story': for item in content['story']: if item['id'] == storyid: log.debug("change position for id %s" % taskid) old_position = item['position'] if direction == 'left': newposition = old_position - 1 else: newposition = old_position + 1 item['position'] = newposition log.debug("update positions - old: %s new: %s" % (old_position, newposition)) break for item in content['story']: if item['position'] == newposition and item['id'] != storyid: item['position'] = old_position content['story'] = order_by_key(content['story'], 'position') access_content(task='w', content=content) else: for item in content['task'][storyid]: if item['id'] == taskid: log.debug("change position for id %s" % taskid) old_position = item['position'] if direction == 'up': newposition = old_position - 1 else: newposition = old_position + 1 item['position'] = newposition log.debug("update positions - old: %s new: %s" % (old_position, newposition)) break for item in content['task'][storyid]: if item['position'] == newposition and item['id'] != taskid: log.debug("also update position for id %s" % item['id']) item['position'] = old_position content['task'][storyid] = order_by_key(content['task'][storyid], 'position') access_content(task='w', content=content) return json.dumps(content)
def items_order(items): """ order the items by position. easy for stories, but loopy for tasks """ items['story'] = order_by_key(items['story'], 'position') # tasks for key, to_be_ordered in items['task'].iteritems(): try: items['task'][key] = order_by_key(to_be_ordered, 'position') except Exception, e: log.error('not able to order the items %s' % e)