svv/database.py
author Tero Marttila <terom@fixme.fi>
Thu, 23 Dec 2010 00:57:46 +0200
changeset 9 0327b83959e9
parent 7 bbac4b0f4320
child 25 cfb55708ee03
permissions -rw-r--r--
Implement working NewOrderView, and start restructuring it again right away...
# coding: utf-8

"""
    Database schema
"""

from sqlalchemy import *

# our schema
schema = MetaData()

#
# tables
#

# customers are organizations or natural persons who are serving as the renting side of an order
customers = Table('customers', schema,
        Column('id', Integer, primary_key=True),

        # organization or person name (may match with contact name)
        Column('name', Unicode, nullable=False, unique=True),

        # free-form internal notes
        Column('notes', Unicode),

        # raw CREATE/UPDATE timestamp
        Column('created_at', DateTime, nullable=False, default=func.now()),
        Column('updated_at', DateTime, nullable=True, onupdate=func.current_timestamp()),
)

# contacts are phone/email contact details for named persons in organizations who are active in some order
contacts = Table('contacts', schema,
        Column('id', Integer, primary_key=True),

        # reference to contact being primary for specified customer (organization or themselves)
        Column('customer', None, ForeignKey('customers.id'), nullable=True),

        # person's natural name
        Column('name', Unicode, nullable=True),

        # freeform telephone number
        Column('phone', String, nullable=True),

        # freeform email address
        Column('email', String, nullable=True),

        # raw CREATE/UPDATE timestamp
        Column('created_at', DateTime, nullable=False, default=func.now()),
        Column('updated_at', DateTime, nullable=True, onupdate=func.current_timestamp()),
)

# orders are the transactions that some customer/contact makes, and involve checking out some inventory for some
# duration
orders = Table('orders', schema,
        Column('id', Integer, primary_key=True),

        # ordering customer, if entered (i.e. who to bill)
        Column('customer', None, ForeignKey('customers.id'), nullable=True),

        # active contact at customer side for order (i.e. who to call)
        Column('contact', None, ForeignKey('contacts.id'), nullable=True),

        # short description of customer event where these items are going
        Column('event_name', Unicode, nullable=True),

        # longer description of what the event is
        Column('event_description', Unicode, nullable=True),
        
        # rough duration of event; time interval during which items are reserved
        Column('event_start', DateTime),
        Column('event_end', DateTime),
        
        # raw CREATE/UPDATE timestamp
        Column('created_at', DateTime, nullable=False, default=func.now()),
        Column('updated_at', DateTime, nullable=True, onupdate=func.current_timestamp()),
)

# editable contract terms for customer order
# provision for standard terms, or more specific ones for certain customers
contract_terms = Table('contract_terms', schema,
        Column('id', Integer, primary_key=True),

        # short description
        Column('name', Unicode, nullable=False),

        # full terms in formatted code
        Column('terms', Unicode, nullable=False),
)