def test_single_item(self): tree = {'item': 'A', 'children': []} data = tuple(flaten_tree(tree)) self.assertEqual(data, ('A',))
def render_previous(self, block_render_threshold=_marker): """Render previous blocks. """ if block_render_threshold == _marker: block_render_threshold = RENDER_BLOCKS_PER_REQUEST_THRESHOLD before_uid = self.request.get('before_uid', '') loaded_uids = self.request.get('loaded_blocks[]', []) insert_before = before_uid if before_uid == '' or not loaded_uids: # render_next should be called before render_previous, so there # should already content be loaded. return '{}' data = [] brainlist = flaten_tree(self.tree) previous = [] found = False for brain in brainlist: if brain.UID == before_uid: found = True break previous.insert(0, brain) if not found: return u'{}' while block_render_threshold > 0 and len(previous) > 0: brain = previous.pop(0) if brain.UID in loaded_uids: break block_html = self.render_block(brain) if not block_html: continue data.insert(0, [brain.UID, block_html]) block_render_threshold -= 1 response_data = { 'insert_before': insert_before, 'data': data, 'first_uid': data and data[0][0], 'last_uid': data and data[-1][0] } self.request.response.setHeader('Content-Type', 'application/json') return dumps(response_data)
def render_next(self, block_render_threshold=_marker): """Renders the next blocks. """ if block_render_threshold == _marker: block_render_threshold = RENDER_BLOCKS_PER_REQUEST_THRESHOLD after_uid = self.request.get('after_uid', '') loaded_uids = self.request.get('loaded_blocks[]', []) insert_after = after_uid or 'TOP' data = [] brainlist = flaten_tree(self.tree) found = False for brain in brainlist: if not found and after_uid == '' and \ brain.UID == self.context.UID(): found = True if not found and brain.UID == after_uid: found = True continue elif not found: continue elif found and brain.UID in loaded_uids: # we already have this block loaded break block_html = self.render_block(brain) if not block_html: continue data.append([brain.UID, block_html]) block_render_threshold -= 1 if block_render_threshold == 0: break response_data = { 'insert_after': insert_after, 'data': data, 'first_uid': data and data[0][0], 'last_uid': data and data[-1][0] } self.request.response.setHeader('Content-Type', 'application/json') return dumps(response_data)
def render_previous(self, block_render_threshold=_marker): """Render previous blocks. """ if block_render_threshold == _marker: block_render_threshold = RENDER_BLOCKS_PER_REQUEST_THRESHOLD before_uid = self.request.get('before_uid', '') loaded_uids = self.request.get('loaded_blocks[]', []) insert_before = before_uid if before_uid == '' or not loaded_uids: # render_next should be called before render_previous, so there # should already content be loaded. return '{}' data = [] brainlist = flaten_tree(self.tree) previous = [] found = False for brain in brainlist: if brain.UID == before_uid: found = True break previous.insert(0, brain) if not found: return u'{}' while block_render_threshold > 0 and len(previous) > 0: brain = previous.pop(0) if brain.UID in loaded_uids: break block_html = self.render_block(brain) if not block_html: continue data.insert(0, [brain.UID, block_html]) block_render_threshold -= 1 response_data = {'insert_before': insert_before, 'data': data, 'first_uid': data and data[0][0], 'last_uid': data and data[-1][0]} self.request.response.setHeader('Content-Type', 'application/json') return dumps(response_data)
def render_next(self, block_render_threshold=_marker): """Renders the next blocks. """ if block_render_threshold == _marker: block_render_threshold = RENDER_BLOCKS_PER_REQUEST_THRESHOLD after_uid = self.request.get('after_uid', '') loaded_uids = self.request.get('loaded_blocks[]', []) insert_after = after_uid or 'TOP' data = [] brainlist = flaten_tree(self.tree) found = False for brain in brainlist: if not found and after_uid == '' and \ brain.UID == self.context.UID(): found = True if not found and brain.UID == after_uid: found = True continue elif not found: continue elif found and brain.UID in loaded_uids: # we already have this block loaded break block_html = self.render_block(brain) if not block_html: continue data.append([brain.UID, block_html]) block_render_threshold -= 1 if block_render_threshold == 0: break response_data = {'insert_after': insert_after, 'data': data, 'first_uid': data and data[0][0], 'last_uid': data and data[-1][0]} self.request.response.setHeader('Content-Type', 'application/json') return dumps(response_data)
def test_recursion(self): tree = { 'item': 'A', 'children': [ {'item': 'B', 'children': [ {'item': 'C', 'children': []}, {'item': 'D', 'children': []}]}, {'item': 'E', 'children': []}]} data = tuple(flaten_tree(tree)) self.assertEqual(data, ('A', 'B', 'C', 'D', 'E'))
def test_recursion(self): tree = { 'item': 'A', 'children': [{ 'item': 'B', 'children': [{ 'item': 'C', 'children': [] }, { 'item': 'D', 'children': [] }] }, { 'item': 'E', 'children': [] }] } data = tuple(flaten_tree(tree)) self.assertEqual(data, ('A', 'B', 'C', 'D', 'E'))
def test_single_item(self): tree = {'item': 'A', 'children': []} data = tuple(flaten_tree(tree)) self.assertEqual(data, ('A', ))