svv/customers.py
author Tero Marttila <terom@fixme.fi>
Mon, 10 Jan 2011 17:51:08 +0200
changeset 53 06dad873204d
parent 14 5b2cc88412f7
permissions -rw-r--r--
items: use DeleteItemForm for ItemView as well
# coding: utf-8
"""
    Customer/contact data model/view/handler
"""

from svv.controllers import PageHandler
from svv.html import tags
from svv import database as db
import svv.urls

class CustomersView (PageHandler) :
    """
        Page for main 'customers' view
    """

    def render_content (self) :
        # database
        conn = self.app.get_connection()

        # customers
        customers = list(conn.execute(db.select([db.customers])))
        contacts = list(conn.execute(db.contacts.join(db.customers).select(use_labels=True)))
        #[
        #    db.contacts.c.id, db.customers.c.id, db.customers.c.name, db.contacts.c.name,
        #    db.contacts.c.phone, db.contacts.c.email,
        #]

        return (
            tags.h1("Asiakkaat"),
            tags.p(u"Tunnen %d asiakasta ja %d yhteyshenkilöä." % (len(customers), len(contacts))),

            tags.table(
                tags.caption(u"Tilaajat"),
                tags.thead(
                    tags.tr(
                        tags.th("ID #"),
                        tags.th("Nimi"),
                    ),
                ),
                tags.tbody(
                    tags.tr(class_=('alternate' if idx % 2 else None))(
                        tags.td(tags.a(href=self.url_for(CustomerView, id=row[db.customers.c.id]))('#%d' % row[db.customers.c.id])),
                        tags.td(tags.a(href=self.url_for(CustomerView, id=row[db.customers.c.id]))(row[db.customers.c.name])),
                    ) for idx, row in enumerate(customers)
                ),
            ),

            tags.table(
                tags.caption(u"Yhteyshenkilöt"),
                tags.thead(
                    tags.tr(
                        tags.th("ID #"),
                        tags.th("Asiakas"),
                        tags.th("Nimi"),
                        tags.th("Puhelinnumero"),
                        tags.th(u"Sähköpostiosoite"),
                    ),
                ),
                tags.tbody(
                    tags.tr(class_=('alternate' if idx % 2 else None))(
                        tags.td(tags.a(href='#')('#%d' % row[db.contacts.c.id])),
                        tags.td(
                            tags.a(href=self.url_for(CustomerView, id=row[db.customers.c.id]))(row[db.customers.c.name])
                                if row[db.customers.c.id] else " "
                        ),
                        tags.td(
                            tags.a(href='#')(row[db.contacts.c.name]),
                        ),
                        tags.td(row[db.contacts.c.phone]),
                        tags.td(row[db.contacts.c.email]),
                    ) for idx, row in enumerate(contacts)
                ),
            ),
        )

class CustomerView (PageHandler) :
    def render_content (self, id) :
        return tags.h1("Info for customer #%d" % id)