forked from Nmargolis/Landlords-Ratings-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.py
123 lines (82 loc) · 3.15 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
"""Utility file to seed ratings database from MovieLens data in seed_data/"""
from model import User, Landlord, Address, Review, Convo, Userconvo, Message, Building, Resource
from model import connect_to_db, db
from server import app
import datetime
def load_users():
"""Load users from u.users into database."""
# print "Users"
# Delete all rows in table to avoid adding duplicates
User.query.delete()
# Read u.user file and insert data
for row in open("data/u.users"):
row = row.rstrip()
user_id, fname, lname, email, password = row.split("|")
user = User(user_id=user_id,
fname=fname,
lname=lname,
email=email,
password=password)
# user = [user_id, fname, lname, email, password]
print user
# Add the user to the database
db.session.add(user)
# Commit all additons to database
db.session.commit()
def load_landlords():
"""Load landlords from u.landlords into database."""
# Delete all rows in table to avoid adding duplicates
Landlord.query.delete()
for row in open("data/u.landlords"):
row = row.rstrip()
landlord_id, fname, lname = row.split("|")
landlord = Landlord(landlord_id=landlord_id,
fname=fname,
lname=lname)
# Add the landlord to the database
db.session.add(landlord)
# Commit all additions to database
db.session.commit()
def load_buildings():
"""Load buildings from u.buildings into database."""
# Delete all rows in table to avoid adding duplicates
Building.query.delete()
for row in open("data/u.buildings"):
row = row.rstrip()
building_id, name = row.split("|")
building = Building(building_id=building_id,
name=name)
# Add the building to the database
db.session.add(building)
# Commit all additions to database
db.session.commit()
def load_addresses():
"""Load addresses from u.addresses into database."""
# Delete all rows in table to avoid adding duplicates
Address.query.delete()
for row in open("data/u.addresses"):
row = row.rstrip()
address_id, street, city, state, zipcode, country, unit, lat, lng, building_id = row.split("|")
address = Address(address_id=address_id,
street=street,
city=city,
state=state,
zipcode=zipcode,
country=country,
unit=unit,
lat=lat,
lng=lng,
building_id=building_id)
# Add the address to the database
db.session.add(address)
# Commit all additions to database
db.session.commit()
if __name__ == "__main__":
connect_to_db(app)
# # In case tables haven't been created, create them
# db.create_all()
# Import different types of data
load_users()
load_landlords()
load_buildings()
load_addresses()