~sirn/fanboi2

ref: 7789db5cdfdad45cdee835212cfd2c1b07e3089a fanboi2/fanboi2/scripts/board_create.py -rw-r--r-- 1.2 KiB
7789db5cKridsada Thanabulpong Add utility for syncing topic bumped at. 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
39
40
41
42
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):
    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')

    if not argv or len(argv) < 2:
        parser.print_help()
        sys.exit(1)

    config_uri = argv[1]
    argv = argv[2:]

    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)))