This repository has been archived by the owner on Sep 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.py
180 lines (122 loc) · 5.38 KB
/
seed.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""Utility file to seed food db"""
from sqlalchemy import func
from model import Donor, Receiver, Address, Food
import json
from model import connect_to_db, db
from server import app
import bcrypt
# from datetime import datetime
def load_receivers():
"""Load food bank data from csv file"""
print "Receivers"
# Delete all rows in table, so if we need to run this a second time,
# we won't be trying to add duplicate data
Receiver.query.delete()
Address.query.delete()
id_counter = 0
for row in open("seed_data/receivers.csv"):
row = row.rstrip()
rows = row.split(",")
receiver_id = id_counter
name = rows[1]
email = rows[3][1] + "@gmail.com"
password = rows[3][2]
phone_number = rows[4]
address = rows[2]
hashedPassword = bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt(10))
address_obj = Address(formatted_add=address)
receiver = Receiver(receiver_id=receiver_id, name=name, email=email,
password=hashedPassword, phone_number=phone_number)
db.session.add(user)
id_counter += 1
db.session.commit()
print "Done!"
# # Read scraped books.json file and turn json into python dictionary
# json_string = open("seed_data/books_low.json").read()
# books_dict = json.loads(json_string)
# #Read a second file of highly rated books and merge the dictionaries together
# j_string_2 = open("seed_data/books_high.json").read()
# books_high = json.loads(j_string_2)
# books_dict.extend(books_high)
# #create empty dict that will keep track of all the unique genres
# all_genres = {}
# #for each book in our json file, assign title, author, rating, pic_url, summary attributes
# for book in books_dict:
# if book['title'] is not None:
# title = book['title'].strip()
# author = book['author']
# avg_rating = float(book['rating'])
# pic_url = book['pic_url']
# summary = " ".join(book['summary_blocks']).strip()
# #create empty list representing all genres for this book
# genres_for_this_book = []
# #loop through the genres for a particular book
# for genre in book['genres']:
# # if genre doesn't already exist in database:
# if genre not in all_genres:
# #make a new Genre object and add that Genre object to the database
# genre_obj = Genre(name=genre)
# db.session.add(genre_obj)
# #add a string representing the genre and its Genre object to the dict all_genres as a key-value pair
# all_genres[genre] = genre_obj
# #regardless of whether this exists in the database, add the corresponding Genre object to the list of genres for this book
# genres_for_this_book.append(all_genres[genre])
# #after the genre loop is complete, create a Book object and pass in all attributes, including a list of genres for this book
# book_obj = Book(title=title, author=author, avg_rating=avg_rating, pic_url=pic_url, summary=summary, genres=genres_for_this_book)
# db.session.add(book_obj)
# db.session.commit()
# print "Done!"
# def load_users():
# """Load sample users from users.csv into database."""
# print "Users"
# User.query.delete()
# for row in open("seed_data/users.csv"):
# row = row.rstrip()
# rows = row.split("|")
# user_id = rows[0]
# name = rows[1]
# email = rows[2]
# password = rows[3]
# hashedPassword = bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt(10))
# user = User(user_id=user_id, name=name, email=email, password=hashedPassword)
# db.session.add(user)
# db.session.commit()
# print "Done!"
# def load_ratings():
# """Load ratings from reviews.json into database."""
# print "Ratings"
# Rating.query.delete()
# json_string = open("seed_data/reviews_low.json").read()
# ratings_dict = json.loads(json_string)
# j_string_2 = open("seed_data/reviews_high.json").read()
# ratings_high = json.loads(j_string_2)
# ratings_dict.extend(ratings_high)
# for rating in ratings_dict:
# score = rating["score"]
# scraped_title = rating["title"].strip(' > ')
# text = " ".join(rating["text_blocks"]).strip()
# matching_book = Book.query.filter(Book.title == scraped_title).first()
# if matching_book and score:
# new_rating = Rating(book=matching_book, score=score, text=text)
# db.session.add(new_rating)
# db.session.commit()
# print "Done!"
def set_val_user_id():
"""Set value for the next user_id after seeding database"""
# Get the Max user_id in the database
result = db.session.query(func.max(User.user_id)).one()
max_id = int(result[0])
# Set the value for the next user_id to be max_id + 1
query = "SELECT setval('users_user_id_seq', :new_id)"
db.session.execute(query, {'new_id': max_id + 1})
db.session.commit()
if __name__ == "__main__":
connect_to_db(app)
db.drop_all()
# In case tables haven't been created, create them
db.create_all()
# Import different types of data
load_books()
load_users()
load_ratings()
set_val_user_id()