~sirn/fanboi2

ref: 6713af3bca6247f57cb9b84ed0856713c56067d8 fanboi2/fanboi2/scripts/create_board.py -rw-r--r-- 1.2 KiB
6713af3bKridsada Thanabulpong Raise minimum body character requirement to 5 characters. 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import optparse
import sys
import transaction
from pyramid.paster import setup_logging, get_appsettings
from sqlalchemy import engine_from_config
from ..models import DBSession, Board


DESCRIPTION = "Insert a new board into the database."
USAGE = "Usage: %prog config arguments"


def main(argv=sys.argv):
    config_uri = argv[1]
    argv = argv[2:]

    parser = optparse.OptionParser(usage=USAGE, description=DESCRIPTION)
    parser.add_option('-t', '--title', dest='title', type='string')
    parser.add_option('-s', '--slug', dest='slug', type='string')

    options, args = parser.parse_args(argv)
    if options.title is None:
        parser.error('You must provide at least --title')

    slug = options.slug
    if slug is None:
        slug = options.title.lower().replace(' ', '_')

    setup_logging(config_uri)
    settings = get_appsettings(config_uri)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    with transaction.manager:
        board = Board(title=options.title, slug=slug)
        DBSession.add(board)
        DBSession.flush()
        print(("Successfully added %s (slug: %s)" %
               (board.title, board.slug)))