Skip to content

Commit

Permalink
Fix bug indexing blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jscott1989 committed Jun 11, 2016
1 parent c727545 commit 8f92758
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/assets/js/edit-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ export const init = () => {
const blocktypes = JSON.parse(elem.dataset['blocktypes']);
const valid_blocktypes = blocktypes.map(({type}) => type);
const UnfilteredContent = JSON.parse(elem.dataset['content']);

const getBlock = (blocks, id) => blocks.filter((b) => b.id == id)[0];

const content = {
blocks: UnfilteredContent.blocks.filter(
(block) => valid_blocktypes.indexOf(block.type) > -1
),
blockLists: UnfilteredContent.blockLists.map((l) =>
l.filter((blockId) => valid_blocktypes.indexOf(UnfilteredContent.blocks[blockId].type) > -1)
l.filter((blockId) => valid_blocktypes.indexOf(getBlock(UnfilteredContent.blocks, blockId).type) > -1)
),
};

Expand Down
3 changes: 2 additions & 1 deletion src/pages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def get_plugin_id(block):
return utils.block_types[block["type"]].__class__.__module__[:-12]

def filter_block_list_item(id):
return plugin_enabled(get_plugin_id(self.content["blocks"][id]))
block = [b for b in self.content["blocks"] if b["id"] == id][0]
return plugin_enabled(get_plugin_id(block))

def map_block_lists(block_list):
return filter(filter_block_list_item, block_list)
Expand Down
2 changes: 1 addition & 1 deletion src/pages/page_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class PastEventsBlockType(BlockType):

def get_past_events(self):
"""Get past events."""
return [e for e in Event.objects.order_by('start') if not e.is_future]
return [e for e in Event.objects.order_by('-start') if not e.is_future]

def render(self, request, data):
"""Return each event in its own block."""
Expand Down
5 changes: 4 additions & 1 deletion src/pages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ def view(request, pk):
"""Show page."""
page = get_object_or_404(Page, url=pk)

def get_block(i):
return [b for b in page.content["blocks"] if b["id"] == i][0]

# We can use .content directly as we're using a dictionary lookup
# So the unused blocks won't be included
blockLists = [[render_block(page.content["blocks"][i], request)
blockLists = [[render_block(get_block(i), request)
for i in l] for l in page.filtered_block_lists]

return render(request, "pages/view.html",
Expand Down

0 comments on commit 8f92758

Please sign in to comment.