svv/items.py
changeset 59 de6abcbd3c03
parent 57 7a48e9d96ec8
equal deleted inserted replaced
58:4f4150296cd3 59:de6abcbd3c03
    11 
    11 
    12 class Item (object) :
    12 class Item (object) :
    13     """
    13     """
    14         Data-mapping for the items table
    14         Data-mapping for the items table
    15     """
    15     """
       
    16 
       
    17     def __init__ (self, name, detail=None, quantity=None, parent=None) :
       
    18 
       
    19         self.name = name
       
    20         self.detail = detail
       
    21         self.quantity = quantity
       
    22         self.parent = parent
    16 
    23 
    17 db.mapper(Item, db.items, properties=dict(
    24 db.mapper(Item, db.items, properties=dict(
    18     # forward ref to parent
    25     # forward ref to parent
    19     parent      = db.relation(Item, remote_side=db.items.c.id, 
    26     parent      = db.relation(Item, remote_side=db.items.c.id, 
    20         backref     = db.backref('children', remote_side=db.items.c.parent_id)
    27         backref     = db.backref('children', remote_side=db.items.c.parent_id)
   545         """
   552         """
   546             Proxy to handler
   553             Proxy to handler
   547         """
   554         """
   548 
   555 
   549         return self.handler.url_for(*args, **kwargs)
   556         return self.handler.url_for(*args, **kwargs)
       
   557     
       
   558     def render_rows (self, items, parent=None) :
       
   559         """
       
   560             Render induvidual rows
       
   561         """
       
   562         
       
   563         for row, item in enumerate(items) :
       
   564             yield html.tr(id=('item-%d' % item.id), class_=('alternate' if row % 2 else None))(
       
   565                 html.td(
       
   566                     html.a(href=self.url_for(InventoryView, fragment=('item-%d' % item.id)))(
       
   567                         u'#%d' % item.id
       
   568                     )
       
   569                 ),
       
   570 
       
   571                 (
       
   572                     html.td(
       
   573                         html.a(href=self.url_for(ItemView, id=item.parent.id))(
       
   574                             item.parent.name
       
   575                         ) if item.parent else None
       
   576                     ),
       
   577                 ) if not parent else None,
       
   578 
       
   579                 html.td(
       
   580                     html.a(href=self.url_for(ItemView, id=item.id))(
       
   581                         item.name
       
   582                     )
       
   583                 ),
       
   584 
       
   585                 html.td(
       
   586                     item.detail
       
   587                 ),
       
   588 
       
   589                 html.td(
       
   590                     "%d kpl" % item.quantity if item.quantity else None
       
   591                 ),
       
   592             )
   550 
   593 
   551     def render (self, items, parent=None) :
   594     def render (self, items, parent=None) :
   552         """
   595         """
   553             Render table for given set of items. If parent is given, those items are assumed to be the children of that
   596             Render table for given set of items. If parent is given, those items are assumed to be the children of that
   554             item.
   597             item.
   555         """
   598         """
   556 
   599 
   557         return html.table(
   600         return html.table(class_='with-borders')(
   558             html.caption(
   601             html.caption(
   559                 u"Kalustolistaus",
   602                 u"Kalustolistaus",
   560                 (
   603                 (
   561                     html.a(href=self.url_for(ItemView, id=parent.id))(
   604                     html.a(href=self.url_for(ItemView, id=parent.id))(
   562                         parent.name
   605                         parent.name
   575                     ) if title
   618                     ) if title
   576                 ),
   619                 ),
   577             ),
   620             ),
   578 
   621 
   579             html.tbody(
   622             html.tbody(
   580                 html.tr(id=('item-%d' % item.id))(
   623                 self.render_rows(items, parent)
   581                     html.td(
       
   582                         html.a(href=self.url_for(InventoryView, fragment=('item-%d' % item.id)))(
       
   583                             u'#%d' % item.id
       
   584                         )
       
   585                     ),
       
   586 
       
   587                     html.td(
       
   588                         html.a(href=self.url_for(ItemView, id=item.parent.id))(
       
   589                             item.parent.name
       
   590                         ) if item.parent else None
       
   591                     ) if not parent else None,
       
   592 
       
   593                     html.td(
       
   594                         html.a(href=self.url_for(ItemView, id=item.id))(
       
   595                             item.name
       
   596                         )
       
   597                     ),
       
   598 
       
   599                     html.td(
       
   600                         item.detail
       
   601                     ),
       
   602 
       
   603                     html.td(
       
   604                         "%d kpl" % item.quantity if item.quantity else None
       
   605                     ),
       
   606                 ) for item in items
       
   607             )
   624             )
   608         )
   625         )
   609 
   626 
   610 
   627 
   611 class InventoryView (PageHandler) :
   628 class InventoryView (PageHandler) :
   621         """
   638         """
   622             Render HTML for full <table> of all items, sorted heirarchially (by parent)
   639             Render HTML for full <table> of all items, sorted heirarchially (by parent)
   623         """
   640         """
   624 
   641 
   625         # listing of inventory items
   642         # listing of inventory items
   626         items = self.session.query(Item).order_by(Item.parent).all()
   643         # group items together by parent; along with the parent item itself
       
   644         # XXX: this only works correctly for top-level parents
       
   645         items = self.session.query(Item).order_by(db.functions.coalesce(Item.parent_id, Item.id), Item.id).all()
   627         
   646         
   628         return ItemTable(self).render(items)
   647         return ItemTable(self).render(items)
   629 
   648 
   630     def render_item_form (self) :
   649     def render_item_form (self) :
   631         """
   650         """