-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.py
134 lines (102 loc) · 4.02 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
# based on Dan Jacob's manage.py from newsmeme
import sys
import os
from flask import current_app
from flask.ext.script import Manager, prompt, prompt_pass, \
prompt_bool, prompt_choices
from fossix import create_app
from fossix.models import User, fdb as db
fapp = create_app()
manager = Manager(fapp)
@manager.option('-u', '--username', dest="username", required=False)
@manager.option('-p', '--password', dest="password", required=False)
@manager.option('-e', '--email', dest="email", required=False)
@manager.option('-r', '--role', dest="role", required=False)
def createuser(username=None, password=None, email=None, role=None):
"""
Create a new user
"""
if username is None:
while True:
username = prompt("Username")
user = User.query.filter(User.username==username).first()
if user is not None:
print "Username %s is already taken" % username
else:
break
if email is None:
while True:
email = prompt("Email address")
user = User.query.filter(User.email==email).first()
if user is not None:
print "Email %s is already taken" % email
else:
break
if password is None:
password = prompt_pass("Password")
while True:
password_again = prompt_pass("Password again")
if password != password_again:
print "Passwords do not match"
else:
break
roles = (
(User.MEMBER, "member"),
(User.MODERATOR, "moderator"),
(User.ADMIN, "admin"),
)
if role is None:
role = prompt_choices("Role", roles, resolve=int, default=User.MEMBER)
user = User(username=username,
email=email,
password=password,
role=role)
db.session.add(user)
db.session.commit()
print "User created with ID", user.id
from alembic.config import Config
from alembic import command
@manager.command
def createall():
"Creates database tables"
db.create_all()
alembic_cfg = Config("alembic.ini")
command.stamp(alembic_cfg, "head")
@manager.command
def dropall():
"Drops all database tables"
if prompt_bool("Are you sure ? You will lose all your data !"):
db.drop_all()
@manager.command
def mailall():
"Sends an email to all users"
subject = prompt("Subject")
message = prompt("Message")
from_address = prompt("From", default="[email protected]")
if prompt_bool("Are you sure ? Email will be sent to everyone!"):
with mail.connect() as conn:
for user in User.query:
message = Message(subject=subject,
body=message,
sender=from_address,
recipients=[user.email])
conn.send(message)
import imp
@manager.command
def migratedb():
"SQLAlchemy database migration"
migration = fapp.config['SQLALCHEMY_MIGRATE_REPO'] + '/versions/%03d_migration.py' % (api.db_version(fapp.config['SQLALCHEMY_DATABASE_URI'], fapp.config['SQLALCHEMY_MIGRATE_REPO']) + 1)
tmp_module = imp.new_module('old_model')
old_model = api.create_model(fapp.config['SQLALCHEMY_DATABASE_URI'], fapp.config['SQLALCHEMY_MIGRATE_REPO'])
exec old_model in tmp_module.__dict__
script = api.make_update_script_for_model(fapp.config['SQLALCHEMY_DATABASE_URI'], fapp.config['SQLALCHEMY_MIGRATE_REPO'], tmp_module.meta, db.metadata)
open(migration, "wt").write(script)
a = api.upgrade(fapp.config['SQLALCHEMY_DATABASE_URI'], fapp.config['SQLALCHEMY_MIGRATE_REPO'])
print 'New migration saved as ' + migration
print 'Current database version: ' + str(api.db_version(fapp.config['SQLALCHEMY_DATABASE_URI'], fapp.config['SQLALCHEMY_MIGRATE_REPO']))
manager.add_option('-c', '--config',
dest="config",
required=False,
help="config file")
if __name__ == "__main__":
manager.run()