-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathapp.py
95 lines (80 loc) · 3.15 KB
/
app.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
import uuid, json, redis, flask
COOKIE_NAME = "sessionID"
def get_session_id():
""" Read session id from cookies, if present """
return flask.request.cookies.get(COOKIE_NAME)
def set_session_id(response, override=False):
""" Store session id in a cookie """
session_id = get_session_id()
if not session_id or override:
session_id = uuid.uuid4()
response.set_cookie(COOKIE_NAME, str(session_id))
CACHE_CLIENT = redis.Redis(host="localhost", port=6379, db=0)
# Chaos experiment 1 - uncomment this to add latency to Redis access
#import chaos
#CACHE_CLIENT = chaos.attach_chaos_if_enabled(CACHE_CLIENT)
# Chaos experiment 2 - uncomment this to raise an exception every other call
#import chaos2
#@chaos2.raise_rediserror_every_other_time_if_enabled
def get_interests(session):
""" Retrieve interests stored in the cache for the session id """
return json.loads(CACHE_CLIENT.get(session) or "[]")
def store_interests(session, query):
""" Store last three queries in the cache backend """
stored = get_interests(session)
if query and query not in stored:
stored.append(query)
stored = stored[-3:]
CACHE_CLIENT.set(session, json.dumps(stored))
return stored
def recommend_other_products(query, interests):
""" Return a list of recommended products for a user, based on interests """
if interests:
return {"this amazing product": "https://youtube.com/watch?v=dQw4w9WgXcQ"}
return {}
app = flask.Flask(__name__)
@app.route("/")
def index():
""" Handle the home page, search form """
resp = flask.make_response("""
<html><body>
<form action="/search" method="POST">
<p><h3>What would you like to buy today?</h3></p>
<p><input type='text' name='query'/>
<input type='submit' value='Search'/></p>
</form>
<p><a href="/search">Recommendations</a>. <a href="/reset">Reset</a>. </p>
</body></html>
""")
set_session_id(resp)
return resp
@app.route("/search", methods=["POST", "GET"])
def search():
""" Handle search, suggest other products """
session_id = get_session_id()
query = flask.request.form.get("query")
try:
new_interests = store_interests(session_id, query)
except redis.exceptions.RedisError as exc:
print("LOG: redis error %s" % str(exc))
new_interests = None
recommendations = recommend_other_products(query, new_interests)
return flask.make_response(flask.render_template_string("""
<html><body>
{% if query %}<h3>I didn't find anything for "{{ query }}"</h3>{% endif %}
<p>Since you're interested in {{ new_interests }}, why don't you try...
{% for k, v in recommendations.items() %} <a href="{{ v }}">{{ k }}</a>{% endfor %}!</p>
<p>Session ID: {{ session_id }}. <a href="/">Go back.</a></p>
</body></html>
""",
session_id=session_id,
query=query,
new_interests=new_interests,
recommendations=recommendations,
))
@app.route("/reset")
def reset():
""" Reset the session ID cookie """
resp = flask.make_response(flask.redirect("/"))
set_session_id(resp, override=True)
return resp