-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
358 lines (307 loc) · 15.2 KB
/
main.py
File metadata and controls
358 lines (307 loc) · 15.2 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
from app_init import app, db
from models import User, Project, Task, ProjectComment, TaskComment
from forms import LoginForm, RegistrationForm, AccountForm, DeleteAccountForm
from flask import session, render_template, request, redirect, url_for, flash
from datetime import datetime
#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
# Ensure the database tables are created
with app.app_context():
db.create_all()
if not hasattr(Task, 'completed'):
with db.engine.connect() as conn:
conn.execute('ALTER TABLE task ADD COLUMN completed BOOLEAN DEFAULT FALSE')
#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
@app.route("/projects", methods=['GET', 'POST'])
def projects():
if 'username' not in session:
flash('You must be logged in to view this page.')
return redirect(url_for('login'))
user = User.query.filter_by(username=session['username']).first()
if user is None:
flash('User not found.')
return redirect(url_for('logout'))
if request.method == 'POST':
project_name = request.form.get('project_name')
project_description = request.form.get('project_description')
project_deadline_str = request.form.get('project_deadline')
start_date = datetime.now()
if project_name and project_description and user and start_date and project_deadline_str:
project_deadline = datetime.strptime(project_deadline_str, '%Y-%m-%dT%H:%M')
new_project = Project(name=project_name, description=project_description, owner=user, start_date=start_date, deadline=project_deadline) # Include start date and deadline in project creation
new_project.participants.append(user)
new_project.owner_id = user.id
db.session.add(new_project)
db.session.commit()
flash('Project created successfully!')
return redirect(url_for('projects'))
projects = user.participated_projects.all()
return render_template('projects.html', projects=projects, user=user)
#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
@app.route("/project/<int:project_id>", methods=['GET', 'POST'])
def project(project_id):
if 'username' not in session:
flash('You must be logged in to view this page.')
return redirect(url_for('login'))
user = User.query.filter_by(username=session['username']).first()
if user is None:
flash('User not found.')
return redirect(url_for('logout'))
project = Project.query.get_or_404(project_id)
if request.method == 'POST':
if 'task_title' in request.form:
task_title = request.form.get('task_title')
task_description = request.form.get('task_description')
task_deadline_str = request.form.get('task_deadline')
task_deadline = datetime.strptime(task_deadline_str, '%Y-%m-%dT%H:%M')
new_task = Task(title=task_title, description=task_description, deadline=task_deadline, project=project)
db.session.add(new_task)
db.session.commit()
flash('Task added successfully!')
return redirect(url_for('project', project_id=project_id))
elif 'participant_username' in request.form:
participant_username = request.form.get('participant_username')
participant = User.query.filter_by(username=participant_username).first()
if participant:
if participant not in project.participants:
project.participants.append(participant)
db.session.commit()
flash('Participant added successfully!')
else:
flash('Participant is already associated with the project.')
else:
flash('Invalid username. Please try again.')
return redirect(url_for('project', project_id=project_id))
elif 'remove_participant_id' in request.form:
participant_id = request.form.get('remove_participant_id')
participant = User.query.get(participant_id)
if participant and participant in project.participants:
project.participants.remove(participant)
db.session.commit()
flash('Participant removed successfully!')
else:
flash('Participant not found in the project.')
return redirect(url_for('project', project_id=project_id))
elif 'comment_content' in request.form:
comment_content = request.form.get('comment_content')
new_comment = ProjectComment(content=comment_content, user=user, project=project)
db.session.add(new_comment)
db.session.commit()
flash('Comment added successfully!')
return redirect(url_for('project', project_id=project_id))
comments = ProjectComment.query.filter_by(project_id=project_id).order_by(ProjectComment.date_posted.desc()).all()
now = datetime.now()
participants = project.participants.all()
return render_template('project.html', project=project, user=user, tasks=project.tasks, comments=comments, now=now, participants=participants)
#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
@app.route("/delete_project_comment/<int:comment_id>", methods=['POST'])
def delete_project_comment(comment_id):
comment = ProjectComment.query.get_or_404(comment_id)
if 'username' not in session or comment.user.username != session['username']:
flash('You do not have permission to delete this comment.')
return redirect(url_for('home_route'))
db.session.delete(comment)
db.session.commit()
flash('Comment deleted successfully!')
if comment.project_id:
return redirect(url_for('project', project_id=comment.project_id))
#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
@app.route("/delete_task_comment/<int:comment_id>", methods=['POST'])
def delete_task_comment(comment_id):
comment = TaskComment.query.get_or_404(comment_id)
if 'username' not in session or comment.user.username != session['username']:
flash('You do not have permission to delete this comment.')
return redirect(url_for('home_route'))
db.session.delete(comment)
db.session.commit()
flash('Comment deleted successfully!')
if comment.task_id:
return redirect(url_for('task', task_id=comment.task_id))
return redirect(url_for('home_route'))
#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
@app.route("/task/<int:task_id>", methods=['GET', 'POST'])
def task(task_id):
task = Task.query.get_or_404(task_id)
user = User.query.filter_by(username=session['username']).first()
if request.method == 'POST':
if 'comment_content' in request.form:
comment_content = request.form.get('comment_content')
new_comment = TaskComment(content=comment_content, user=user, task=task)
db.session.add(new_comment)
db.session.commit()
flash('Comment added successfully!')
status = request.form.get('status')
if status:
task.status = status
task.completed = (status == 'Completed')
db.session.commit()
assignee_username = request.form.get('assignee_username[]')
if assignee_username:
assignee = User.query.filter_by(username=assignee_username).first()
if assignee:
if assignee in task.project.participants:
task.assignee = assignee
db.session.commit()
flash('Assignee added successfully!')
else:
flash('Assignee does not have access to the project.')
else:
flash('Invalid username for assignee. Please try again.')
remove_assignee = request.form.get('remove_assignee')
if remove_assignee:
if task.assignee:
task.assignee = None
db.session.commit()
flash('Assignee removed successfully!')
else:
flash('No assignee to remove.')
return redirect(url_for('task', task_id=task.id))
comments = TaskComment.query.filter_by(task_id=task_id).order_by(TaskComment.date_posted.desc()).all()
return render_template('task.html', task=task, comments=comments)
#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
@app.route("/account", methods=['GET', 'POST'])
def account():
if 'username' not in session:
flash('You must be logged in to view this page.')
return redirect(url_for('login'))
form = AccountForm()
user = User.query.filter_by(username=session['username']).first()
if user is None:
flash('User not found.')
return redirect(url_for('logout'))
if form.validate_on_submit():
if user.check_password(form.current_password.data):
if form.username.data:
user.username = form.username.data
if form.email.data:
user.email = form.email.data
if form.new_password.data:
user.set_password(form.new_password.data)
if form.submit_delete_account.data:
db.session.delete(user)
session.pop('username', None)
flash('Your account has been deleted.')
return redirect(url_for('home_route'))
db.session.commit()
flash('Account information updated successfully.')
return redirect(url_for('account'))
else:
flash('Invalid current password. Please try again.')
return render_template('account.html', title='Account Settings', form=form, user=user)
#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
@app.route("/update_username", methods=['POST'])
def update_username():
if 'username' not in session:
flash('You must be logged in to perform this action.')
return redirect(url_for('login'))
form = AccountForm()
user = User.query.filter_by(username=session['username']).first()
if user is None:
flash('User not found.')
return redirect(url_for('logout'))
if form.validate_on_submit():
user.username = form.username.data
db.session.commit()
flash('Username updated successfully!')
else:
flash('Invalid username. Please try again.')
return redirect(url_for('account'))
#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
@app.route("/update_email", methods=['POST'])
def update_email():
if 'username' not in session:
flash('You must be logged in to perform this action.')
return redirect(url_for('login'))
form = AccountForm()
user = User.query.filter_by(username=session['username']).first()
if user is None:
flash('User not found.')
return redirect(url_for('logout'))
if form.validate_on_submit():
if user.check_password(form.current_password.data):
user.email = form.email.data
db.session.commit()
flash('Email updated successfully.')
return redirect(url_for('account'))
else:
flash('Invalid current password. Please try again.')
return redirect(url_for('account'))
#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
@app.route("/update_password", methods=['POST'])
def update_password():
if 'username' not in session:
flash('You must be logged in to perform this action.')
return redirect(url_for('login'))
form = AccountForm()
user = User.query.filter_by(username=session['username']).first()
if user is None:
flash('User not found.')
return redirect(url_for('logout'))
if form.validate_on_submit():
if user.check_password(form.current_password.data):
user.set_password(form.new_password.data)
db.session.commit()
flash('Password updated successfully.')
return redirect(url_for('account'))
else:
flash('Invalid current password. Please try again.')
return redirect(url_for('account'))
#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
@app.route("/delete_account", methods=['POST'])
def delete_account():
if 'username' not in session:
flash('You must be logged in to perform this action.')
return redirect(url_for('login'))
form = DeleteAccountForm()
user = User.query.filter_by(username=session['username']).first()
if user is None:
flash('User not found.')
return redirect(url_for('logout'))
if form.validate_on_submit():
if user and user.check_password(form.delete_account_password.data):
db.session.delete(user)
db.session.commit()
session.pop('username', None)
flash('Your account has been deleted.')
return redirect(url_for('home_route'))
else:
flash('Invalid password. Please try again.')
return redirect(url_for('account'))
#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
@app.route("/register", methods=['GET', 'POST'])
def register():
form = RegistrationForm()
if form.validate_on_submit():
user = User(username=form.username.data, email=form.email.data)
user.set_password(form.password.data)
db.session.add(user)
db.session.commit()
flash('Congratulations, you are now a registered user!')
return redirect(url_for('login'))
return render_template('register.html', title='Register', form=form)
#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
@app.route("/login", methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter((User.username == form.username.data) | (User.email == form.username.data)).first()
if user is None or not user.check_password(form.password.data):
flash('Invalid username or password')
return render_template('login.html', title='Sign In', form=form, error='Invalid username or password')
session['username'] = user.username
flash('You have been logged in!')
return redirect(url_for('home_route'))
return render_template('login.html', title='Sign In', form=form)
#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
@app.route("/logout")
def logout():
session.pop('username', None)
flash('You have been logged out.')
return redirect(url_for('home_route'))
#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
@app.route("/")
def home_route():
return render_template("home.html")
#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
if __name__ == "__main__":
from wsgi import initialize
initialize()