1. Chris Perkins
  2. moviedemo

Source

moviedemo / moviedemo / websetup.py

"""Setup the moviedemo application"""
import logging
from datetime import datetime

import transaction
from tg import config

from moviedemo.config.environment import load_environment

log = logging.getLogger(__name__)

def setup_app(command, conf, vars):
    """Place any commands to setup moviedemo here"""
    load_environment(conf.global_conf, conf.local_conf)
    # Load the models
    from moviedemo import model
    print "Creating tables"
    model.metadata.create_all(bind=config['pylons.app_globals'].sa_engine)

    u = model.User()
    u.user_name = u'manager'
    u.display_name = u'Example manager'
    u.email_address = u'manager@somedomain.com'
    u.password = u'managepass'

    model.DBSession.add(u)

    g = model.Group()
    g.group_name = u'managers'
    g.display_name = u'Managers Group'

    g.users.append(u)

    model.DBSession.add(g)

    p = model.Permission()
    p.permission_name = u'manage'
    p.description = u'This permission give an administrative right to the bearer'
    p.groups.append(g)

    model.DBSession.add(p)

    u1 = model.User()
    u1.user_name = u'editor'
    u1.display_name = u'Example editor'
    u1.email_address = u'editor@somedomain.com'
    u1.password = u'editpass'
    model.DBSession.add(u1)
    
    genres = ['action', 'animation', 'comedy', 'documentary', 'drama', 'sci-fi']

    for genre in genres:
        model.DBSession.add(model.Genre(name=genre))
    
    movies = [
        {'title': 'Back to the Future',
         'description':"""In 1985, Doc Brown invents time travel; in 1955, Marty McFly accidentally prevents his parents from meeting, putting his own existence at stake""",
         'genre_id': 6,
         'release_date': '4/3/85'
         },
        {'title': 'Fight Club',
         'description':"""An office employee and a soap salesman build a global organization to help vent male aggression.""",
         'genre_id': 1,
         'release_date': '10/14/99'
         },
        {'title': 'Jabberwocky',
         'description':"""After the death of his father the young cooper 'Dennis Cooper' goes to town where he has to pass several adventures...""",
         'genre_id': 1,
         'release_date': '4/15/75'
         },
        {'title': 'The Big Lebowski',
         'description':""""Dude" Lebowski, mistaken for a millionaire Lebowski, seeks restitution for his ruined rug and enlists his bowling buddies to help get it.""",
         'genre_id': 3,
         'release_date': '4/15/75'
         },
    ]
    for movie in movies:
        movie['release_date'] = datetime.strptime(movie['release_date'],'%m/%d/%y')
        model.DBSession.add(model.Movie(**movie))
            
    model.DBSession.flush()

    transaction.commit()
    print "Successfully setup"