# Path: moonrock/models/site.py
# class Document(Node):
#     __tablename__ = 'document'
#     id = Column(Integer, ForeignKey('node.id'), primary_key=True)
#     title = Column(Text)
# 
#     def __json__(self, request):
#         return dict(
#             id=self.id,
#             title=self.title
#         )
# 
# class Folder(Node):
#     __tablename__ = 'folder'
#     id = Column(Integer, ForeignKey('node.id'), primary_key=True)
#     title = Column(Text)
#     __acl__ = [(Allow, Everyone, 'view'),
#                (Allow, 'group:editors', 'edit')]
# 
#     def __json__(self, request):
#         return dict(
#             id=self.id,
#             title=self.title,
#             resourceType='Folder'
#         )
# 
# Path: moonrock/models/users.py
# class User(BaseObject):
#     __tablename__ = 'users'
# 
#     id = Column(Integer,
#                 Sequence('user_id_seq'),
#                 primary_key=True)
#     userid = Column(String(20), unique=True)
#     username = Column(String(120), unique=True)
#     email = Column(String(120))
#     first_name = Column(String(120))
#     last_name = Column(String(120))
#     twitter = Column(String(120))
#     password = Column(String(120))
#     groups = Column(ArrayType)
# 
#     def __repr__(self):
#         return "<User(id=%d, username='%s')>" % (self.id, self.username)
# 
#     def __json__(self, request):
#         return dict(
#             id=self.id,
#             userid=self.userid,
#             first_name=self.first_name,
#             last_name=self.last_name,
#             twitter=self.twitter,
#             email=self.email
#         )
# 
# USERS = [
#     # Twitter usernames
#     dict(
#         id=1,
#         username='pauleveritt',
#         email='p@x.com',
#         first_name='Firstie',
#         last_name='Lastie',
#         twitter='paulweveritt',
#         password='password',
#         groups=groups
#     ),
#     dict(
#         id=2,
#         username='stormfburg',
#         email='p@x.com',
#         first_name='STORM',
#         last_name='Fburg',
#         twitter='stormfburg',
#         password='password',
#         groups=groups
#     ),
#     dict(
#         id=3,
#         username='chrismcdonough',
#         email='p2@x.com',
#         first_name='Firstie',
#         last_name='Lastie',
#         twitter='chrismcdonough',
#         password='password',
#         groups=groups
#     ),
#     dict(
#         id=4,
#         username='blaflamme',
#         email='p2@x.com',
#         first_name='Firstie',
#         last_name='Lastie',
#         twitter='blaiselaflamme',
#         password='password',
#         groups=groups
#     ),
#     dict(
#         id=5,
#         username='davidemoro',
#         email='p2@x.com',
#         first_name='Firstie',
#         last_name='Lastie',
#         twitter='davidemoro',
#         password='password',
#         groups=groups
#     )
# ]


import os
import sys
import transaction
from pyramid.config import Configurator
from pyramid_sqlalchemy import Session
from pyramid_sqlalchemy.meta import metadata
from pyramid.paster import (
    get_appsettings,
    setup_logging,
)
from ..models.site import (
    Document,
    Folder,
)
from ..models.users import User, USERS




def usage(argv):
    cmd = os.path.basename(argv[0])
    print('usage: %s <config_uri>\n'
          '(example: "%s development.ini")' % (cmd, cmd))
    sys.exit(1)


def main(argv=sys.argv):
    # Usage and configuration
    if len(argv) != 2:
        usage(argv)
    config_uri = argv[1]
    setup_logging(config_uri)
    settings = get_appsettings(config_uri)
    config = Configurator(settings=settings)
    config.include('pyramid_sqlalchemy')

    # Make the database with schema and default data
    with transaction.manager:
        metadata.create_all()
        root = Folder(name='', title='My SQLTraversal Root')
        Session.add(root)
        f1 = root['f1'] = Folder(title='Folder 1')
        f1['da'] = Document(title='Document 1A')


# Path: moonrock/models/site.py
# class Document(Node):
#     __tablename__ = 'document'
#     id = Column(Integer, ForeignKey('node.id'), primary_key=True)
#     title = Column(Text)
# 
#     def __json__(self, request):
#         return dict(
#             id=self.id,
#             title=self.title
#         )
# 
# class Folder(Node):
#     __tablename__ = 'folder'
#     id = Column(Integer, ForeignKey('node.id'), primary_key=True)
#     title = Column(Text)
#     __acl__ = [(Allow, Everyone, 'view'),
#                (Allow, 'group:editors', 'edit')]
# 
#     def __json__(self, request):
#         return dict(
#             id=self.id,
#             title=self.title,
#             resourceType='Folder'
#         )
# 
# Path: moonrock/models/users.py
# class User(BaseObject):
#     __tablename__ = 'users'
# 
#     id = Column(Integer,
#                 Sequence('user_id_seq'),
#                 primary_key=True)
#     userid = Column(String(20), unique=True)
#     username = Column(String(120), unique=True)
#     email = Column(String(120))
#     first_name = Column(String(120))
#     last_name = Column(String(120))
#     twitter = Column(String(120))
#     password = Column(String(120))
#     groups = Column(ArrayType)
# 
#     def __repr__(self):
#         return "<User(id=%d, username='%s')>" % (self.id, self.username)
# 
#     def __json__(self, request):
#         return dict(
#             id=self.id,
#             userid=self.userid,
#             first_name=self.first_name,
#             last_name=self.last_name,
#             twitter=self.twitter,
#             email=self.email
#         )
# 
# USERS = [
#     # Twitter usernames
#     dict(
#         id=1,
#         username='pauleveritt',
#         email='p@x.com',
#         first_name='Firstie',
#         last_name='Lastie',
#         twitter='paulweveritt',
#         password='password',
#         groups=groups
#     ),
#     dict(
#         id=2,
#         username='stormfburg',
#         email='p@x.com',
#         first_name='STORM',
#         last_name='Fburg',
#         twitter='stormfburg',
#         password='password',
#         groups=groups
#     ),
#     dict(
#         id=3,
#         username='chrismcdonough',
#         email='p2@x.com',
#         first_name='Firstie',
#         last_name='Lastie',
#         twitter='chrismcdonough',
#         password='password',
#         groups=groups
#     ),
#     dict(
#         id=4,
#         username='blaflamme',
#         email='p2@x.com',
#         first_name='Firstie',
#         last_name='Lastie',
#         twitter='blaiselaflamme',
#         password='password',
#         groups=groups
#     ),
#     dict(
#         id=5,
#         username='davidemoro',
#         email='p2@x.com',
#         first_name='Firstie',
#         last_name='Lastie',
#         twitter='davidemoro',
#         password='password',
#         groups=groups
#     )
# ]


# Path: moonrock/scripts/initializedb.py
import os
import sys
import transaction
from pyramid.config import Configurator
from pyramid_sqlalchemy import Session
from pyramid_sqlalchemy.meta import metadata
from pyramid.paster import (
    get_appsettings,
    setup_logging,
)
from ..models.site import (
    Document,
    Folder,
)
from ..models.users import User, USERS





def usage(argv):
    cmd = os.path.basename(argv[0])
    print('usage: %s <config_uri>\n'
          '(example: "%s development.ini")' % (cmd, cmd))
    sys.exit(1)


def main(argv=sys.argv):
    # Usage and configuration
    if len(argv) != 2:
        usage(argv)
    config_uri = argv[1]
    setup_logging(config_uri)
    settings = get_appsettings(config_uri)
    config = Configurator(settings=settings)
    config.include('pyramid_sqlalchemy')

    # Make the database with schema and default data
    with transaction.manager:
        metadata.create_all()
        root = Folder(name='', title='My SQLTraversal Root')
        Session.add(root)
        f1 = root['f1'] = Folder(title='Folder 1')
        f1['da'] = Document(title='Document 1A')


        for user in USERS: