forked from shea256/angular-flask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
66 lines (52 loc) · 1.7 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
import os
import json
import argparse
import requests
from angular_flask.core import db
from angular_flask.models import Post
def create_sample_db_entry(api_endpoint, payload):
url = 'http://localhost:5000/' + api_endpoint
r = requests.post(
url, data=json.dumps(payload),
headers={'Content-Type': 'application/vnd.api+json',
'Accept' : 'application/vnd.api+json'})
print r.text
def create_db():
db.create_all()
def drop_db():
db.drop_all()
def test_db():
url = 'http://localhost:5000/api/post'
r = requests.get(url,
headers={'Content-Type': 'application/vnd.api+json',
'Accept' : 'application/vnd.api+json'})
print r.text
return
def main():
parser = argparse.ArgumentParser(
description='Manage this Flask application.')
parser.add_argument(
'command', help='the name of the command you want to run')
parser.add_argument(
'--seedfile', help='the file with data for seeding the database')
args = parser.parse_args()
if args.command == 'create_db':
create_db()
print "DB created!"
elif args.command == 'delete_db':
drop_db()
print "DB deleted!"
elif args.command == 'seed_db' and args.seedfile:
with open(args.seedfile, 'r') as f:
seed_data = json.loads(f.read())
for item_class in seed_data:
items = seed_data[item_class]
for item in items:
create_sample_db_entry('api/' + item_class, item)
print "\nSample data added to database!"
elif args.command == 'test':
test_db()
else:
raise Exception('Invalid command')
if __name__ == '__main__':
main()