svv/customers.py
author Tero Marttila <terom@fixme.fi>
Thu, 23 Dec 2010 01:07:42 +0200
changeset 10 4bdb45071c89
parent 9 0327b83959e9
child 13 cb18f86e38d9
permissions -rw-r--r--
Fix up PageHandler.render to PageHandler.render_content, add PageHandler.process, and fix up orders
# 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.tr(
                    tags.th("ID #"),
                    tags.th("Nimi"),
                ),
                (tags.tr(
                    tags.td(tags.a(href=self.url_for(CustomerView, id=customer[db.customers.c.id]))('#%d' % customer[db.customers.c.id])),
                    tags.td(tags.a(href=self.url_for(CustomerView, id=customer[db.customers.c.id]))(customer[db.customers.c.name])),
                ) for customer in customers)
            ),

            tags.table(
                tags.tr(
                    tags.th("ID #"),
                    tags.th("Asiakas"),
                    tags.th("Nimi"),
                    tags.th("Puhelinnumero"),
                    tags.th(u"Sähköpostiosoite"),
                ),
                (tags.tr(
                    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 row in contacts)
            ),
        )

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