From 8d8db89b8b92bec42861bfffe8a02860df830a89 Mon Sep 17 00:00:00 2001 From: gfawcett Date: Thu, 27 Nov 2008 00:59:41 +0000 Subject: [PATCH] Course.item_tree working, sample display code in course_detail.xhtml git-svn-id: svn://svn.open-ils.org/ILS-Contrib/servres/trunk@58 6d9bc8c9-1ec2-4278-b937-99fde70a366f --- conifer/syrup/models.py | 24 ++++++++++++++++++++---- conifer/templates/course_detail.xhtml | 11 +++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/conifer/syrup/models.py b/conifer/syrup/models.py index 0267c27..cd5cf37 100644 --- a/conifer/syrup/models.py +++ b/conifer/syrup/models.py @@ -115,16 +115,32 @@ class Course(m.Model): return self.code or self.title def items(self): - return Item.objects.filter(course=self.id) + return self.item_set.all() def item_tree(self): """ Return a list, representing a tree of the course items, in display order. Every element of the list is an (Item, [Item]) - tuple, where the second element is the (optional) list of - sub-elements (if a heading) or None (if an item). + tuple, where the second element is a list of sub-elements (if + a heading) or None (if an item). """ - raise NotImplementedError + items = self.items() + # make a node-lookup table + dct = {} + for item in items: + dct.setdefault(item.parent_heading, []).append(item) + for lst in dct.values(): + lst.sort(key=lambda item: item.sort_order) # sort in place + # walk the tree + out = [] + def walk(parent, accum): + here = dct.get(parent, []) + for item in here: + sub = [] + walk(item, sub) + accum.append((item, sub)) + walk(None, out) + return out class Member(m.Model): diff --git a/conifer/templates/course_detail.xhtml b/conifer/templates/course_detail.xhtml index d5242c3..79c4415 100644 --- a/conifer/templates/course_detail.xhtml +++ b/conifer/templates/course_detail.xhtml @@ -25,9 +25,20 @@ items = course.items() ${item.title} ${item.item_type} + ${item.parent_heading} + +

Show-as-tree test

+ +
    +
  1. + ${item} + ${show_tree(subs)} +
  2. +
+ ${show_tree(course.item_tree())} -- 2.11.0