svv/items.py
changeset 59 de6abcbd3c03
parent 57 7a48e9d96ec8
--- a/svv/items.py	Thu Jan 20 23:14:07 2011 +0200
+++ b/svv/items.py	Thu Jan 20 23:21:14 2011 +0200
@@ -14,6 +14,13 @@
         Data-mapping for the items table
     """
 
+    def __init__ (self, name, detail=None, quantity=None, parent=None) :
+
+        self.name = name
+        self.detail = detail
+        self.quantity = quantity
+        self.parent = parent
+
 db.mapper(Item, db.items, properties=dict(
     # forward ref to parent
     parent      = db.relation(Item, remote_side=db.items.c.id, 
@@ -547,6 +554,42 @@
         """
 
         return self.handler.url_for(*args, **kwargs)
+    
+    def render_rows (self, items, parent=None) :
+        """
+            Render induvidual rows
+        """
+        
+        for row, item in enumerate(items) :
+            yield html.tr(id=('item-%d' % item.id), class_=('alternate' if row % 2 else None))(
+                html.td(
+                    html.a(href=self.url_for(InventoryView, fragment=('item-%d' % item.id)))(
+                        u'#%d' % item.id
+                    )
+                ),
+
+                (
+                    html.td(
+                        html.a(href=self.url_for(ItemView, id=item.parent.id))(
+                            item.parent.name
+                        ) if item.parent else None
+                    ),
+                ) if not parent else None,
+
+                html.td(
+                    html.a(href=self.url_for(ItemView, id=item.id))(
+                        item.name
+                    )
+                ),
+
+                html.td(
+                    item.detail
+                ),
+
+                html.td(
+                    "%d kpl" % item.quantity if item.quantity else None
+                ),
+            )
 
     def render (self, items, parent=None) :
         """
@@ -554,7 +597,7 @@
             item.
         """
 
-        return html.table(
+        return html.table(class_='with-borders')(
             html.caption(
                 u"Kalustolistaus",
                 (
@@ -577,33 +620,7 @@
             ),
 
             html.tbody(
-                html.tr(id=('item-%d' % item.id))(
-                    html.td(
-                        html.a(href=self.url_for(InventoryView, fragment=('item-%d' % item.id)))(
-                            u'#%d' % item.id
-                        )
-                    ),
-
-                    html.td(
-                        html.a(href=self.url_for(ItemView, id=item.parent.id))(
-                            item.parent.name
-                        ) if item.parent else None
-                    ) if not parent else None,
-
-                    html.td(
-                        html.a(href=self.url_for(ItemView, id=item.id))(
-                            item.name
-                        )
-                    ),
-
-                    html.td(
-                        item.detail
-                    ),
-
-                    html.td(
-                        "%d kpl" % item.quantity if item.quantity else None
-                    ),
-                ) for item in items
+                self.render_rows(items, parent)
             )
         )
 
@@ -623,7 +640,9 @@
         """
 
         # listing of inventory items
-        items = self.session.query(Item).order_by(Item.parent).all()
+        # group items together by parent; along with the parent item itself
+        # XXX: this only works correctly for top-level parents
+        items = self.session.query(Item).order_by(db.functions.coalesce(Item.parent_id, Item.id), Item.id).all()
         
         return ItemTable(self).render(items)