-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.py
More file actions
390 lines (297 loc) · 13 KB
/
routes.py
File metadata and controls
390 lines (297 loc) · 13 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
from flask import render_template, redirect, url_for, request, jsonify
from flask_login import LoginManager, login_user, current_user, login_required, logout_user
from forms import *
from app import app, db
from models import *
import json
# Configure flask login
login = LoginManager(app)
login.init_app(app)
@login.user_loader
def load_user(id):
return User.query.get(int(id))
@app.route("/", methods=['GET', 'POST'])
def index():
login_form = LoginForm()
reg_form = RegistrationForm()
# Allow login if validation success
if login_form.validate_on_submit():
user_object = User.query.filter_by(username=login_form.username.data).first()
login_user(user_object)
return redirect(url_for('index'))
return render_template("index.html", register_form=reg_form, login_form=login_form)
@app.route("/private", methods=['GET'])
@login_required
def private():
# Usar isso se na for usar o "@login_required"
#if not current_user.is_authenticated:
# return "Please login."
return "something"
@app.route("/logout", methods=['GET'])
def logout():
logout_user()
return redirect(url_for('index'))
@app.route('/register/', methods=['POST'])
def register():
reg_form = RegistrationForm()
# Updated database if validation success
if reg_form.validate_on_submit():
username = reg_form.username.data
password = reg_form.password.data
hashed_pswd = pbkdf2_sha256.hash(password)
# Check username exists
user_object = User.query.filter_by(username=username).first()
if user_object:
render_template('register.html', form=reg_form)
# Add it into DB
user = User(username=username, password=hashed_pswd)
db.session.add(user)
db.session.commit()
return redirect(url_for('index'))
@app.route('/search/')
def search():
return render_template('search.html')
@app.route('/create-project/', methods=['POST'])
@login_required
def create_project():
proj_form = ProjectForm()
if proj_form.validate_on_submit():
name = proj_form.name.data
description = proj_form.description.data
# Check name exists
project_object = Project.query.filter_by(name=name).first()
if project_object:
return redirect(url_for('projects')) # Colocar mensagem de erro se nome for igual
# Add it into DB
id = load_user( current_user.id ).id
project = Project(name=name, owner=id, subs=0, description=description)
db.session.add(project)
db.session.commit()
return redirect(url_for('projects'))
@app.route('/delete-project', methods=['POST'])
@login_required
def delete_project():
project_id = request.form['project_id']
# Check name exists
project_object = Project.query.filter_by(id=project_id).first()
if project_object:
id = load_user( current_user.id ).id
if project_object.owner == id:
# First it deletes all questions and solutions
questions = Question.query.filter_by(project=project_id).all()
for question in questions:
delete_question(project_name=project_object.name, question_id=question.id)
# Unsubscribes everyone
users = project_user = User.query.join(User.projects).filter(Project.id==project_id).all()
for user in users:
unsubscribe(project_name=project_object.name, user=user)
db.session.delete(project_object)
db.session.commit()
#else TODO
# colocar notificação de que não foi possível deletar
return redirect(url_for('projects'))
@app.route('/projects/', methods=['GET', 'POST'])
@login_required
def projects():
# Forms de busca
search_form = SearchProjects()
# Todos os projetos existentes
projects = Project.query.all()
# Projetos os quais sou owner
my_projects = Project.query.filter_by(owner=load_user( current_user.id ).id)
# Projetos nos quais estou inscrito
subscribed_projects = current_user.projects
# Form para a criação de projetos
proj_form = ProjectForm()
if request.method == 'POST':
form_name = request.form['form-name']
if form_name == 'search' and search_form.validate_on_submit():
name = search_form.name.data
projects = Project.query.filter_by(name=name)
return render_template('projects.html', projects=projects, form=search_form, my_projects=my_projects,
subscribed_projects=subscribed_projects, create_project_form=proj_form)
@app.route('/projects/<string:project_name>/subscribe', methods=['POST'])
@login_required
def subscribe(project_name=None):
if not project_name:
return 'Not possible' # TODO
project_object = Project.query.filter_by(name=project_name).first()
id = load_user( current_user.id ).id
# if it is already subscribed, do not subscribe
project_user = User.query.join(User.projects).filter(Project.name==project_name).filter(User.id==id).first()
if project_user or project_object.owner == id:
return redirect(url_for('projects'))
project_object.subscribers.append(current_user)
# Adiciona um nos subscribers
project_object.subs = project_object.subs + 1
db.session.commit()
return redirect(url_for('projects'))
@app.route('/projects/<string:project_name>/unsubscribe', methods=['POST'])
@login_required
def unsubscribe(project_name=None, user=None):
if not user:
user = current_user
if not project_name:
return 'Not possible' # TODO
project_object = Project.query.filter_by(name=project_name).first()
if project_object.subs > 0:
project_object.subs = project_object.subs - 1
project_object.subscribers.remove(user)
db.session.commit()
return redirect(url_for('projects'))
@app.route('/quill')
def quill():
return render_template('quill.html')
@app.route("/temp", methods=['GET', 'POST'])
@login_required
def temp():
return render_template('temp.html')
# Project page
@app.route("/projects/<string:project_name>", methods=['GET'])
@login_required
def project(project_name=None):
# Forms da question
question_form = QuestionForm()
# Returns questions related to project_name
project = Project.query.filter_by(name=project_name).first()
project_id = project.id
# Is the user subscribed?
is_subscribed = False
id = load_user( current_user.id ).id
if User.query.join(User.projects).filter(Project.name==project_name).filter(User.id==id).all():
is_subscribed = True
is_owner = False
id = load_user( current_user.id ).id
if project_id == id:
is_owner = True
is_owner = True
# Nonexistent project
if not project_name or not project_id:
return redirect(url_for('projects'))
questions = Question.query.filter_by(project=project_id)
return render_template('project_page.html', form=question_form, questions=questions, project_name=project_name,
is_subscribed=is_subscribed, is_owner=is_owner, description=project.description)
@app.route("/projects/<string:project_name>/create-question", methods=['GET'])
@login_required
def create_question_page(project_name=None):
question_form = QuestionForm()
return render_template('create-question-page.html', project_name=project_name, question_form=question_form)
@app.route("/projects/<string:project_name>/create-question", methods=['POST'])
@login_required
def create_question(project_name=None):
if not project_name:
return redirect(url_for('projects'))
# Checks if user is subscribed to project TODO
# Get content JSON
content = request.get_json()
question_form = QuestionForm()
name = content['form']['name']
number = content['form']['number']
description = content['delta']
# Question is ralated to project id
print(project_name)
project_object = Project.query.filter_by(name=project_name).first()
id = project_object.id
question = Question(description=description, name=name, number=number, project=id, sol_number=0)
# Add it into DB
db.session.add(question)
db.session.commit()
return redirect(url_for('project', project_name=project_name))
@app.route('/projects/<string:project_name>/delete-question', methods=['POST'])
@login_required
def delete_question(project_name=None, question_id=None):
if not question_id:
question_id = request.form['question_id']
# Check if question exists
question_object = Question.query.filter_by(id=question_id).first()
if question_object:
# Checks if current user is subscribed or if it is project owner
id = load_user( current_user.id ).id
project_object = Project.query.filter_by(name=project_name).first()
project_user = User.query.join(User.projects).filter(Project.name==project_name).filter(User.id==id).all()
if project_user or project_object.owner == id:
# First delete all questions solutions
solutions = Solution.query.filter_by(question=question_id).all()
for solution in solutions:
delete_solution(project_name=project_name, question_id=question_id, solution_id=solution.id)
db.session.delete(question_object)
db.session.commit()
#else TODO
# colocar notificação de que não foi possível deletar
return redirect(url_for('project', project_name=project_name))
# Project page
@app.route("/projects/<string:project_name>/<int:question_id>", methods=['GET'])
@login_required
def question(project_name=None, question_id=None):
solution_form = SolutionForm()
# Associated question
question = Question.query.filter_by(id=question_id).first()
# Solutions to this question
solutions = Solution.query.filter_by(question=question_id)
return render_template('question-page.html', form=solution_form, question_id=question_id,
project_name=project_name, question=question, solutions=solutions)
@app.route("/projects/<string:project_name>/<int:question_id>/create-solution", methods=['GET'])
@login_required
def create_solution_page(project_name=None, question_id=None):
solution_form = SolutionForm()
question = Question.query.filter_by(id=question_id).first()
return render_template('create-solution-page.html', project_name=project_name, solution_form=solution_form, question=question)
@app.route("/projects/<string:project_name>/<int:question_id>/create-solution", methods=['POST'])
@login_required
def create_solution(project_name=None, question_id=None):
if not project_name or not question_id:
return redirect(url_for('projects'))
# Checks if user is subscribed to project TODO
content = request.get_json()
description = content['delta']
id = load_user( current_user.id ).id
owner = User.query.filter_by(id=id).first()
owner_name = owner.username
# Project is ralated to question_id
solution = Solution(description=description, question=question_id, owner=id, owner_name=owner_name)
# Add it into DB
db.session.add(solution)
# Adds number of solutions
q1 = Question.query.filter_by(id=question_id).first()
q1.sol_number = q1.sol_number + 1
db.session.commit()
return redirect(url_for('question', project_name=project_name, question_id=question_id))
@app.route('/projects/<string:project_name>/<int:question_id>/delete-solution', methods=['POST'])
@login_required
def delete_solution(project_name=None, question_id=None, solution_id=None):
if not solution_id:
solution_id = request.form['question_id']
# Check if solution exists
solution_object = Solution.query.filter_by(id=solution_id).first()
if solution_object:
# Checks if current user owns solution
id = load_user( current_user.id ).id
if solution_object.owner == id:
db.session.delete(solution_object)
q1 = Question.query.filter_by(id=question_id).first()
if q1.sol_number > 0:
q1.sol_number = q1.sol_number - 1
db.session.commit()
#else TODO
# colocar notificação de que não foi possível deletar
return redirect(url_for('project', project_name=project_name))
@app.route('/quill/<string:project_name>/<int:question_id>', methods=['POST'])
@login_required
def test_post_quill(project_name=None, question_id=None):
content = request.get_json()
print(content)
if content:
quill = QuillTest(description=content, p_name=project_name, question_id=question_id)
db.session.add(quill)
db.session.commit()
#else TODO
# colocar notificação de que não foi possível deletar
return redirect(url_for('project'))
@app.route('/quill/<string:project_name>/<int:question_id>', methods=['GET'])
@login_required
def test_get_quill(project_name=None, question_id=None):
quill = QuillTest.query.filter_by(p_name=project_name, question_id=question_id).first()
print(quill.description)
#else TODO
# colocar notificação de que não foi possível deletar
return render_template('show.html', quill=quill)