-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
213 lines (172 loc) · 5.99 KB
/
server.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
from flask import Flask, render_template, session, flash, request, redirect, url_for
import sqlite3
from Database import Database
from base64 import b64encode
from RegisterForm import RegisterForm
from passlib.hash import sha256_crypt
from LoginForm import LoginForm
from functools import wraps
# TODO: DELETE BOOKS PRAGMA ACIVATE
app = Flask(__name__)
db = Database("Bookshop.db")
def require_login(function): # add wishlistte kullanılcak
@wraps(function)
def decorated_function(*args, **kwargs):
if "logged_in" in session:
return function(*args, **kwargs)
else:
flash("Please login to see this page.")
return redirect(url_for("login"))
return decorated_function
@app.route("/logout")
@require_login
def logout():
session.clear()
return redirect(url_for("index"))
@app.route("/unregister")
@require_login
def unregister():
username = session["username"]
session.clear()
message = db.delete_user(username)
flash(message)
return redirect(url_for("index"))
@app.route("/")
def index():
return render_template("index.html")
@app.route("/book/update_book<string:key>", methods=["GET", "POST"])
def update_book(key):
if request.method == "POST":
website = request.form["website"]
area = request.form["area"]
stock = request.form["stock"]
title = request.form["title"]
order = request.form["order"]
message = db.update_book(
str(key), str(website), str(area), str(stock), str(title), str(order)
)
flash(message)
return redirect(url_for("showbooks"))
return render_template("./book/update_book.html", bookId=key)
@app.route("/register", methods=["GET", "POST"])
def register():
form = RegisterForm(request.form)
if request.method == "POST" and form.validate():
name = form.name.data
username = form.username.data
email = form.email.data
password = sha256_crypt.encrypt(form.password.data)
message = db.register(name, username, email, password)
flash(message)
return redirect(url_for("index"))
else:
return render_template("register.html", form=form)
@app.route("/login", methods=["GET", "POST"])
def login():
form = LoginForm(request.form)
if request.method == "POST":
username = form.username.data
password = form.password.data
message, success = db.login(username, password)
flash(message)
if success:
redirect(url_for("index"))
session["logged_in"] = True
session["username"] = username
return redirect(url_for("index"))
else:
return redirect(url_for("login"))
return render_template("login.html", form=form)
@app.route("/book/showbooks")
def showbooks():
rows = db.show_all_books()
imgs = []
fav_book = None
for i in rows:
try:
imgs.append(b64encode(i["Img"]).decode("utf-8"))
except:
pass
if "logged_in" in session:
fav_book = db.get_fav_book(session["username"])
if fav_book:
fav_book = fav_book[0]
return render_template(
"./book/show_books.html", rows=rows, imgs=imgs, fav_book=fav_book
)
@app.route("/author/showauthor")
def showauthor():
rows = db.show_all_authors()
birthday = []
for row in rows:
try:
birthday.append(row["Birthday"].split()[0])
except:
pass
return render_template("./author/show_authors.html", rows=rows, birthday=birthday)
@app.route("/author/addauthor", methods=["POST", "GET"])
def addauthor():
if request.method == "POST":
firstName = request.form["firstName"]
lastName = request.form["lastName"]
birthday = request.form["birthday"]
country = request.form["country"]
hrs = request.form["hrs"]
message = db.add_author(firstName, lastName, birthday, country, hrs)
flash(message)
return render_template("./author/addauthor.html")
@app.route("/book/addbook", methods=["POST", "GET"])
def addbook():
if request.method == "POST":
title = request.form["title"]
website = request.form["website"]
area = request.form["area"]
stock = request.form["stock"]
authId = request.form["authId"]
message = db.add_book(title, website, area, stock, authId)
flash(message)
return render_template("./book/addbook.html")
@app.route("/author/removeauthor", methods=["POST", "GET"])
def removeauthor():
if request.method == "POST":
message = db.remove_author(request.form["authorId"])
flash(message)
return render_template("./author/removeauthor.html")
@app.route("/book/removebook", methods=["POST", "GET"])
def removebook():
if request.method == "POST":
message = db.remove_book(request.form["bookId"])
flash(message)
return render_template("./book/removebook.html")
@app.route("/author/update_author<string:key>", methods=["POST", "GET"])
def update_author(key):
if request.method == "POST":
firstName = request.form["firstName"]
lastName = request.form["lastName"]
birthday = request.form["birthday"]
country = request.form["country"]
hrs = request.form["hrs"]
message = db.update_author(
str(key),
str(firstName),
str(lastName),
str(birthday),
str(country),
str(hrs),
)
flash(message)
return render_template("./author/update_author.html", authorId=key)
@app.route("/book/make_fav<string:key>")
@require_login
def make_fav(key):
message = db.make_fav(session["username"], key)
flash(message)
return redirect(url_for("showbooks"))
@app.route("/book/author_info<string:key>")
def author_info(key):
rows, message = db.show_author_info(key)
flash(message)
return render_template("./book/author_info.html", rows=rows)
if __name__ == "__main__":
app.secret_key = "itudb2109"
app.run(debug=True)