-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
142 lines (101 loc) · 4.3 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
131
132
133
134
135
136
137
138
139
140
141
142
from flask.ext.script import Manager
import os
import datetime
import bcrypt
from flask.ext.security.utils import encrypt_password
from flask.ext.migrate import Migrate, MigrateCommand
from blog import app, db
from blog.users.models import user_datastore
manager = Manager(app)
manager.add_command('db', MigrateCommand)
@manager.command
def add_admin(email, password):
"""Add an admin user to your database"""
user = user_datastore.create_user(email=email,
password=encrypt_password(password))
admin_role = user_datastore.find_or_create_role("admin")
user_datastore.add_role_to_user(user, admin_role)
user.confirmed_at = datetime.datetime.utcnow()
db.session.commit()
print "Created admin user: %s" % (user, )
@manager.command
def init(name, test=False, indent=""):
"""Initialize and rename a flask-blog project"""
print "{0}Initializing flask-blog project with name '{1}'".format(
indent, name)
module_name = "_".join(name.split()).lower()
print "{0}Python main module will be: {1}".format(indent, module_name)
module_files = ["manage.py", "dev.py", "shell.py", "blog/config.py",
"Procfile"]
for filename in module_files:
print "{0}Updating module name in '{1}'".format(indent, filename)
if not test:
with open(filename) as f:
lines = [l.replace("blog", module_name) for l in f.readlines()]
with open(filename, 'w') as f:
f.writelines(lines)
print '{0}Generating salts and secret keys'.format(indent)
with open("blog/config.py") as f:
lines = f.readlines()
if not test:
with open("blog/config.py", "w") as f:
for line in lines:
if "REPLACE_WITH_RANDOM" in line:
line = line.replace("REPLACE_WITH_RANDOM", bcrypt.gensalt())
f.write(line)
print "{0}Renaming 'blog' module to '{1}'".format(indent, module_name)
if not test:
os.rename("blog", module_name)
print "{0}Finished initializing project".format(indent)
@manager.command
def wizard(test=False):
"""New project wizard to go through all required startup steps"""
print 'Starting the flask-blog wizard'
indent = ' ' * 4
default_name = "blog"
print '\n\nProject name:'
name = raw_input("> Please enter a name for your project [{0}]: ".format(
default_name))
name = name or default_name
init(name, test=test, indent=indent)
if test:
name = default_name
config_file = "{0}/config.py".format(name)
lines = []
database_string = ""
with open(config_file) as f:
for line in f:
if line.strip().startswith("SQLALCHEMY_DATABASE_URI"):
parts = line.split("=", 1)
uri = parts[1].strip()
print '\n\nDatabase configuration:'
print '*** NB Please ensure your database has been created'
print ('Database string must be a valid python expression that'
' will be understood by SQLAlchemy. Please include '
'surrounding quotes if this is just a static string')
database_string = raw_input("> Please enter your database "
"connection string [{0}]: ".format(uri))
database_string = database_string or uri
parts[1] = " {0}\n".format(database_string)
parts.insert(1, "=")
lines.append("".join(parts))
else:
lines.append(line)
print "{0}Writing database connection string: {1}".format(indent,
database_string)
if not test:
with open(config_file, "w") as f:
f.writelines(lines)
print '{0}Initializing Alembic migrations'.format(indent)
os.system("python manage.py migrate init")
print '{0}Creating first revision'.format(indent)
os.system('python manage.py migrate revision --autogenerate -m '
'"Initial database migration"')
print "{0}Upgrading database to head".format(indent)
os.system("python manage.py migrate upgrade head")
print "\n\nCreate admin user"
email = raw_input("> Please enter an admin email address: ")
password = raw_input("> Please enter an admin password: ")
add_admin(email, password)
if __name__ == "__main__":
manager.run()