-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
382 lines (311 loc) · 11.3 KB
/
app.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
import datetime
import json
from flask import Flask, make_response, request, url_for, jsonify, render_template, request, redirect, session , escape, g
import MySQLdb
from flask.ext.httpauth import HTTPBasicAuth
import os
from flask.ext.sqlalchemy import SQLAlchemy
from werkzeug import generate_password_hash, check_password_hash
from werkzeug import BaseResponse
from sqlalchemy import create_engine
from sqlalchemy import text
from functools import wraps
from Models import db
from Models import *
from flask.ext.login import login_user, logout_user, current_user, \
login_required, LoginManager
UPLOAD_FOLDER = '/home/shubham/Desktop/web_development/tutplus/data/user_dp/'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = "A0Zr98j/3yX R~XHH!jmN]LWX/,?RT"
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://13CS30030:cse12@10.5.18.68/13CS30030"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db.init_app(app)
login_manager = LoginManager()
login_manager.session_protection = "strong"
# login_serializer = URLSafeTimedSerializer(app.secret_key)
login_manager.init_app(app)
with app.app_context():
db.create_all()
@app.before_request
def before_request():
g.user = current_user
@login_manager.user_loader
def user_loader(user_id):
"""Given *user_id*, return the associated User object.
:param unicode user_id: user_id (email) user to retrieve
"""
return User.query.get(int(user_id))
def template_or_json(template=None):
""""Return a dict from your view and this will either
pass it to a template or render json. Use like:
@template_or_json('template.html')
"""
def decorated(f):
@wraps(f)
def decorated_fn(*args, **kwargs):
ctx = f(*args, **kwargs)
if request.is_xhr or not template:
return jsonify(ctx)
else:
return render_template(template, **ctx)
return decorated_fn
return decorated
# Home API
@app.route("/" , methods = ['GET', 'POST'])
def home():
return render_template('login.html')
# API for login
@app.route('/login', methods = ['GET', 'POST'])
def login():
if request.method == 'POST':
json_data = request.get_json(force=True)
if not json_data:
print("error")
return redirect(url_for('login'))
email_ = json_data['email']
pwd = json_data['password']
print email_, pwd
if g.user is not None and g.user.is_authenticated:
print "shouldnt hppen"
return redirect(url_for('profile'))
user = User.query.filter_by(email = email_).first()
print "IN LOGIN"
if user and user.check_password(pwd):
# session['email'] = email_
session['user_id'] = user.user_id
USERID = user.user_id
session.modified = True
print "In profile redirect"
return redirect(url_for('profile'))
else:
print "IN login wala !! "
return redirect(url_for('login'))
if request.method == 'GET':
print "get h"
return render_template('login.html')
@app.route('/student', methods = ['GET'])
def student():
return render_template('student.html')
@app.route('/signUp', methods = ['GET','POST'])
def add_user():
if request.method == 'POST':
json_data = request.get_json(force=True)
if not json_data:
print("error")
return redirect(url_for('add_user'))
name = json_data['name']
email = json_data['email']
pwd = json_data['password']
DOB = str(json_data['dob'])
print "*****" + pwd
link = "link"
flag = json_data['flag']
newuser = User(name, email, pwd, link, flag , DOB)
session['email'] = email
db.session.add(newuser)
session['user_id'] = name
db.session.commit()
user = User.query.filter_by(email = (session['email'])).first()
if flag == 2:
newfac = Faculty(user.user_id)
db.session.add(newfac)
db.session.commit()
elif flag == 0:
newadm = Admin(user.user_id)
db.session.add(newadm)
db.session.commit()
elif flag == 1:
newst = Student(user.user_id)
db.session.add(newst)
db.session.commit()
return redirect(url_for('profile'), 302)
if request.method == 'GET':
return render_template('signup.html')
@app.route('/parentPortal', methods = ['GET' , 'POST'])
def parentPortal():
if request.method == 'POST' :
json_data = request.get_json(force = True)
if not json_data :
print ("Error !! No credentials Given !! ")
return redirect(url_for('parentPortal'))
rollno = json_data['RollNo']
_email = json_data['Email']
user = User.query.filter_by(user_id = rollno)
if user is None or user.email != _email :
print("Sorry Wrong Credentials !! ")
return redirect(url_for('parentPortal'))
else:
Performance = Performance_Sheet.query.filter_by(Student_Id = rollno)
return render_template('Parent_Portal.html')
@app.route('/forgotPassword', methods = ['GET' , 'POST'])
def forgotPassword():
if request.method == 'POST' :j
json_data = request.get_json(force = True)
if not json_data :
print ("Error !! No credentials Given !! ")
return redirect(url_for('forgotPassword'))
rollno = json_data['RollNo']
_email = json_data['Email']
user = User.query.filter_by(user_id = rollno)
if user is None or user.email != _email :
print("Sorry Wrong Credentials !! ")
return redirect(url_for('forgotPassword'))
else:
password = user.password
context = {
'Password' : password
}
return context
@app.route('/profile', methods=['GET', 'POST'])
@template_or_json('profile.html')
def profile():
if g is None:
return redirect(url_for('signup'))
else:
return ({'redirect': 'True', 'link' : '/profile'})
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('home'))
# API for searching a course
@app.route('/searchresults', methods = ['GET','POST'])
def search_course():
if request.method == 'POST':
json_data = request.get_json(force=True)
if not json_data:
print("error")
return redirect(url_for('search_course'))
cid = json_data['course_id']
course = Course.query.filter_by(course_id = cid).first()
if course:
session['course_id'] = cid
return redirect(url_for('course_home'))
else:
return redirect(url_for('search_course'))
# session['email'] = email
if request.method == 'GET':
return render_template('student_home.html')
# API for enrolling a student in a course
@app.route('/enroll')
def enroll():
newenroll = Enrolls(session['user_id'],session['course_id'])
db.session.add(newenroll)
db.session.commit()
return redirect(url_for('course_home'), 302)
# API to add a new notice
@app.route('/addnotice', methods = ['GET','POST'])
def add_notice():
if request.method == 'POST':
json_data = request.get_json(force=True)
if not json_data:
print("error")
return redirect(url_for('search_course'))
cid = json_data['c_id']
msg = json_data['message']
newnotice = Notice(cid,msg)
db.session.add(newnotice)
db.session.commit()
return redirect(url_for('course_home'), 302)
# API to add a new course
@app.route('/addcourse', methods = ['GET','POST'])
def add_course():
if request.method == 'POST':
json_data = request.get_json(force=True)
if not json_data:
print("error")
return redirect(url_for('search_course'))
cid = json_data['c_id']
cname = json_data['course_name']
pre = json_data['prereq']
fac_id = json_data['faculty_id']
course = Course.query.filter_by(course_id = cid).first()
if course:
perror("error")
return redirect(url_for('add_course'))
newcourse = Course(cid,cname,pre,fac_id)
db.session.add(newcourse)
db.session.commit()
return redirect(url_for('faculty_home'), 302)
# API to approve a course
@app.route('/approve', methods = ['GET','POST'])
def approve():
if request.method == 'POST':
json_data = request.get_json(force=True)
if not json_data:
print("error")
return redirect(url_for('approve'))
cid = json_data['c_id']
course = Course.query.filter_by(course_id = cid).first()
if course:
course.approve()
return redirect(url_for('course_home'))
else:
perror("error")
return redirect(url_for('error'))
# API to get list of all courses
@app.route('/allcourses')
def getAllCourses():
j = Course.query.all()
d = jsonify(json_data = [i.serialize for i in j])
return d
# API to get list of all courses of a faculty
@app.route('/facultycourses')
def getFacultyCourses():
return jsonify(json_data = [i.serialize for i in Course.query.filter_by(faculty = session['user_id']).all()])
# API to get all notices
@app.route('/allnotices')
def getAllNotices():
for i in Notice.query.all():
print i.serialize
return jsonify(json_data = [i.serialize for i in Notice.query.all()])
# API to get all notices of a course
@app.route('/allcoursenotices')
def getCourseNotices():
return jsonify(json_data = [i.serialize for i in Notice.query.filter_by(c_id = session['course_id']).all()])
# API to get all notices of a student
@app.route('/allstudentnotices')
def getStudentNotices():
enrolled_courses = []
for c in Enrolls.query.filter_by(student_id = session['user_id']).all():
p = c.course_id
enrolled_courses.append(p)
d = jsonify(json_data = [i.serialize for i in Notice.query.all() if i.c_id in enrolled_courses])
return d
# API for listing
@app.route('/listcourses', methods = ['GET'])
def list_course():
return render_template('course_list.html')
# API for adding a lecture
@app.route('/addLecture', methods = ['GET' , 'POST'])
def addLecture():
if request.method == 'POST' :
json_data = result.get_json(force = True)
# course_id = json_data['Course_Id']
course_id = session['course_id']
notes = json_data['Notes']
Date_Time = json_data['Date_Time']
Topic = json_data['Topic']
Link = json_data['Link']
# course_id = 1
# notes = "abc"
# Date_Time = "2012-12-12"
# Topic = "topic"
# link = "link"
NewLec = Lecture(course_id, Date_Time, Topic)
NewLec.setNotes(notes)
NewLec.setLink(Link)
db.session.add(NewLec)
Lecture_Id= NewLec.Lecture_Id
Questions = json_data['Questions']
Answers = json_data['Answers']
# Questions = "q"
# Answers = "a"
NewTest = Test(Lecture_Id, Questions, Answers)
db.session.add(NewTest)
db.session.commit()
return redirect(url_for('course_home'))
if __name__ == "__main__":
app.secret_key = "shubham12345"
app.run(host="0.0.0.0", port = 5000, debug=True, threaded=True)