This repository was archived by the owner on Dec 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
222 lines (180 loc) · 8.96 KB
/
server.py
File metadata and controls
222 lines (180 loc) · 8.96 KB
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
from flask import Flask, render_template, request, redirect
import data_manager
from operator import itemgetter
import csv
from datetime import datetime
app = Flask(__name__)
@app.route("/")
def top5():
TABLE_HEADERS = ["id", "submission_time", "view_number", "vote_number", "title", "message", "image"]
questions = data_manager.question_top_five()
return render_template("index.html", TABLE_HEADERS=TABLE_HEADERS, questions=questions)
@app.route("/list")
def list():
TABLE_HEADERS = ["id", "submission_time", "view_number", "vote_number", "title", "message", "image"]
questions = data_manager.get_questions()
return render_template("list.html", TABLE_HEADERS=TABLE_HEADERS, questions=questions)
@app.route("/question/<string:question_id>/", methods=["POST", "GET"])
def display_question(question_id):
questions = data_manager.get_data_question(question_id)
answers = data_manager.get_answers()
comments = data_manager.get_comments()
tags=data_manager.get_tags()
return render_template("question.html", question_id=question_id, answers=answers, questions=questions, comments=comments, tags=tags)
@app.route("/question/<string:question_id>/edit/", methods=["POST", "GET"])
def edit(question_id):
question_to_update = data_manager.get_data_question(question_id)
if request.method == "POST":
new_title = request.form["update-title"]
new_message = request.form["update-message"]
data_manager.update_question(new_title, new_message, question_id)
return redirect("/question/"+question_id)
else:
return render_template("edit.html", question_id=question_id, question_to_update = question_to_update)
@app.route("/add_question", methods=["POST", "GET"])
def question():
if request.method == "POST":
data_manager.add_new_question(datetime.now().timestamp(), 0, 0, request.form["question_name"], request.form["description"], "")
return redirect("/list")
return render_template("add-question.html")
@app.route("/question/<string:question_id>/newanswer/", methods=["POST", "GET"])
def answer(question_id):
if request.method == "POST":
data_manager.add_new_answer(0, 0, question_id, request.form["comment"],"")
return redirect("/question/"+question_id)
return render_template("newanswer.html")
@app.route("/question/<string:question_id>/comment_to_question", methods=["POST", "GET"])
def comment_to_question(question_id):
if request.method == "POST":
comment = request.form.get("comment")
data_manager.add_new_comment(question_id,comment,0,0)
return redirect("/question/"+question_id)
else:
return render_template("comment_to_question.html")
@app.route("/question/<string:question_id>/comment_to_answer", methods=["POST", "GET"])
def comment_to_answer(question_id, answer_id):
if request.method == "POST":
comment = request.form.get("comment")
data_manager.add_new_answer(answer_id,comment,0,0)
return redirect("/question/"+question_id)
else:
return render_template("comment_to_answer.html")
@app.route("/<string:question_id>/vote_up", methods=["POST","GET"])
def vote_up(question_id):
question_id=question_id
vote_a = data_manager.get_data_question_vote(question_id)
vote=int(vote_a[-1])
if request.method == "GET":
print(vote)
vote += 1
data_manager.update_question_vote(vote, question_id)
return redirect("/list")
@app.route("/<string:question_id>/vote_down", methods=["POST","GET"])
def vote_down(question_id):
question_id = question_id
questions = data_manager.get_questions()
question_to_update = get_data(questions, question_id)
vote = int(question_to_update["vote_number"])
if request.method == "GET":
vote -= 1
question_to_update["vote_number"]=str(vote)
questions.pop(questions.index(question_to_update))
questions.append(question_to_update)
with open("/home/shadowsamurai/gitdir/ask-mate-2-python-Andrassyattila/sample_data/question.csv", 'w', newline='') as csvfile:
fieldnames = ["id", "submission_time", "view_number", "vote_number", "title", "message", "image"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for row in questions:
writer.writerow(row)
return redirect("/list")
@app.route("/question/<int:question_id>/delete_question/", methods=["POST", "GET"])
def delete_question(question_id):
if request.method == "GET":
question_id=question_id
data_manager.delete_question(question_id)
return redirect("/list")
@app.route("/question/<string:question_id>/delete_answer/<string:answer_id>/", methods=["POST", "GET"])
def delete_answer(answer_id, question_id):
if request.method == "GET":
data_manager.delete_answer(answer_id)
return redirect("/question/"+question_id)
@app.route("/question/<string:question_id>/delete_comment/<string:comment_id>/", methods=["POST", "GET"])
def delete_comment(comment_id, question_id):
if request.method == "GET":
data_manager.delete_comment(comment_id)
return redirect("/question/"+question_id)
'''@app.route("/question/<string:question_id>/vote_up_answer", methods=["POST", "GET"])
def vote_up_answer(question_id):
question_id=question_id
answers = data_manager.get_answers()
answer_to_update = get_data_answer(answers, question_id)
vote = int(answer_to_update["vote_number"])
if request.method == "GET":
vote += 1
answer_to_update["vote_number"]=str(vote)
answers.pop(answers.index(answer_to_update))
answers.append(answer_to_update)
with open("/home/shadowsamurai/gitdir/ask-mate-2-python-Andrassyattila/sample_data/answer.csv", 'w', newline='') as csvfile:
fieldnames = ["id","submission_time","vote_number","question_id","message,image"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for row in answers:
writer.writerow(row)
return redirect("/list")
@app.route("/question/<string:question_id>/vote_down_aswer", methods=["POST", "GET"])
def vote_down_answer(question_id):
question_id = question_id
questions = data_manager.get_questions()
question_to_update = get_data_answer(questions, question_id)
vote = int(question_to_update["vote_number"])
if request.method == "GET":
vote -= 1
question_to_update["vote_number"]=str(vote)
questions.pop(questions.index(question_to_update))
questions.append(question_to_update)
with open("/home/shadowsamurai/gitdir/ask-mate-2-python-Andrassyattila/sample_data/question.csv", 'w', newline='') as csvfile:
fieldnames = ["id","submission_time","vote_number","question_id","message,image"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for row in questions:
writer.writerow(row)
return redirect("/list")'''
@app.route("/search/", methods=["POST", "GET"])
def search_question():
TABLE_HEADERS = ["id", "submission_time", "view_number", "vote_number", "title", "message", "image"]
found_data = request.args.get('q')
if found_data:
found = data_manager.search_question(found_data)
return render_template("search.html", found= found, found_data=found_data, TABLE_HEADERS=TABLE_HEADERS)
@app.route("/question/<question_id>/answer/<string:answer_id>/edit/", methods=["POST", "GET"])
def edit_answer(answer_id, question_id):
answer_to_update = data_manager.get_data_answer(answer_id)
if request.method == "POST":
new_message = request.form["update-message"]
data_manager.update_question(new_message, answer_id)
return redirect("/question/"+question_id)
else:
return render_template("edit.html",question_id=question_id, answer_id=answer_id, answer_to_update=answer_to_update)
@app.route("/question/<question_id>/comment/<string:comment_id>/edit/", methods=["POST", "GET"])
def edit_comment(comment_id, question_id):
comment_to_update = data_manager.get_data_comment(comment_id)
if request.method == "POST":
new_message = request.form["update-message"]
data_manager.update_question(new_message, comment_id)
return redirect("/question/"+question_id)
else:
return render_template("edit.html",question_id=question_id, comment_id=comment_id, comment_to_update=comment_to_update)
@app.route("/question/<string:question_id>/tag/", methods=["POST", "GET"])
def add_tag(question_id):
tags=data_manager.get_tags()
if request.method == "POST":
new_tag=request.form["tag-name"]
data_manager.add_new_tag(new_tag)
data_manager.tag_to_question(question_id,new_tag)
return redirect("/question/"+question_id)
else:
return render_template("tag.html",question_id=question_id,tags=tags)
if __name__ == "__main__":
app.run(
debug=True
)