-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
306 lines (249 loc) · 12.8 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
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
from flask import Flask, request, render_template, session, redirect, url_for, escape, request
import data_handler
import data_handler_2
app = Flask(__name__)
@app.route('/')
def index():
username = session['username'] if 'username' in session else 'Anonymus'
if username != 'Anonymus':
image = data_handler_2.get_users_image(username)['image']
id = data_handler_2.get_id_by_username(username)['id']
return render_template("main_page.html", username=username, image=image, id=id)
else:
return render_template("main_page.html", username=username)
@app.route('/table')
def table():
username = session['username'] if 'username' in session else 'Anonymus'
info = data_handler.get_all_data_for_lapozo('question', 0)
how_many_question = len(data_handler.get_all_data('question'))
pages = int(how_many_question / 5)
return render_template("table.html", info=info, username=username, pages=pages)
@app.route('/table/<int:number>')
def table__for_sites(number):
offset_number = ((number -1) * 10)
how_many_question = len(data_handler.get_all_data('question'))
pages = int(how_many_question / 10)
if (how_many_question % 10) != 0:
pages = pages + 1
username = session['username'] if 'username' in session else 'Anonymus'
info = data_handler.get_all_data_for_lapozo('question', offset_number)
return render_template("table.html", info=info, username=username, first_pages=offset_number, pages=pages)
@app.route('/question/add_new_question', methods=['GET', 'POST'])
def add_new_question():
if request.method == 'GET':
tags = data_handler.get_all_tags()
return render_template("new_question.html", tags=tags)
else:
title = request.form["title"]
message = request.form["message"]
image = request.form["image"]
tag = request.form["tag"]
username = session['username']
data_handler.add_new_question(title, message, image, tag, username)
return redirect("/")
@app.route('/question/<int:question_id>', methods=["GET", "POST"])
def question_details(question_id):
info = data_handler.get_question_by_id(question_id)
info_tag = data_handler.get_tag(question_id)
profile_question = data_handler_2.get_profile_by_question_id(question_id)
question_data = info[0]['title']
detail_data = info[0]['message']
picture_data = info[0]['image']
view_number = info[0]['view_number']
popular_number = info[0]['vote_number']
tag = info_tag['name']
username = session['username'] if 'username' in session else 'Anonymus'
answers = data_handler.get_answers_by_question_id(question_id)
if request.method == 'GET':
view_number += 1
info[0]['view_number'] = view_number
data_handler.update_view_number(view_number, question_id)
return render_template("question.html", question_id=question_id, question_data=question_data,
detail_data=detail_data, profile_question=profile_question,
picture_data=picture_data, view_number=view_number, popular_number=popular_number,
tag=tag, answers=answers, username=username)
@app.route('/like/<int:question_id>', methods=["GET"])
def popularity(question_id):
question = data_handler.get_question_by_id(question_id)
popular_number = question[0]['vote_number']
view_number = question[0]['view_number']
if request.method == 'GET':
popular_number += 1
view_number -= 1
data_handler.update_question(question_id, view_number, popular_number)
return redirect(url_for('question_details', question_id=question_id))
@app.route('/dislike/<int:question_id>', methods=["GET"])
def not_so_popular(question_id):
question = data_handler.get_question_by_id(question_id)
popular_number = question[0]['vote_number']
view_number = question[0]['view_number']
if request.method == 'GET':
popular_number += -1
view_number -= 1
data_handler.update_question(question_id, view_number, popular_number)
return redirect(url_for('question_details', question_id=question_id))
@app.route("/question/add_new_answer/<int:question_id>", methods=['GET', 'POST'])
def add_new_answer(question_id):
answer = request.form["answer"]
username = session['username']
data_handler.add_new_answer(answer, question_id, username)
return redirect(url_for('question_details', question_id=question_id))
@app.route('/answer_like/<int:question_id>/<int:answer_id>', methods=["GET", "POST"])
def answer_more_popularity(question_id, answer_id):
answer = data_handler.get_answer_by_id(answer_id)
answer_popular_number = answer['vote_number']
question = data_handler.get_question_by_id(question_id)
view_number = question[0]['view_number']
popular_number = question[0]['vote_number']
if request.method == 'GET':
answer_popular_number += 1
data_handler.update_answer_vote_number(answer_id, answer_popular_number)
view_number -= 1
data_handler.update_question(question_id, view_number, popular_number)
return redirect(
url_for('question_details', question_id=question_id))
@app.route('/answer_dislike/<int:question_id>/<int:answer_id>', methods=["GET", "POST"])
def answer_less_popularity(question_id, answer_id):
answer = data_handler.get_answer_by_id(answer_id)
answer_popular_number = answer['vote_number']
question = data_handler.get_question_by_id(question_id)
view_number = question[0]['view_number']
popular_number = question[0]['vote_number']
if request.method == 'GET':
answer_popular_number -= 1
data_handler.update_answer_vote_number(answer_id, answer_popular_number)
view_number -= 1
data_handler.update_question(question_id, view_number, popular_number)
return redirect(
url_for('question_details', question_id=question_id))
@app.route('/answer_edit/<int:question_id>/<int:answer_id>', methods=["GET", "POST"])
def answer_edit(question_id, answer_id):
if request.method == 'GET':
answer = data_handler.get_answer_by_id(answer_id)
return render_template("answer_edit.html", answer=answer)
message = request.form['message']
data_handler.update_answer(answer_id, message)
return redirect(url_for('question_details', question_id=question_id))
@app.route('/comment/<int:question_id>', methods=["GET", "POST"])
def question_comment(question_id):
if request.method == 'GET':
comments = data_handler.get_all_comments(question_id)
return render_template("comment.html", question_id=question_id, comments=comments)
else:
comment = request.form["comment"]
username = session['username']
data_handler.add_new_comment(comment, question_id, username)
comments = data_handler.get_all_comments(question_id)
return render_template("comment.html", question_id=question_id, comments=comments)
@app.route('/comment_answer/<int:answer_id>', methods=["GET", "POST"])
def answer_comment(answer_id):
if request.method == 'GET':
comments = data_handler.get_all_comments_answer(answer_id)
question_id = data_handler.get_question_id_by_answer_id(answer_id)
return render_template("comment_answer.html", answer_id=answer_id, comments=comments, question_id=question_id)
else:
comment_answer = request.form["comment"]
username = session['username']
data_handler.add_new_comment_answer(comment_answer, answer_id, username)
comments = data_handler.get_all_comments_answer(answer_id)
question_id = data_handler.get_question_id_by_answer_id(answer_id)
return render_template("comment_answer.html", answer_id=answer_id, comments=comments, question_id=question_id)
@app.route('/search_results', methods=['GET', 'POST'])
def find_search_results():
form_data = request.form.to_dict()
search_phrase = form_data['search_phrase']
if search_phrase == '':
search_phrase = ' '
return redirect(url_for('show_search_results', search_phrase=search_phrase))
@app.route('/search?q=<search_phrase>')
def show_search_results(search_phrase):
results = data_handler.get_search_results(search_phrase)
return render_template('search_results.html', results=results)
@app.route('/delete-tag/<int:question_id>')
def delete_tag_from_question(question_id):
data_handler.delete_tag_from_question(question_id)
return redirect(url_for('question_details', question_id=question_id))
@app.route('/delete_comment/<int:question_id>/<int:comment_id>')
def delete_question_comment(question_id, comment_id):
data_handler.delete_line(comment_id)
comments = data_handler.get_all_comments(question_id)
return render_template('comment.html', question_id=question_id, comments=comments)
@app.route('/delete_comment_answer/<int:answer_id>/<int:comment_id>')
def delete_answer_comment(answer_id, comment_id):
data_handler.delete_line(comment_id)
comments = data_handler.get_all_comments_answer(answer_id)
question_id = data_handler.get_question_id_by_answer_id(answer_id)
return render_template('comment_answer.html', answer_id=answer_id, question_id=question_id, comments=comments)
@app.route('/edit_comment_answer/<int:answer_id>/<int:comment_id>', methods=["GET", "POST"])
def edit_answer_comment(answer_id, comment_id):
comment = data_handler.get_comment_by_id(comment_id)
if request.method == 'GET':
return render_template("comment_edit.html", comment=comment, answer_id=answer_id)
else:
message = request.form['message']
comment_id = comment['id']
data_handler.update_comment(comment_id, message)
return redirect(url_for('answer_comment', answer_id=answer_id))
@app.route('/tags')
def number_of_tags():
tags = data_handler.count_tags()
return render_template("tags.html", tags=tags)
@app.route('/registration', methods=["GET", "POST"])
def registration():
if request.method == 'GET':
return render_template("user_registration.html")
else:
username = request.form['username']
every_username = data_handler_2.check_name_in_database(username)
if every_username is None:
image = request.form['image']
password = request.form['password']
hashed_password = data_handler_2.hash_password(password)
if len(image) < 5:
data_handler_2.save_registration_without_image(username, hashed_password)
else:
data_handler_2.save_registration(username, hashed_password, image)
return render_template("log_in.html")
else:
return render_template('message.html', message='Username already taken', url=url_for('registration'))
@app.route('/log_in', methods=['GET', 'POST'])
def log_in():
if request.method == 'GET':
return render_template("log_in.html")
else:
username_check = request.form['username']
password_check_input = request.form['password']
username = data_handler_2.check_name_in_database(username_check)
if username is not None:
password_check_database = data_handler_2.get_users_password(username_check)['password']
verify = data_handler_2.verify_password(password_check_input, password_check_database)
if verify is True:
username = session['username'] = request.form['username']
return render_template('message.html', message='Successful log in as {0}'.format(username), url=url_for('index'))
else:
return render_template('message.html', message='You wrote wrong username or password',
url=url_for('log_in'))
else:
return render_template('message.html', message='You wrote wrong username or password', url=url_for('log_in'))
@app.route('/logout')
def logout():
if 'username' not in session:
return render_template('message.html', message='You are not logged in, silly', url=url_for('log_in'))
else:
session.pop('username', None)
return render_template('message.html', message='Check out is successful', url=url_for('index'))
@app.route('/user-page/<int:user_id>')
def user_page(user_id):
questions = data_handler_2.get_questions_by_user(user_id)
answers = data_handler_2.get_answers_by_user(user_id)
question_comments = data_handler_2.get_question_comments_by_user(user_id)
answer_comments = data_handler_2.get_answer_comments_by_user(user_id)
return render_template("user_page.html", questions=questions, answers=answers, question_comments=question_comments,
answer_comments=answer_comments)
@app.route('/user-list')
def user_list():
users = data_handler_2.get_all_user_data()
return render_template('user_list.html', users=users)
if __name__ == '__main__':
app.secret_key = 'superheroes'
app.run(debug=True)