# import datetime
# from sqlalchemy import *
# from sqlalchemy.orm import *
# from {{package}}.model import Session, metadata
# 
# meta = metadata
# 
# #
# # Domain objects
# #
# 
# class User(object):
#     def __init__(
#             self, username, password=None, 
#             email=None, fullname=None,
#             nickname = None, dob = None, gender = None, postcode = None,
#             country = None, language = None, timezone = None,
#             group_uid=None,  active=None, session=None, verify=None, 
#             passurl=None,):
# 
#         self.username = username
#         self.verify = verify
#         self.password = password
#         self.email=email
#     
#         self.fullname=fullname
#         self.nickname =nickname
#         self.dob = dob
#         self.gender = gender
#         self.postcode = postcode
#         self.country = country
#         self.language = language
#         self.timezone = timezone
#         
#         self.active = active
#         self.group_uid = group_uid
#         self.session = session
#         self.passurl = passurl
# 
# class App(object):
#     def __init__(self, name=None):
#         self.name = name
#         
# class Role(object):
#     def __init__(self, name=None):
#         self.name = name
#         
# class Group(object):
#     def __init__(self, name=None):
#         self.name = name
#     
# class Email(object):
#     pass
#       
# class Permission(object):
#     def __init__(self, user_uid=None, 
#                  app_uid=None, role_uid=None):
#         self.user_uid = user_uid
#         self.app_uid = app_uid
#         self.role_uid = role_uid
# 
# class History(object):
#     def __init__(self, username, signed_in=None, 
#                  last_accessed=None, signed_out=None):
#         self.username = username
#         self.signed_in = signed_in
#         self.last_accessed = last_accessed
#         self.signed_out = signed_out
# 
# class Site(object):
#     def __init__(self, trustroot=None):
#         self.trustroot = trustroot
#         
# class Persona(object):
#     def __init__(self, name=None):
#         self.name = name
#         
# #
# # Functions (used in column defaults)
# #
# 
# def now():
#     return datetime.datetime.now()
# 
# # 
# # Tables
# #
# 
# 
# groups_table = Table('groups',  meta,
#     Column("uid", Integer, Sequence('groups_uid_seq', optional=True), primary_key=True,),
#     Column('name', String(255), unique=True, nullable=False),)
# 
# users_table = Table('users', meta,
#     Column("uid", Integer, Sequence('users_uid_seq', optional=True), primary_key=True,),
#     Column('username', String(255), unique=True,  nullable=False),
#     Column('password', String(255)),
#     #Column('firstname', String(255)),
#     #Column('surname', String(255)),
#     
#     Column('email', String(255)),
#     Column('nickname', String(255)),
#     Column('fullname', String(255)),
#     Column('dob', DateTime(timezone=False)),
#     Column('gender', String(1)),
#     Column('postcode', String(255)),
#     Column('country', String(255)),
#     Column('language', String(255)),
#     Column('timezone', String(255)),
# 
#     Column('active', Boolean()),
#     Column('sleep', DateTime(timezone=False)),
#     Column('registered', DateTime(timezone=False), nullable=False, default=now),
#     Column('session', Integer, nullable=False, default=30),
#     Column('passurl', String(1023)),
#     Column('group_uid', Integer, ForeignKey("groups.uid"), nullable=False, default=1,),)
# 
# emails_table = Table('emails',  meta,
#     Column("uid", Integer, Sequence('emails_uid_seq', optional=True), primary_key=True,),
#     Column('user_uid', String(255), ForeignKey("users.uid"), nullable=False,),
#     Column('email', String(255), nullable=False),
#     Column('verified', DateTime(timezone=False)),
#     Column('code', String(255)),)
# 
# sites_table = Table('sites', meta,
#     Column("uid", Integer, Sequence('sites_uid_seq', optional=True), primary_key=True,),  
#     Column('user_uid', String(255), ForeignKey("users.uid"), nullable=False,),
#     Column('trustroot', String(255), nullable=False),)
# 
# apps_table = Table('apps',  meta,
#     Column("uid", Integer, Sequence('apps_uid_seq', optional=True), primary_key=True,),
#     Column('name', String(255), unique=True, nullable=False),)
# 
# roles_table = Table('roles',  meta,
#     Column("uid", Integer, Sequence('roles_uid_seq', optional=True), primary_key=True,),
#     Column('name', String(255), unique=True, nullable=False),)
# 
# permissions_table = Table('permissions', meta,
#     Column("uid", Integer, Sequence('permissions_uid_seq', optional=True), primary_key=True,),
#     Column('user_uid', Integer, ForeignKey("users.uid"),  nullable=False),
#     Column('app_uid', Integer, ForeignKey("apps.uid")),
#     Column('role_uid', Integer, ForeignKey("roles.uid")),)
# 
# histories_table = Table('histories', meta,
#     Column("uid", Integer, Sequence('histories_uid_seq', optional=True), primary_key=True,),
#     Column('user_uid', Integer, ForeignKey("users.uid"), nullable=False),
#     Column('signed_in', DateTime(timezone=False), nullable=False, default=now),
#     Column('last_accessed', DateTime(timezone=False)),
#     Column('signed_out', DateTime(timezone=False)),
# )
# 
# #
# # Mappers
# #
# 
# permission_mapper = mapper(Permission, permissions_table)
# 
# app_mapper = mapper(App, apps_table,
#     properties = {'permissions' : relation(permission_mapper, # private=True
#         ),},)
# 
# role_mapper = mapper(Role, roles_table, 
#     properties = {'permissions' : relation(permission_mapper, # private=True
#         ),},)
# 
# group_mapper = mapper(Group, groups_table)
# 
# site_mapper = mapper(Site, sites_table)
# 
# history_mapper = mapper(History, histories_table)
# 
# email_mapper = mapper(Email, emails_table)
# 
# user_mapper = mapper(User, users_table,
#     properties = {
#         'histories':relation(history_mapper, backref="user"),
#         'sites':relation(site_mapper, backref="user"),
#         'current_sessions': relation(
#             history_mapper, 
#             primaryjoin= and_(
#                 users_table.c.uid==histories_table.c.user_uid, 
#                 histories_table.c.signed_out==None,), 
#             lazy=False),
#         'permissions' : relation(permission_mapper, # private=True
#         ),
#         'emails' : relation(email_mapper, backref="user"),
#     }
# )
