-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_interface.py
executable file
·261 lines (215 loc) · 8.62 KB
/
db_interface.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import datetime
from classes import Restaurant, Review
from helpers_fff import average
def add_user(c, full_name, username, password, email, confirm_id, admin):
c.execute('''INSERT INTO Users (full_name, username, password, email, status, admin) VALUES (?, ?, ?, ?, ?, ?)''',
(full_name, username, password, email, confirm_id, admin))
def check_login(c, username, password):
"""
:param c: sqlite cursor
:param username: username to be checked
:param password: password to be checked
:return: "inactive"/True/False
"""
res = c.execute("SELECT * FROM Users WHERE username=? AND password=?", (username, password))
if res.fetchone(): # Username and password correct
res_status = c.execute("SELECT status FROM Users WHERE username=?", (username,))
status = res_status.fetchone()
if status and status[0] != 'active':
return 'inactive'
return True # Login succeeded
return False # Login failed
def check_username_exists(c, username):
"""
:param c: sqlite cursor
:param username: username to be checked
:return: True/False
"""
res = c.execute("SELECT * FROM Users WHERE username=?", (username,))
if res.fetchone():
return True
return False
def check_email_exists(c, email):
"""
:param c: sqlite cursor
:param email: email to be checked
:return: True/False
"""
res = c.execute("SELECT * FROM Users WHERE email=?", (email,))
if res.fetchone():
return True
return False
def confirm(c, user, uuid):
"""
Confirms a user with a uuid. Returns False if user/uuid not found.
:param c: sqlite cursor
:param user: username to be checked
:param uuid: uuid confirmation token
:return: True/False
"""
res = c.execute('SELECT * FROM Users WHERE username=? AND status=?', (user, uuid))
if res.fetchone():
c.execute('UPDATE Users SET status=? WHERE username=?', ('active', user))
return True
return False
def get_restaurants(c):
results = []
c.execute("SELECT * FROM Restaurants LIMIT 1000")
for restaurant in c.fetchall():
r = Restaurant(
id=restaurant[0],
name=restaurant[1],
suburb=restaurant[2],
address=restaurant[3],
postcode=restaurant[4],
phone=restaurant[5],
hours=restaurant[6],
cuisine=restaurant[7],
owner=restaurant[8],
website=restaurant[9],
cost=restaurant[10],
image=restaurant[11],
rating=find_average_rating(c, restaurant[0]),
numRating=find_number_rating(c, restaurant[0])
)
results.append(r)
return results
def search_restaurants(c, criteria="", search_term="", search_term2=""):
"""
Search for restaurants by specified criteria.
Text searches: returns restaurants where search_term in lowercase characters is found in the required field
Number searches (rating, cost etc.): returns restaurants where (search_term <= field <= search_term2)
search_term2 is only used for number searches.
"""
results = []
restaurants = get_restaurants(c)
for r in restaurants:
search_all_fields = [r.get_name(), r.get_cuisine(), r.get_suburb()]
any_search = False
if criteria == "any":
for field in search_all_fields:
if search_term.lower() in field.lower():
any_search = True
break
name_search = criteria == "name" and search_term.lower() in r.get_name().lower()
cuisine_search = criteria == "cuisine" and search_term.lower() in r.get_cuisine().lower()
cost_search = criteria == "cost" and search_term <= r.get_cost() <= search_term2
suburb_search = criteria == "suburb" and search_term.lower() in r.get_suburb().lower()
rating_search = criteria == "rating" and search_term <= r.get_rating() <= search_term2
if any_search or name_search or cuisine_search or cost_search or suburb_search or rating_search:
results.append(r)
return results
def get_restaurant_by_id(c, id):
c.execute("""SELECT * FROM Restaurants WHERE ID=?""", (id,))
res = c.fetchone()
if res:
r = Restaurant(
id=res[0],
name=res[1],
suburb=res[2],
address=res[3],
postcode=res[4],
phone=res[5],
hours=res[6],
cuisine=res[7],
owner=res[8],
website=res[9],
cost=res[10],
image=res[11],
rating=find_average_rating(c, res[0]),
numRating=find_number_rating(c, res[0])
)
return r
# Calculates average rating based on all user ratings for id
def find_average_rating(c, i):
ratings = c.execute("SELECT rating FROM Ratings WHERE restaurant=?", (i,)).fetchall()
ratings = [r[0] for r in ratings]
avg = average(ratings)
if avg == -1:
return 0 # this needs to be a number
return avg
def find_total_average_rating(c):
ratings = c.execute("SELECT rating FROM Ratings").fetchall()
ratings = [r[0] for r in ratings]
avg = average(ratings)
if avg == -1:
return 0 # this needs to be a number
return avg
def find_user_rating(c, restaurant_id, username):
rating = c.execute("SELECT rating FROM Ratings WHERE user = ? AND restaurant=?", (username, restaurant_id))
if rating.fetchone():
return rating
return False
def find_number_rating(c, i):
r = c.execute("SELECT COUNT(*) FROM Ratings WHERE restaurant=?", (i,)).fetchone()
return r[0]
def add_rating(c, restaurant_id, username, rating):
"""
:param c: sqlite cursor
:param restaurant_id: ID of the restaurant being rated
:param username: Username of the user adding the rating
:param rating: Rating (float)
:return: True/False, depending on whether the action succeeded
"""
if check_username_exists(c, username):
c.execute("""INSERT INTO Ratings (user, restaurant, rating) VALUES (?, ?, ?)""",
(username, restaurant_id, rating))
return True
return False
def update_rating(c, restaurant_id, username, rating):
if check_username_exists(c, username):
c.execute("UPDATE Ratings SET rating=? WHERE user = ? AND restaurant=?", (rating, username, restaurant_id))
return True
return False
def already_rated_restaurant(c, restaurant_id, username):
res = c.execute("SELECT * FROM Ratings WHERE restaurant=? AND user=?", (restaurant_id, username))
if res.fetchone():
return True
return False
def already_reviewed_restaurant(c, restaurant_id, username):
res = c.execute("SELECT * FROM Reviews WHERE restaurant=? AND user=?", (restaurant_id, username))
if res.fetchone():
return True
return False
def add_review(c, username, restaurant_id, review_body, timestamp, reported):
if check_username_exists(c, username):
num = c.execute('SELECT MAX(id) FROM Reviews').fetchone()[0]
c.execute('INSERT INTO Reviews(id, user, restaurant, review, timestamp, reported) '
'VALUES (?, ?, ?, ?, ?, ?)', (num + 1, username, restaurant_id, review_body, timestamp, reported))
return True
return False
def get_reviews(c, restaurant_id):
results = []
c.execute("SELECT * FROM Reviews WHERE restaurant=?", (restaurant_id,))
for review in c.fetchall():
r = Review(
id=review[0],
user=review[1],
review=review[3],
timestamp=datetime.datetime.strptime(review[4], "%Y-%m-%d %H:%M:%S.%f"),
reported=review[5]
)
results.append(r)
return results
def get_reported(c):
results = []
c.execute("SELECT * FROM Reviews WHERE reported=1")
for review in c.fetchall():
r = Review(
id=review[0],
user=review[1],
review=review[3],
timestamp=datetime.datetime.strptime(review[4], "%Y-%m-%d %H:%M:%S.%f"),
reported=review[5]
)
results.append(r)
return results
def get_admin_stats(c):
result = {}
result['restaurant_count'] = c.execute("SELECT COUNT(*) FROM Restaurants").fetchone()[0]
result['user_count'] = c.execute("SELECT COUNT(*) FROM Users").fetchone()[0]
result['review_count'] = c.execute("SELECT COUNT(*) FROM Reviews").fetchone()[0]
result['rating_count'] = c.execute("SELECT COUNT(*) FROM Ratings").fetchone()[0]
result['average_restaurant_rating'] = find_total_average_rating(c)
result['reported_reviews_count'] = c.execute("SELECT COUNT(*) FROM Reviews WHERE reported=1").fetchone()[0]
return result