forked from UlyssesU/rateMyLandlord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrollers.py
310 lines (254 loc) · 11 KB
/
controllers.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""
This file defines actions, i.e. functions the URLs are mapped into
The @action(path) decorator exposed the function at URL:
http://127.0.0.1:8000/{app_name}/{path}
If app_name == '_default' then simply
http://127.0.0.1:8000/{path}
If path == 'index' it can be omitted:
http://127.0.0.1:8000/
The path follows the bottlepy syntax.
@action.uses('generic.html') indicates that the action uses the generic.html template
@action.uses(session) indicates that the action uses the session
@action.uses(db) indicates that the action uses the db
@action.uses(T) indicates that the action uses the i18n & pluralization
@action.uses(auth.user) indicates that the action requires a logged in user
@action.uses(auth) indicates that the action requires the auth object
session, db, T, auth, and tempates are examples of Fixtures.
Warning: Fixtures MUST be declared with @action.uses({fixtures}) else your app will result in undefined behavior
"""
from pydal.validators import IS_IN_SET
from py4web import action, request, abort, redirect, URL, Field
from yatl.helpers import A
from py4web.utils.auth import Auth
from .common import db, session, T, cache, auth, logger, authenticated, unauthenticated, flash
from py4web.utils.form import Form, FormStyleBulma
from py4web.utils.url_signer import URLSigner
from .models import get_user_email, get_user, get_username
import random
url_signer = URLSigner(session)
lord_id = 0
@action('index', method=["GET", "POST"])
@action.uses(db, auth, 'index.html')
def index():
landlord_list = db(db.landlord.id).select().as_list()
id_list = []
for r in landlord_list:
# check to see if the landlord has no reviews
# if so don't include them in the display
if db((db.reviews.reviews_landlordID == r['id'])).select().first() is not None:
id_list.append(r['id'])
if len(id_list) > 1:
random_landlords = random.sample(id_list, 2)
else: # if there is only one landlord populate page with the only landlord twice
random_landlords = [1, 1]
print(random_landlords)
example_landlord1 = db.landlord[random_landlords[0]]
print(example_landlord1)
example_landlord1_name = example_landlord1.first_name + " " + example_landlord1.last_name
rows1 = db(
(db.reviews.reviews_landlordID == random_landlords[0])
).select().sort(lambda row: random.random()).first()
example_landlord2 = db.landlord[random_landlords[1]]
print(example_landlord2)
example_landlord2_name = example_landlord2.first_name + " " + example_landlord2.last_name
rows2 = db(
(db.reviews.reviews_landlordID == random_landlords[1])
).select().sort(lambda row: random.random()).first()
return dict(
load_reviews_url=URL('load_reviews', signer=url_signer),
add_reviews_url=URL('add_reviews', signer=url_signer),
delete_reviews_url=URL('delete_reviews', signer=url_signer),
search_url=URL('search', signer=url_signer),
example_landlord1_name=example_landlord1_name,
example_landlord1_id=random_landlords[0],
example_landlord2_name=example_landlord2_name,
example_landlord2_id=random_landlords[1],
rows1=rows1,
rows2=rows2,
get_votes_url=URL('get_votes', signer=url_signer),
set_votes_url=URL('set_votes', signer=url_signer),
get_voters_url=URL('get_voters', signer=url_signer),
get_search_url_url=URL('get_search_url', signer=url_signer),
)
@action('load_reviews')
@action.uses(url_signer.verify(), db)
def load_reviews():
rows = db(db.reviews).select().as_list()
email = auth.get_user()['email']
for r in rows:
# add URL of the corresponding landlord review page
r['url'] = URL('reviews', r['reviews_landlordID'])
landlord = db(r['reviews_landlordID'] == db.landlord.id).select().first()
r['landlord_name'] = landlord.first_name + ' ' + landlord.last_name
return dict(rows=rows, email=email)
@action('dashboard_landlord')
@action.uses(url_signer.verify(), db)
def dashboard_landlord():
return dict()
@action('dashboard_user')
@action.uses(db, session, auth.user, 'dashboard_user.html')
def dashboard_user():
user = db(db.auth_user.email == get_user_email()).select().first()
username = user.first_name + " " + user.last_name
email = user.email
return dict(
username=username,
email=email,
load_reviews_url=URL('load_reviews', signer=url_signer),
add_reviews_url=URL('add_reviews', signer=url_signer),
delete_reviews_url=URL('delete_reviews', signer=url_signer),
get_votes_url=URL('get_votes', signer=url_signer),
set_votes_url=URL('set_votes', signer=url_signer),
get_voters_url=URL('get_voters', signer=url_signer),
)
@action('reviews/<landlord_id:int>', method=["GET", "POST"])
@action.uses(db, session, auth.user, 'reviews.html')
def reviews(landlord_id=None):
assert landlord_id is not None
landlord = db.landlord[landlord_id]
landlord_name = landlord.first_name + " " + landlord.last_name
landlordID = landlord_id
session['landlordID'] = landlord_id
rows = db(
(db.reviews.reviews_landlordID == landlord_id)
).select() # as list
num_rows = db(
(db.reviews.reviews_landlordID == landlord_id)
).count()
avg_overall = 0
avg_friend = 0
avg_resp = 0
for r in rows:
avg_overall += float(r.reviews_score_overall)
avg_friend += float(r.reviews_score_friendliness)
avg_resp += float(r.reviews_score_responsiveness)
if num_rows > 0:
avg_overall = round(avg_overall/num_rows)
avg_friend = round(avg_friend/num_rows)
avg_resp = round(avg_resp/num_rows)
return dict(
avg_overall=avg_overall,
avg_friend=avg_friend,
avg_resp=avg_resp,
landlordID=landlordID,
landlord_name=landlord_name,
load_reviews_url=URL('load_reviews', signer=url_signer),
add_reviews_url=URL('add_reviews', signer=url_signer),
delete_reviews_url=URL('delete_reviews', signer=url_signer),
get_votes_url=URL('get_votes', signer=url_signer),
set_votes_url=URL('set_votes', signer=url_signer),
get_voters_url=URL('get_voters', signer=url_signer),
)
@action('add_landlord', method=["GET", "POST"])
@action.uses(db, session, auth.user, 'add_landlord.html')
def add_landlord():
form = Form(db.landlord, csrf_session=session, formstyle=FormStyleBulma)
if form.accepted:
landlord_id = int(db(db.landlord.id).select().last())
redirect(URL('reviews', landlord_id))
return dict(form=form)
@action('add_reviews', method=["GET", "POST"])
@action.uses(url_signer.verify(), db, auth, auth.user)
def add_reviews():
renter = db(db.auth_user.email == get_user_email()).select().first()
renter_name = renter.first_name + " " + renter.last_name
print(renter_name)
renter_id = renter.id if renter is not None else "Unknown"
renter_email = renter.email
reviews_landlordID = int(session.get('landlordID', None))
reviews_score_friendliness = int(request.json.get('reviews_score_friendliness'))
reviews_score_responsiveness = int(request.json.get('reviews_score_responsiveness'))
reviews_property_address = request.json.get('reviews_property_address')
reviews_score_overall = (reviews_score_friendliness+reviews_score_responsiveness)/2
reviews_contents = request.json.get('reviews_contents')
id = db.reviews.insert(
renter_name=renter_name,
reviews_renters_id=renter_id,
renter_email=renter_email,
reviews_landlordID=reviews_landlordID,
reviews_address_id=request.json.get('reviews_address_id'),
reviews_contents=request.json.get('reviews_contents'),
reviews_score_responsiveness=request.json.get('reviews_score_responsiveness'),
reviews_score_friendliness=request.json.get('reviews_score_friendliness'),
reviews_property_address=request.json.get('reviews_property_address'),
reviews_score_overall=(reviews_score_friendliness+reviews_score_responsiveness)/2,
)
return dict(
renter_name=renter_name,
id=id,
reviews_landlordID=reviews_landlordID,
renter_id=renter_id,
renter_email=renter_email,
reviews_score_responsiveness=reviews_score_responsiveness,
reviews_score_friendliness=reviews_score_friendliness,
reviews_property_address=reviews_property_address,
reviews_score_overall=reviews_score_overall,
reviews_contents=reviews_contents,
)
@action('delete_reviews')
@action.uses(url_signer.verify(), db)
def delete_reviews():
id = request.params.get('id')
assert id is not None
db(db.reviews.id == id).delete()
return "--REVIEW DELETED--"
@action('get_votes')
@action.uses(db, auth.user, url_signer.verify())
def get_votes():
review_id = request.params.get('review_id')
r = db((db.votings.review == review_id) & (db.votings.voter == get_user())).select().first()
voted = r.voted if r is not None else 0
return dict(voted=voted)
@action('set_votes', method="POST")
@action.uses(db, auth.user, url_signer.verify())
def set_votes():
review_id = request.json.get('review_id')
checkVoted = request.json.get('voted')
unVote = db((db.votings.review == review_id) & (db.votings.voter == get_user())).select().first()
if unVote is None:
voted = checkVoted
elif checkVoted == unVote['voted']:
voted = 0
else:
voted = checkVoted
db.votings.update_or_insert(((db.votings.review == review_id) & (db.votings.voter == get_user())),
voter=get_user(),
voted=voted,
review=review_id,
)
return "-Vote Updated-"
@action('get_voters')
@action.uses(db, auth.user, url_signer.verify())
def get_voters():
review_id = request.params.get('review_id')
count = 0
allvTers = db((db.votings.review == review_id) & (db.votings.voted == 1)).select().as_list()
for vTer in allvTers:
count = count + 1
alldvTers = db((db.votings.review == review_id) & (db.votings.voted == 2)).select().as_list()
for dvTer in alldvTers:
count = count - 1
return dict(count=count)
@action('search')
@action.uses()
def search():
q = request.params.get("q")
q_name = q.split()
q_first_name = q_name[0].title()
not_found = False
if len(q_name) < 2:
rows = db(db.landlord.first_name.ilike(q_first_name+'%')).select().as_list()
if len(rows) == 0:
not_found = True
else:
q_last_name = q_name[1].title()
rows = db((db.landlord.first_name.ilike(q_first_name + '%')) &
(db.landlord.last_name.ilike(q_last_name + '%'))).select().as_list()
if len(rows) == 0:
not_found = True
return dict(rows=rows, not_found=not_found)
@action('get_search_url')
@action.uses(db, url_signer)
def get_search_url():
lord_id = int(request.params.get('lord_id'))
return dict(url=URL('reviews', lord_id))