-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.py
67 lines (52 loc) · 2.04 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
import os
from flask.ext.script import Manager
from blog import app
from blog.models import Post
from blog.database import session
manager = Manager(app)
# ----Run the app locally---- #
@manager.command
def run():
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
# ----Add migration management commands---- #
from flask.ext.migrate import Migrate, MigrateCommand
from blog.database import Base
class DB(object):
def __init__(self, metadata):
self.metadata = metadata
migrate = Migrate(app, DB(Base.metadata))
manager.add_command('db', MigrateCommand)
# ----Seed database with some fake blog posts---- #
@manager.command
def seed():
content = """Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."""
for i in range(25):
post = Post(
title="Test Post #{}".format(i),
content=content
)
session.add(post)
session.commit()
# ----Seed database with a fake user---- #
from werkzeug.security import generate_password_hash
from blog.models import User
@manager.command
def adduser():
name = raw_input("Name: ")
email = raw_input("Email: ")
if session.query(User).filter_by(email=email).first():
print "User with that email address already exists"
return
password = ""
password_2 = ""
while not (password and password_2) or password != password_2:
password = raw_input("Password: ")
password_2 = raw_input("Re-enter password: ")
break
user = User(name=name, email=email,
password=generate_password_hash(password))
session.add(user)
session.commit()
if __name__ == "__main__":
manager.run()