-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_display.py
147 lines (129 loc) · 6.23 KB
/
server_display.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
from flask import Blueprint, render_template, request, redirect, url_for, session, Flask
from werkzeug.utils import secure_filename
import data_manager, utility
import os
import user_handling
from datetime import datetime
app = Flask(__name__)
UPLOAD_FOLDER = "static/images"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
display = Blueprint('display', __name__,
template_folder='templates')
@display.route('/<question_id>', methods=['POST', 'GET'])
def route_display(question_id):
if request.method == 'GET':
answers = data_manager.get_answers_by_question_id(question_id)
question = data_manager.get_question_by_id(question_id)
question_tags = utility.get_tags_by_question_id(question_id)
question_comments = utility.show_comment_question(question_id)
answer_comments = []
for item in answers:
sublist = utility.show_comment_answer(item['id'])
for element in sublist:
answer_comments.append(element)
return render_template(
'display_question.html',
question=question,
answers=answers,
question_id=question_id,
question_tags=question_tags,
question_comments=question_comments,
answer_comments=answer_comments)
else:
file = request.files['file']
if file and utility.allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
answer = {'submission_time': datetime.today(),
'vote_number': 0,
'question_id': question_id,
'message': request.form['answer'],
'image': UPLOAD_FOLDER + '/' + filename if file.filename else '',
'user_id': None}
if 'user_id' in session:
answer['user_id'] = session['user_id']
data_manager.add_new_answer(answer)
return redirect('/display/' + question_id)
@display.route('/<question_id>/<direction>', methods=['POST'])
def route_counter_question(question_id, direction):
question = data_manager.get_question_by_id(question_id)
votes = int(question['vote_number'])
votes += 1 if direction == 'up-vote' else -1
updated_votes = {'id': question['id'],
"submission_time": question["submission_time"],
'view_number': question['view_number'],
'vote_number': votes,
'title': question['title'],
'message': question['message'],
'image': question['image']}
data_manager.update_question(updated_votes)
return redirect('/display/' + question_id)
@display.route('/<question_id>/<response_id>/<direction>', methods=['POST'])
def route_counter_answer(question_id, response_id, direction):
answers = data_manager.get_answer_by_id(response_id)
votes = int(answers['vote_number'])
votes += 1 if direction == 'up-vote' else -1
updated_votes = {'id': answers['id'],
"submission_time": answers["submission_time"],
'vote_number': votes,
'question_id': answers['question_id'],
'message': answers['message'],
'image': answers['image']}
data_manager.update_answer(updated_votes)
return redirect('/display/' + question_id)
@display.route('/<question_id>/edit', methods=['GET', 'POST'])
@user_handling.login_required
def route_edit_question(question_id):
question = data_manager.get_question_by_id(question_id)
if request.method == 'GET':
return render_template('ask.html', question=question,
req_url=url_for('display.route_edit_question',
question_id=question_id),
question_id=question_id)
else:
updated_question = {'id': question['id'],
"submission_time": question["submission_time"],
'view_number': question['view_number'],
'vote_number': question['vote_number'],
'title': request.form['title'],
'message': request.form['message'],
'image': question['image']}
data_manager.update_question(updated_question)
return redirect('/display/' + question_id)
@display.route('/<question_id>/<answer_id>/edit', methods=['GET', 'POST'])
@user_handling.login_required
def route_edit_answer(question_id, answer_id):
if request.method == 'GET':
answer = data_manager.get_answer_by_id(answer_id)
question = data_manager.get_question_by_id(question_id)
req_url = url_for('display.route_edit_answer',
question_id=question_id,
answer_id=answer_id)
return render_template('post_answer.html',
answers=answer,
question=question,
req_url=req_url)
else:
answer = data_manager.get_answer_by_id(answer_id)
updated_votes = {'id': answer['id'],
"submission_time": answer["submission_time"],
'vote_number': answer['vote_number'],
'question_id': answer['question_id'],
'message': request.form['answer'],
'image': answer['image']}
data_manager.update_answer(updated_votes)
return redirect('/display/' + question_id)
@display.route('/<question_id>/delete', methods=['GET', 'POST'])
@user_handling.login_required
def route_delete_question(question_id):
utility.delete_question_and_answers(question_id)
return redirect('')
@display.route('/<question_id>/<answer_id>/delete', methods=['GET', 'POST'])
@user_handling.login_required
def route_delete_answer(question_id, answer_id):
utility.delete_answer(answer_id)
return redirect('/display/' + question_id)
@display.route('/<question_id>/<comment_id>/commdelete')
def route_delete_comment(question_id, comment_id):
data_manager.delete_comment(comment_id)
return redirect(url_for('display.route_display', question_id=question_id))