forked from jstacoder/flask-angular-blog-example
/
seed_db.py
95 lines (79 loc) · 2.8 KB
/
seed_db.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
# coding: utf-8
import os
from sqlalchemy import create_engine
from phlaskr.models import AppUser,UserProfile,Email,Post,Tag,Comment
from phlaskr.app import application as api
from redis import Redis
print '\n'.join(os.environ.keys())
def convert_uri_to_args(uri):
if uri is None:
return False
trash,remainder = uri.split('://')
login,host_info = remainder.split('@')
user,pw = login.split(':')
host,port = host_info.split(':')
return dict(
host=host,
port=port,
db=0,
password=pw
)
redis_args =\
os.environ.get('REDISCLOUD_URL') and\
convert_uri_to_args(
os.environ.get('REDISCLOUD_URL')
) or {}
cache = Redis(
**redis_args
)
key = os.environ.get('DATABASE_URL')
def start():
AppUser._engine = create_engine(os.environ.get('DATABASE_URL'),echo=True)
AppUser.metadata.bind = AppUser._engine
def seed():
if not cache.get(key):
cache.set(key,1)
content = {
'1':'''
Wow Youll never Guess !!!
OMG!!!!
''',
'2':'''
Sometimes i think about all kinds of things
I just never know where to start
''',
'3':'''
This was soooooo
crazy, Im not sure
if i beleive it myself.
''',
'4':'''
Now for some real magixc
!!!!!!!
'''
}
kyle = AppUser.get_new(username='test',password='test')
email = Email(address='test@test.com',app_user_id=kyle.id,user_type='app')
email.save()
tag = Tag.get_new(name='tag')
post = Post.get_new(title='test post',content='fsfsfwsd',author_id=kyle.id,tags=[tag.id])
post = Post.get_new(title='test post2',content='fsfsfwsd',author_id=kyle.id,tags=[tag.id])
post = Post.get_new(title='test post3',content='fsfsfwsd',author_id=kyle.id,tags=[tag.id])
post = Post.get_new(title='test post4',content='fsfsfwsd',author_id=kyle.id,tags=[tag.id])
joel = AppUser.get_new(username='admin',password=os.environ.get('ADMIN_PASSWORD'))
email = Email(address=os.environ.get('ADMIN_EMAIL'),app_user_id=joel.id,user_type='app')
email.save()
tag = Tag.get_new(name='tags')
post = Post.get_new(title='First test post',content=content['1'],author_id=joel.id,tags=[tag.id])
post = Post.get_new(title='Just my thoughts',content=content['2'],author_id=joel.id,tags=[tag.id])
post = Post.get_new(title='Some crazy stuff',content=content['3'],author_id=joel.id,tags=[tag.id])
post = Post.get_new(title='You Wont Beleive This',content=content['4'],author_id=joel.id,tags=[tag.id])
def reset():
ctx = api.test_request_context()
ctx.push()
start()
AppUser.metadata.create_all(checkfirst=True)
seed()
ctx.pop()
if __name__ == "__main__":
reset()