-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
105 lines (85 loc) · 3.83 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
from flask_script import Manager, prompt_bool
from sqlalchemy.exc import SQLAlchemyError
from blagging import app, db
from blagging.models import Post, Author, Tag
manager = Manager(app)
@manager.command
def passwd(user, passwd):
user = db.session.query(Author).filter_by(display_name=user).one()
user.password = passwd
db.session.commit()
print("Password Changed")
@manager.command
def initdb():
try:
db.create_all()
except SQLAlchemyError as e:
db.session.rollback()
print("Error creating database: {}".format(e))
print('Database Initialized')
@manager.command
def dropdb():
if prompt_bool('Are you sure you want to drop the database?'):
try:
db.drop_all()
except SQLAlchemyError as e:
db.session.rollback()
print("Error dropping data: {}".format(e))
print('Database tables dropped')
@manager.command
def create_admin():
admin = Author(display_name="admin", email="[email protected]", password="admin")
placeholder = Post(title="Placeholder", display_title="Placeholder", short_desc="Placeholder", body="Placeholder",
tags="Placeholder",
author=admin)
try:
db.session.add(admin)
db.session.commit()
db.session.add(placeholder)
db.session.commit()
except SQLAlchemyError as e:
db.session.rollback()
print("Error inserting adding: {}".format(e))
print("admin and first entry created")
@manager.command
def test_data():
"""Command to populate database with test data"""
with db.session.no_autoflush:
test_short = 'This is going to be the portion of the post that is displayed to the end user when they first \
log into the app.'
test_body = "Lorem ipsum dolor sit amet, debet gubergren duo at, tamquam veritus verterem mea in, eu cibo iudico \
pri. Postea discere perfecto cum ut. Mel agam melius repudiare at. Eu voluptua nominati vix, mandamus \
inciderint pri in. Accommodare mediocritatem has ex. Mel et hinc facer lobortis, at officiis corrumpit \
consetetur pri, quo no ignota tritani."
androiddrew = Author(display_name='Androiddrew', email='[email protected]', password='test')
lauraurban = Author(display_name='UrbanDecayed', email='[email protected]', password='test')
try:
db.session.add(androiddrew)
db.session.add(lauraurban)
except SQLAlchemyError as e:
db.session.rollback()
print("Error inserting users: {}".format(e))
def add_post(title, short_desc, body, tags):
post = Post(title=title, display_title=title, short_desc=short_desc, body=body, tags=tags,
author=androiddrew)
db.session.add(post)
try:
for name in ['programming', 'flask', 'dirp', 'food']:
db.session.add(Tag(name=name))
db.session.commit()
except SQLAlchemyError as e:
db.session.rollback()
print("Error inserting Tags: {}".format(e))
try:
add_post(title='First post', short_desc=test_short, body=test_body, tags='programming,flask')
add_post(title='Second post', short_desc=test_short, body=test_body, tags='dirp,flask')
add_post(title='Third post', short_desc=test_short, body=test_body, tags='programming,food')
add_post(title='Fourth post', short_desc=test_short, body=test_body, tags='programming,food')
add_post(title='Inactive', short_desc=test_short, body=test_body, tags='dirp,food')
db.session.commit()
except SQLAlchemyError as e:
db.session.rollback()
print("Error inserting posts: {}".format(e))
print('Test data created')
if __name__ == '__main__':
manager.run()