-
Notifications
You must be signed in to change notification settings - Fork 1
/
manage.py
executable file
·130 lines (106 loc) · 2.91 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
124
125
126
127
128
129
130
#!venv/bin/python
# -*- coding: utf-8 -*-
"""
manage
~~~~~~
Management utilities for commit --blog
"""
from flask_script import Manager
from commitblog import create_app
try:
from dotenv import load_dotenv
load_dotenv(verbose=True)
except ImportError:
from sys import stderr
print('python-dotenv not installed, not trying to load .env', file=stderr)
pass
manager = Manager(create_app)
@manager.command
def init_db():
from commitblog import db
db.create_all()
@manager.command
def reinit_db():
from commitblog import db
db.drop_all()
db.create_all()
@manager.command
def make_admin(username):
from commitblog import db, Blogger
hopeful = Blogger.query.filter(Blogger.username == username).first()
assert hopeful is not None, f'no blogger found for username "{username}"'
hopeful.admin = True
db.session.add(hopeful)
db.session.commit()
@manager.command
def register_cli_client():
from time import time
from commitblog import db
from models.auth import OAuth2Client
client = OAuth2Client(
client_id='commit--cli',
client_id_issued_at=int(time()),
client_secret = '')
client.set_client_metadata({
'client_name': 'commit--blog cli',
'client_uri': 'https://commit--blog.com/cli',
'grant_types': ['authorization_code'],
'redirect_uris': ['http://localhost:33205/oauth/authorized'],
'response_types': ['code'],
'scope': 'blog',
'token_endpoint_auth_method': 'none',
})
db.session.add(client)
db.session.commit()
@manager.command
def migrate(q):
from commitblog import db
from sqlalchemy.sql import text
migs = {}
migs['q1'] = text("""
alter table commit_post
add column markdown_renderer varchar null
""")
migs['q2'] = text("""
alter table blogger
add column admin boolean not null default false
check (admin in (0, 1))
""")
migs['q3'] = text("""
alter table blogger
add column gh_email_choice boolean check
(gh_email_choice in (0, 1))
""")
query = migs[q]
db.engine.execute(query.execution_options(autocommit=True))
@manager.command
def run_tasks():
import tasks
tasks.run()
@manager.command
def test_email():
import tasks
from models import Task
tasks.email(Task(details={
'message': 'hello',
'recipient': 'uniphil+recip@gmail.com',
'variables': {'value': 42},
}))
@manager.command
def grunserver():
"""run locally in a prod-ish way with gunicorn"""
import subprocess
try:
subprocess.run([
'gunicorn',
'wsgi:app',
'--no-sendfile',
'--workers=2',
'--worker-class=gevent',
'--bind=0.0.0.0:5000',
'--reload',
])
except KeyboardInterrupt:
pass
if __name__ == '__main__':
manager.run()