-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
426 lines (343 loc) · 13.8 KB
/
main.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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
from __future__ import print_function
from flask import Flask
from flask import request, render_template, jsonify, make_response, redirect, url_for
from flask_login import LoginManager, UserMixin, login_required, login_user, logout_user, current_user
from forms import *
from auth import User
from functions import *
'''
TODO:
- Add some dummy content on the home page
- Add search sql query
- Add "mark as read" query
- Apply/post job queries
- Error in registration: IntegrityError: 1062 (23000): Duplicate entry 'U1230' for key 'PRIMARY'
'''
app = Flask(__name__)
app.config.update(dict(
SECRET_KEY="laks;dfjaslk;fjsal;kfjsalfja;lskdfj",
WTF_CSRF_SECRET_KEY="ofhuio2h3r283ubrqlb32iuob4ir"
))
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
@login_manager.user_loader
def load_user_from_id(id):
try:
return User.from_id(id, type='student' if id.startswith('U') else 'company')
except:
return None
@app.context_processor
def add_global_template_vars():
notifications = add_notification(current_user.id) if not current_user.is_anonymous else None
return dict(
search_form=SearchForm(query=request.args.get('query')), # add search form for every page
notifications=notifications
)
def student_login_required(func):
pass
'''
Page Routes
'''
@app.route('/')
def index():
return render_template('pages/index.j2')
@app.route('/search')
def search():
query = request.args.get('query', '')
if query:
students = search_student(query)
jobs = search_jobs(query)
company = search_company(query)# if current_user.type != 'company' else []
else:
students, company, jobs = [], [], []
print(students, company)
return render_template('pages/search.j2', students=students, jobs=jobs, companies=company, title='Search')
##### You can remove this once the messages query is working. it's just for creating fake messages
from datetime import datetime, timedelta
#Ben
# @app.route('/student/<user>')
# def student(user):
# student_data = stud(user) # get info for a student
# friends = listfriends(student_data['sid'])
# friend_ids = [f['sid'] for f in friends]
# if student_data['privacysetting'] == 'friendly public' and (not current_user.is_authenticated or current_user.id in friend_ids):
# return redirect(url_for('not_found_error'))
#
# following = listfollowers_user(student_data['sid'])
# applications = listapplications(student_data['sid'])
#
# messages = getmessages(current_user.id, student_data['sid']) if current_user.is_authenticated else None
# friend_request = get_friend_request(current_user.id, student_data['sid']) if current_user.is_authenticated else None
# return render_template('pages/student.j2', user=student_data, following=following, friends=friends,
# applications=applications, messages=messages, friend_request=friend_request, title=student_data['sname'])
@app.route('/student/<user>')
def student(user):
student_data = stud(user) # get info for a student
following = listfollowers_user(student_data['sid'])
friends = listfriends(student_data['sid'])
applications = listapplications(student_data['sid'])
print(applications)
print(student_data['privacysetting'])
print(student_data['sid'])
print(friends)
# messages = getmessages(current_user.id, student_data['sid'])
messages = getmessages(current_user.id, student_data['sid']) if current_user.is_authenticated else None
print(messages)
return render_template('pages/student.j2', user=student_data, following=following, friends=friends, applications=applications, messages=messages, title=student_data['sname'])
# @app.route('/company/<user>')
# def company(user):
# company_data = com(user)
# followers = listfollowers_company(user)
# jobs = comjobs(company_data['cid'])
# applications_com = listapplications_com(company_data['cid'])
# im_following = is_following(current_user.id, company_data['cid']) if current_user.is_authenticated else None
# return render_template('pages/company.j2', company=company_data, followers=followers,
# jobs=jobs, applications_com=applications_com, im_following=im_following, title=company_data['cname'])
@app.route('/company/<user>')
def company(user):
company_data = com(user)
followers = listfollowers_company(company_data['cid'])
jobs = comjobs(company_data['cid'])
applications_com = listapplications_com(company_data['cid'])
print(company_data['cname'])
print('123')
print(user)
print(followers)
return render_template('pages/company.j2', company=company_data, followers=followers, jobs=jobs, applications_com=applications_com, title=company_data['cname'])
@app.route('/job/<aid>', methods=['GET', 'POST'])
def job(aid):
job_data = selectjob(aid)
sent_application = has_applied(aid, current_user.id) if current_user.is_authenticated else None
print(sent_application)
form = ApplicationForm(request.form)
if request.method == 'POST' and form.validate() and current_user.is_authenticated:
sendapplication(aid, current_user.id, form.email_phone.data)
return redirect(url_for('student', user=current_user.username))
return render_template('pages/job.j2', job=job_data, aid=aid, form=form, title=job_data['title'])
# @app.route('/notifications')
# @login_required
# def notifications():
# Noti_data = add_notification(current_user.id)
# return render_template('pages/notification.j2', notifs=Noti_data, user=current_user, title='Notifications')
@app.route('/notifications')
@login_required
def notifications():
Noti_data = add_notification(current_user.id)
Re_data = add_request(current_user.id)
return render_template('pages/notification.j2', notifs=Noti_data, requests = Re_data, user=current_user, title='Notifications')
'''
Asynchronous Methods
'''
@app.route('/notification/<aid>/<sid>/mark_read', methods=['POST'])
@login_required
def mark_read(aid, sid):
status = read_notification(aid, sid) # mark notification as read
return jsonify({
'success': status
})
@app.route('/company/<user>/follow', methods=['POST'])
@login_required
def follow(user):
status = follow_com(user, current_user.id)
return jsonify({
'success': status
})
@app.route('/student/<user>/befriend', methods=['POST'])
@login_required
def friend_request(user):
status = send_friend_request(current_user.id, user)
return jsonify({
'success': status
})
@app.route('/student/<user>/accfri',methods = ['POST'])
@login_required
def acc_frirequest(user):
Noti_data = add_notification(current_user.id)
Re_data = add_request(current_user.id)
if request.method == 'POST':
accept_friend_request(user,current_user.id)
print(user)
print(current_user.id)
print(datetime.now())
return render_template('pages/notification.j2', notifs=Noti_data, requests = Re_data, user=current_user, title='Notifications')
#
@app.route('/student/<user>/rejfri',methods = ['POST'])
@login_required
def Rej_frirequest(user):
Noti_data = add_notification(current_user.id)
Re_data = add_request(current_user.id)
if request.method == 'POST':
reject_friend_request(user,current_user.id)
print(user)
print('123')
print(current_user.id)
print(datetime.now())
return render_template('pages/notification.j2', notifs=Noti_data, requests = Re_data, user=current_user, title='Notifications')
@app.route('/apply/<aid>/<contact_by>', methods=['POST'])
@login_required
def apply(aid, contact_by):
status = sendapplication(aid, current_user.id, contact_by)
return jsonify({
'success': status
})
@app.route('/message/<sid>/send', methods=['POST'])
@login_required
def send_message(sid):
status = False
timestamp = datetime.now()
message = request.form.get('message')
if message:
status = sendmessage(current_user.id, sid, message, timestamp)
return jsonify({
'success': status,
'timestamp': timestamp.strftime('%Y-%m-%d %H:%M:%S')
})
@app.route('/messages/<sid>/get')
@login_required
def get_messages(sid):
date = request.args.get('date')
messages = getnewmessages(sid, current_user.id, date)
return jsonify({
# 'success': status,
'messages': messages
})
@app.route('/messages/<sid>/all')
@login_required
def get_all_messages(sid):
messages = getmessages(sid, current_user.id)
return jsonify({
# 'success': status,
'messages': messages
})
@app.route('/message/<sid>/read/<date>', methods=['POST'])
@login_required
def read_message(sid, date):
status = markreadmessage(current_user.id, sid, date)
return jsonify({
'success': status
})
'''
Form Pages
'''
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm(request.form)
if request.method == 'POST' and form.validate():
user = User.from_form(form)
print(user)
if user:
if user.type == 'company':
login_user(user, remember=form.remember.data)
print(current_user)
return redirect(url_for('company', user=user.username))
elif user.type == 'student':
login_user(user, remember=form.remember.data)
return redirect(url_for('student', user=user.username))
return render_template('form/login.j2', form=form, title='Login')
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegistrationForm(request.form)
if request.method == 'POST' and form.validate():
if form.act_type.data == 'company':
companyregister(form.name.data, form.password.data, form.username.data)
user = User.from_form(form)
if user:
login_user(user)
print('logged in company', user.username)
return redirect(url_for('company', user=user.username))
elif form.act_type.data == 'student':
studentregister(form.name.data, form.password.data, form.username.data)
user = User.from_form(form)
if user:
login_user(user)
print('logged in user', user.username)
return redirect(url_for('student', user=user.username))
return render_template('form/register.j2', form=form, title = 'Register')
@app.route('/student/<user>/updateprofile', methods=['GET', 'POST'])
@login_required
def student_edit(user):
form = UpdateForm(request.form)
student_data = stud(user) #use loginname
print(current_user.id)
if request.method == 'POST' and form.validate():
print('123')
# phone,email,university,GPA,major,interests,qualification,sid
updateprofile(form.phone.data, form.email.data, form.university.data, form.GPA.data, form.major.data,
form.interest.data, form.qualifications.data, form.privacysetting.data, current_user.id)
print(current_user.id)
print(form.email.data)
return redirect(url_for('student', user=current_user.username))
return render_template('form/student_update.j2', form=form, user=student_data, title='{}|Update Profile'.format(student_data['sname']))
#func: updateprofile_com
@app.route('/company/<user>/updateprofile', methods=['GET', 'POST'])
@login_required
def company_edit(user):
form = UpdateForm_com(request.form)
company_data = com(user) #use loginname
print(current_user.id)
if request.method == 'POST' and form.validate():
print('123')
updateprofile_com(form.location.data, form.industry.data, current_user.id)
print(current_user.id)
print(form.location.data)
return redirect(url_for('company', user=company_data['username']))
return render_template('form/company_update.j2', form=form, user=company_data)
@app.route('/post_job/<user>', methods=['GET', 'POST'])
@login_required
def post_job(user):
form = PostJob(request.form)
company_data = com_from_id(user) # get info for a company
if request.method == 'POST' and form.validate():
postjobs(current_user.id,form.joblocation.data,form.title.data,form.salary.data, form.bk.data, form.description.data)
return redirect(url_for('company', user=company_data['username']))
print(company_data['location'])
print("99")
return render_template('form/job_posting.j2', form=form, user=company_data)
@app.route('/candidate/<user>/<job>')
def candidate(user,job):
candidate_data = stud(user) # get info for a student
# print(candidate_data['sid'])
following = listfollowers_user(candidate_data['sid'])
friends = listfriends(candidate_data['sid'])
job = job
print(user)
print(candidate_data)
# print(candidate_data['sid'])
# print(friends)
return render_template('pages/candidate.j2', user=candidate_data, job = job, friends = friends, following = following)
@app.route('/Application_suc/<user>/<job>', methods=['POST'])
@login_required
def decision(user,job):
status = Application_suc(user, job)
print(user)
print(job)
return jsonify({
'success': status
})
@app.route('/Application_fail/<user>/<job>', methods=['POST'])
@login_required
def decision_fail(user,job):
status = Application_fail(user, job)
print(user)
print(job)
return jsonify({
'success': status
})
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('index'))
@app.errorhandler(500)
def internal_error(error):
return render_template('layouts/error.j2',
error_code=500, title='Internal Server Error',
message="It's our bad. Sorry! :|"), 500
@app.errorhandler(404)
def not_found_error(error):
return render_template('layouts/error.j2',
error_code=404, title='Page Not Found',
message="I think you're lost. That sucks."), 404
if __name__ == '__main__':
app.run(debug=True, port=5003)