#!/www/python/bin/python
"""$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/toboso/bin/create_db.py $
$Id: create_db.py 25075 2004-09-10 18:27:41Z dbinger $

Create an initial database.
"""

import os
os.environ['SITE'] = 'toboso'

from dulcinea import site_util
from dulcinea.address import Address
from dulcinea.contact import Contact
from toboso.base import open_database

def init_database(db):
    for name, modname, klassname in [
        ('user_db', 'dulcinea.contact', 'ContactDatabase'),
        ('session_manager', 'dulcinea.persistent_session',
         'DulcineaSessionManager'),
        ]:
        db.init_root(name, modname, klassname)
    
def add_test_data(db):
    for (user_id, name, phone, email) in [
        ("boss", "Big Boss", "555-1212", "boss@example.org"),
        ("don", "Don Jones", "333-6515", "don@example.org"),
        ("jane", "Jane Smith", "123-4567", "jane@example.org"),
        ("spot", "Spot the Dog", None, "spot@example.org")]:
        user = Contact(user_id)
        user.set_password(user_id)
        address = Address()
        user.set_address(address)
        first, last = name.split(' ', 1)
        user.set_first_name(first)
        user.set_last_name(last)
        user.set_phone(phone)
        user.set_email(email)
        db.user_db.add_user(user)

    boss = db.user_db['boss']
    db.user_db.get_admin().set_permission('manage-permissions', boss)
    db.user_db.get_admin().set_permission('staff', boss)


if __name__ == "__main__":
    db = open_database(
        'file:' + os.path.join(site_util.get_config_value('var_directory'),
                               'toboso.durus'))
    init_database(db)
    add_test_data(db)
    db.get_connection().commit()
