-
Notifications
You must be signed in to change notification settings - Fork 84
/
manage.py
executable file
·123 lines (101 loc) · 3.18 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
import os
import binascii
import unittest
from flask_assets import ManageAssets
from flask_rq import get_worker
from flask_script import Manager, Server, Command
from flask_script.commands import ShowUrls, Clean
from flask_migrate import Migrate, MigrateCommand
from server import create_app, generate
from server.models import db, User, Course, Version
from server.extensions import assets_env, cache
# default to dev config
env = os.getenv('OK_ENV', 'dev')
app = create_app(env)
migrate = Migrate(app, db)
manager = Manager(app)
class RunTests(Command):
def run(self):
test_loader = unittest.defaultTestLoader
test_runner = unittest.TextTestRunner()
test_suite = test_loader.discover('tests/')
test_runner.run(test_suite)
manager.add_command("server", Server(host='localhost'))
manager.add_command("show-urls", ShowUrls())
manager.add_command("clean", Clean())
manager.add_command('db', MigrateCommand)
manager.add_command('test', RunTests())
manager.add_command("assets", ManageAssets(assets_env))
@manager.shell
def make_shell_context():
""" Creates a python REPL with several default imports
in the context of the app
"""
return dict(app=app, db=db, User=User)
@manager.command
def seed():
generate.seed()
@manager.command
def cacheflush():
with app.app_context():
cache.clear()
print("Flushed")
@manager.command
def setup_default():
admin = User(email="[email protected]", is_admin=True)
db.session.add(admin)
admin = User(email="[email protected]", is_admin=True)
db.session.add(admin)
db.session.commit()
course = Course(
offering='cal/cs61a/sp16',
institution='UC Berkeley',
display_name='CS 61A',
active=True
)
db.session.add(course)
url = 'https://github.com/okpy/ok-client/releases/download/v1.5.5/ok'
ok = Version(name='ok-client', current_version='v1.5.4', download_link=url)
db.session.add(ok)
db.session.commit()
@manager.command
def createdb():
""" Creates a database with all of the tables defined in
your SQLAlchemy models
"""
db.create_all()
setup_default()
@manager.command
def dropdb():
""" Creates a database with all of the tables defined in
your SQLAlchemy models
"""
if app.config['ENV'] != "prod":
db.drop_all()
@manager.command
def resetdb():
""" Drop & create a database with all of the tables defined in
your SQLAlchemy models.
DO NOT USE IN PRODUCTION.
"""
if app.config['ENV'] != "prod":
print("Dropping database...")
db.drop_all()
print("Seeding database...")
createdb()
seed()
@manager.command
def generate_session_key():
""" Helper for admins to generate random 24 character string. Used as the
secret key for sessions. Must be consistent (and secret) per environment.
Output: b'cd8c2471.................2416c0e030d09'
Copy the value in between the quotation marks to the settings file
"""
return binascii.hexlify(os.urandom(24))
@manager.command
def worker():
""" Run RQ workers. """
get_worker().work()
if __name__ == "__main__":
manager.run()