/
mockseed.py
163 lines (110 loc) · 4.31 KB
/
mockseed.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""Utility file to seed Apptivist database from Mockaroo CSV's"""
from model import connect_to_db, db
from model import Article, Tag, User, Action, article_tags
from server import app
import datetime
def load_mock_users():
"""Load users from users-50-topcities.csv into database.
Working. """
print "Loading Mock Users..."
for i, row in enumerate(open("seed_data/mockaroo/users-50-topcities.csv")):
row = row.rstrip().split(",")
user_id_str, name, email, password, zipcode = row
user_id = int(user_id_str)
user = User(user_id=user_id,
name=name,
email=email,
password=password,
zipcode=zipcode)
db.session.add(user)
db.session.commit()
print "Mock users table loaded."
###############################################################################
### Article Loads ###
def load_mock_articles():
"""This loads the mockaroo articles.
Working."""
print "Mockaroo Articles Loading..."
for i, row in enumerate(open("seed_data/mockaroo/articles-100.csv")):
row = row.rstrip().split(",")
article_id_str, title, url, img_src, date_str, user_id_str = row
article_id = int(article_id_str)
user_id = int(user_id_str)
date = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
article = Article(article_id=article_id,
title=title,
url=url,
img_src=img_src,
date=date,
user_id=user_id,
tag_list=[])
db.session.add(article)
db.session.commit()
print "Mockaroo Articles table loaded."
###############################################################################
### Tag Loads ###
def load_tags():
"""Loads the few articletag connections I made already.
Good for experimenting!
Working."""
print "Tags Loading..."
for i, row in enumerate(open("seed_data/tagnames.csv")):
row = row.rstrip().split(",")
tag_id_str, tag_name, meetup_topic, gg_code = row
tag_id = int(tag_id_str)
tag = Tag(tag_id=tag_id,
tag_name=tag_name,
meetup_topic=meetup_topic,
gg_code=gg_code
)
db.session.add(tag)
db.session.commit()
print "Tags table loaded."
def load_mock_article_tags():
"""This loads mocked articletag associations.
Not working. """
print "Mock ArticleTags Loading..."
for i, row in enumerate(open("seed_data/mockaroo/articletags.csv")):
row = row.rstrip().split(",")
article_id_str, tag_id_str = row
article_id = int(article_id_str)
tag_id = int(tag_id_str)
article = Article.query.get(article_id)
tag = Tag.query.get(tag_id)
if article.tag_list:
if tag not in article.tag_list:
article.tag_list.append(tag)
db.session.commit()
else:
article.tag_list = []
article.tag_list.append(tag)
db.session.commit()
print "Mock ArticleTags table loaded."
###############################################################################
### Action Items to Load ###
def load_mock_actions():
"""This loads mocked action items"""
print "Mockaroo Actions Loading..."
for i, row in enumerate(open("seed_data/mockaroo/actionitems.csv")):
row = row.rstrip().split(",")
action_id_str, tag_id_str, article_id_str, action_user_str, action_type = row
action_id = int(action_id_str)
tag_id = int(tag_id_str)
article_id = int(article_id_str)
action_user = int(action_user_str)
action = Action(
action_id=action_id,
tag_id=tag_id,
article_id=article_id,
action_user=action_user,
action_type=action_type
)
db.session.add(action)
db.session.commit()
print "Mocked actions table loaded."
##############################################################################
### Helper Functions ###
if __name__ == "__main__":
connect_to_db(app)
print "Connected to DB."
# db.create_all()