-
Notifications
You must be signed in to change notification settings - Fork 1
/
manage.py
executable file
·107 lines (83 loc) · 2.87 KB
/
manage.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Kuulemma
# Copyright (C) 2014, Fast Monkeys Oy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import importlib
from flask import current_app
from flask.ext.failsafe import failsafe
from flask.ext.script import Manager, Server
@failsafe
def create_app():
from kuulemma import Application
return Application()
manager = Manager(create_app)
manager.add_command('runserver', Server(host='localhost'))
@manager.shell
def make_shell_context():
from kuulemma.extensions import db
context = {}
context['app'] = current_app
context['db'] = db
context.update(db.Model._decl_class_registry)
return context
@manager.command
def run(script):
"""Runs script from scripts folder in the application context.
Example: python manage.py run hearing.add_hameentie"""
module, function = script.rsplit('.', 1)
if not module.startswith('scripts.'):
module = 'scripts.%s' % module
module = importlib.import_module(module)
function = getattr(module, function)
function()
@manager.command
def add_hearing(name):
"""Prevents having to add a separate function for each hearing.
Example: python manage.py add_hearing hameentie
"""
script_module = importlib.import_module('scripts.hearing')
hearing_module = importlib.import_module('scripts.hearing.' + name)
getattr(script_module, '_add_hearing')(hearing_module.hearing)
@manager.command
def add_sample_data():
"""
Generates 5 hearings with randomized starting and closing dates.
Adds random number of comments to each hearing.
"""
from kuulemma.extensions import db
from kuulemma.sample_data import get_sample_hearing
print('Start running script.')
for _ in range(5):
db.session.add(get_sample_hearing())
print('Hearing added.')
db.session.commit()
print('Script completed succesfully.')
@manager.command
def add_user():
from kuulemma.extensions import db
from kuulemma.models import User
user = User(
email='[email protected]',
username='john',
password='john1234',
is_admin=True,
is_official=True,
active=True,
)
db.session.add(user)
db.session.commit()
if __name__ == '__main__':
manager.run()