forked from nerdneha/hs-restaurant-reviews
/
review.py
57 lines (45 loc) · 1.85 KB
/
review.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
import pymongo
from bottle import SimpleTemplate
class Review(object):
def __init__(self, data):
self.data = data
def to_html(self):
#Each review always has a restaurant name
template = ""
template = "<b><a href='/restaurant/{{item['restaurant_name']}}'>{{item['restaurant_name']}}</a></b>"
#Add address if there is one
if 'address' in self.data:
template += " at <b>{{item['address']}}</b>"
###ITEM TEXT FORMATTING###
item_exists = 'restaurant_item' in self.data
item_has_comments = 'item_comments' in self.data
item_has_price = 'item_price' in self.data
#text regarding the name of what you ate
if item_exists:
template += "<br> I had the {{item['restaurant_item']}}"
if item_has_price:
template += " for ${{item['item_price']}}"
if item_has_comments:
template += " and {{item['item_comments']}}"
###ITEM OVERALL FORMATTING###
comment_exists = 'restaurant_rating_reason' in self.data
rating_exists = 'restaurant_rating' in self.data
if comment_exists or rating_exists:
template += "<br> Overall: "
if comment_exists:
template += "{{item['restaurant_rating_reason']}}"
if rating_exists:
template += ", "
if rating_exists:
template += "{{item['restaurant_rating']}} of 5"
if 'is_recommended' in self.data:
template += "<br> I recommend this place!"
#Each review always has a reviewer and time
template += '''<br>
<i>review by <a href="/users/{{item['user']}}">{{item['user']}}</a> on {{item['time']}}</i>
'''
return SimpleTemplate(template).render(item = self.data)
def get_all_reviews(db):
return [Review(data) for data in db.reviews.find()]
def get_reviews_by(db, attribute, value):
return [Review(data) for data in db.reviews.find({attribute: value})]