-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.py
160 lines (141 loc) · 4.83 KB
/
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
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
import sqlite3
from sqlite3 import Error
from werkzeug.datastructures import ImmutableMultiDict
import user
def init_db():
"""Create User, Furniture, Transaction tables."""
conn = None
try:
conn = sqlite3.connect('sqlite_db')
create_user_query = """CREATE TABLE IF NOT EXISTS User (
email VARCHAR(20) NOT NULL PRIMARY KEY,
password TEXT NOT NULL,
name VARCHAR(20) NOT NULL,
zipcode INTEGER,
rating DOUBLE DEFAULT 0,
transaction_count INTEGER DEFAULT 0,
phone_number VARCHAR(10)
);"""
create_furniture_query = """CREATE TABLE IF NOT EXISTS Furniture (
fid INTEGER PRIMARY KEY AUTOINCREMENT,
owner VARCHAR(20) NOT NULL,
buyer VARCHAR(20),
title VARCHAR(50),
labels TEXT,
status TEXT NOT NULL DEFAULT "init",
image_url TEXT,
description VARCHAR(200),
FOREIGN KEY(owner) REFERENCES User(email),
FOREIGN KEY(buyer) REFERENCES User(email)
);"""
create_transaction_query = """CREATE TABLE IF NOT EXISTS Transactions (
tid INTEGER PRIMARY KEY AUTOINCREMENT,
fid INTEGER NOT NULL,
seller VARCHAR(20) NOT NULL,
buyer VARCHAR(20) NOT NULL,
FOREIGN KEY(fid) REFERENCES Furniture(fid),
FOREIGN KEY(seller) REFERENCES User(email),
FOREIGN KEY(buyer) REFERENCES User(email)
);"""
conn.execute(create_user_query)
conn.execute(create_furniture_query)
conn.execute(create_transaction_query)
print('Database Online, User, furniture and '
'transaction tables created')
except Error as e:
print(e)
finally:
if conn:
conn.close()
def clear():
"""Drop database"""
conn = None
try:
conn = sqlite3.connect('sqlite_db')
conn.execute("DROP Table User")
conn.execute("DROP Table Furniture")
conn.execute("DROP Table Transactions")
print('Database Cleared, dropped User, Furniture, Transaction Tables')
except Error as e:
print(e)
finally:
if conn:
conn.close()
def populate_testing_data():
"""Populate testing data in 3 tables."""
conn = None
try:
conn = sqlite3.connect('sqlite_db')
conn.execute("INSERT INTO User VALUES(?,?,?,?,?,?,?);",
("[email protected]", "hashedpassword", "Bob",
10025, 4.5, 2, "7348829897"))
conn.execute("INSERT INTO Furniture(owner, title, labels,"
"status, image_url, description) VALUES(?,?,?,?,?,?);",
("[email protected]", "Alienware Gaming Monitors", "monitor",
"init", "www.googlecom", "This is a monitor"))
conn.commit()
except Error as e:
print(e)
finally:
if conn:
conn.close()
def insert_mock_user():
try:
mock_form = ImmutableMultiDict([
('email', '[email protected]'),
('password', 'password'),
('name', 'Zhihao Jiang'),
('mobile_phone', '6466466646'),
('zipcode', 10025)])
_, _ = user.register(mock_form)
print(f"mock user inserted {mock_form}")
except Error as e:
print(e)
return
def insert_mock_furniture():
try:
mock_furniture = (1, "[email protected]",
"Lamp", "cheap", "http", "very good")
conn = sqlite3.connect("sqlite_db")
conn.execute(
"INSERT INTO Furniture "
"(fid, owner, title,"
" labels , image_url, description ) "
"VALUES (?, ?, ?, ?, ?, ?)", mock_furniture
)
conn.commit()
print(f"mock user inserted {mock_furniture}")
print(f"mock furniture inserted {mock_furniture}")
except Error as e:
print(e)
return
def insert_mock_buyer():
try:
mock_user = ("[email protected]", "password",
"Buyer_Sause", 10025, 10, 1, "6466466646")
conn = sqlite3.connect("sqlite_db")
conn.execute(
"INSERT INTO User "
"(email, password, name, zipcode,"
" rating, transaction_count, phone_number) "
"VALUES (?, ?, ?, ?, ?, ?, ?)", mock_user
)
conn.commit()
print(f"mock user for buying inserted {mock_user}")
except Error as e:
print(e)
return
def mock_buy(status):
try:
conn = sqlite3.connect("sqlite_db")
cur = conn.cursor()
cur.execute(
"UPDATE Furniture "
"SET buyer = ?, status = ? where fid = 1",
['[email protected]', status]
)
conn.commit()
print("mock buying behavior")
except Error as e:
print(e)
return