-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
146 lines (124 loc) · 5.07 KB
/
application.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
import os, requests
from flask import Flask, session, render_template, url_for, request, redirect, jsonify
from flask_session import Session
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
app = Flask(__name__)
# Check for environment variable
if not os.getenv("DATABASE_URL"):
raise RuntimeError("DATABASE_URL is not set")
# Configure session to use filesystem
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Set up database
# postgres://xqlpqzxylltuvf:6f4e2969fc6fa5aa129df741c341bf2bbd4a9f8d1fb6f0a0d05e8a8283e12dd9@ec2-184-73-153-64.compute-1.amazonaws.com:5432/df45h8i6h1g4nc
# postgres://prashantsingh:j@localhost:5432/postgres
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
# Goodreads api key
KEY = "k6fepBYZ2ZoNgVz6kfLg"
@app.route("/")
def index():
if "username" in session:
return redirect(url_for("search"))
else:
return render_template("index.html")
@app.route("/reg", methods=["POST", "GET"])
def reg():
method = request.method
if method == "POST":
# register user if not already exist
username = request.form.get("username")
password = request.form.get("password")
row = db.execute("SELECT * FROM users WHERE username = :username", {"username":username})
if row.rowcount == 1:
message = "user already exist"
return render_template("error.html", message=message)
else:
db.execute("INSERT INTO users(username, password) VALUES(:username, :password)", {"username":username, "password":password})
db.commit()
# redirect to login
return render_template("login.html")
else:
return render_template("registration.html")
@app.route("/login", methods=["GET", "POST"])
def login():
method = request.method
if method == "POST":
username = request.form.get("username")
password = request.form.get("password")
# search books
userRow = db.execute("SELECT * FROM users WHERE username = :username", {"username": username})
if userRow.rowcount == 0:
message = "user does not exist "
return render_template("error.html", message=message)
pwRow = db.execute("SELECT * FROM users WHERE password = :password", {"password": password})
if pwRow.rowcount == 0:
message = "Please enter correct password"
return render_template("error.html", message=message)
session["username"] = username
return redirect(url_for("search"))
else:
return render_template("login.html")
@app.route("/search", methods=["GET", "POST"])
def search():
method = request.method
if method == "POST":
query = request.form.get("query")
query = "%" + query + "%"
# get all books matching with query
books = db.execute("SELECT * FROM books WHERE title LIKE :query OR isbn LIKE :query OR author LIKE :query OR year LIKE :query", {"query":query}).fetchall()
return render_template("search.html", books=books)
else:
return render_template("search.html")
'''
Show the details of selected book .
'''
@app.route("/book/<isbn>")
def book(isbn):
book = db.execute("SELECT * FROM books WHERE isbn = :isbn", {"isbn":isbn}).fetchone()
if book is None:
return render_template("error.html", message="No such book")
res = requests.get("https://www.goodreads.com/book/review_counts.json", params={"key": KEY, "isbns": isbn})
resJson = res.json()
bookReview = resJson["books"][0]
#Show other users comment
usersComment = db.execute("SELECT * FROM reviews WHERE isbn = :isbn", {"isbn":isbn}).fetchall()
return render_template("book.html", book=book, bookReview = bookReview, usersComment=usersComment)
'''
Save review of the book for current user.
'''
@app.route("/saveReview/<isbn>", methods=["GET", "POST"])
def saveReview(isbn):
method = request.method
if method == "POST":
username = session["username"]
rating = request.form.get("rating")
comment = request.form.get("comment")
# if comment already exist then update it
row = db.execute("SELECT * FROM reviews WHERE username = :username AND isbn = :isbn", {"username":username, "isbn":isbn})
if row.rowcount >= 1:
db.execute("DELETE FROM reviews WHERE username = :username AND isbn = :isbn", {"username":username, "isbn":isbn, "rating":rating, "comment":comment})
db.execute("INSERT INTO reviews(username, isbn, rating, comment) VALUES(:username, :isbn, :rating, :comment)", {"username":username, "isbn":isbn, "rating":rating, "comment":comment})
db.commit()
# message = "Your comment is saved successfully !"
return redirect(url_for("book", isbn = isbn))
else:
message = "Please save comment properly."
return render_template("error.html", message=message)
@app.route("/logout")
def logout():
session.pop("username", None)
return redirect(url_for("index"))
'''
Return results for books query .
This implements autosearch feature .
'''
@app.route("/books")
def books():
query = request.args.get("query")
query = "%" + query + "%"
# get all books matching with query
books = db.execute("SELECT * FROM books WHERE title ILIKE :query OR isbn ILIKE :query OR author ILIKE :query OR year ILIKE :query", {"query":query}).fetchall()
return render_template("books.html", books=books)