-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlearnville_new.py
277 lines (257 loc) · 11.6 KB
/
learnville_new.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
from flask import Flask, render_template, redirect, url_for, request, session
from flask_mysqldb import MySQL
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'learnzilla'
mysql = MySQL(app)
app.secret_key = 'S66D0HrapD9c8rGpBoGR'
@app.route("/")
def home():
return render_template('js.html')
@app.route("/register", methods = ["GET","POST"])
def register():
return render_template('register.html')
@app.route('/login', methods = ["GET","POST"])
def login():
if request.method == 'POST':
userdetails=request.form
name = userdetails['username']
password = userdetails['password']
cur = mysql.connection.cursor()
result = cur.execute("select username,password from student")
all_users = cur.fetchall()
if (name,password) in all_users:
session['username'] = name
session['type'] = 'student'
return redirect(url_for('dashboard'))
else:
result = cur.execute("select username,password from teacher")
all_users = cur.fetchall()
if (name,password) in all_users:
session['username'] = name
session['type'] = 'teacher'
return redirect(url_for('dashboard'))
else:
return 'Incorrect Username or Password'
else:
return redirect(url_for('register'))
@app.route('/choice', methods = ["GET","POST"])
def choice():
if request.method == 'POST':
signupdetails=request.form
fullname = signupdetails["fullname"]
email = signupdetails["emailid"]
return render_template('profile.html', fullname=fullname,email=email)
else:
return redirect(url_for('register'))
@app.route('/signup/<atype>/<fullname>/<email>', methods = ["GET","POST"])
def signup(atype, fullname, email):
if request.method == 'POST':
finaldetails=request.form
username = finaldetails["username"]
password = finaldetails["pass"]
conpass = finaldetails["conpass"]
institution = finaldetails["institution"]
date = finaldetails["date"]
bio = finaldetails["bio"]
cur = mysql.connection.cursor()
result = cur.execute("select username from teacher union select username from student")
usernames = cur.fetchall()
ok = True
for u in usernames:
if u[0] == username:
ok = False
break
if ok:
if password == conpass:
cur = mysql.connection.cursor()
cur.execute("insert into %s values('%s','%s','%s','%s','%s','%s','%s')" %(atype, username,fullname,email,password,institution,date,bio))
mysql.connection.commit()
cur.close()
return redirect(url_for('register'))
else:
return "The passwords do not match"
else:
return "Username has been taken"
else:
return render_template('SignUp2.html')
@app.route("/dashboard")
def dashboard():
if "username" in session:
if session['type'] == 'teacher':
cur = mysql.connection.cursor()
result = cur.execute("select * from course where taught_by='%s'" %(session['username']))
all_courses = cur.fetchall()
return render_template('home.html', all_courses = all_courses,stype='teacher')
elif session['type'] == 'student':
cur = mysql.connection.cursor()
result = cur.execute("select c.* from used_courses u,course c where u.courseid=c.courseid and u.username='%s'" %(session['username']))
all_courses = cur.fetchall()
return render_template('home.html', all_courses = all_courses,stype='student')
else:
return redirect(url_for('register'))
@app.route("/addcourse", methods=['GET','POST'])
def addcourse():
if "username" in session:
if request.method=='POST':
cur = mysql.connection.cursor()
result = cur.execute("select max(courseid) from course")
fetch = cur.fetchall()
if fetch==((None,),):
new_course_id='00000'
else:
new_course_id = str(int(fetch[0][0])+1).rjust(5,'0')
newcdetails = request.form
cname=newcdetails['cname']
subt=newcdetails['subt']
cur = mysql.connection.cursor()
cur.execute("insert into course values('%s','%s','%s',null,'%s',null)" %(new_course_id,cname,subt,session['username']))
mysql.connection.commit()
cur.close()
return redirect(url_for('course',course_id=new_course_id))
else:
return render_template('addcourse.html')
else:
return redirect(url_for('register'))
@app.route("/allcourses")
def allcourses():
if "username" in session:
cur = mysql.connection.cursor()
result = cur.execute("select * from course")
all_courses = cur.fetchall()
cur = mysql.connection.cursor()
result = cur.execute("select courseid from used_courses where username='%s'" %(session['username']))
selected_courses = cur.fetchall()
return render_template('allcourses.html', all_courses = all_courses,stype=session['type'], selected_courses=selected_courses)
else:
return redirect(url_for('register'))
@app.route("/pincourse/<course_id>")
def pincourse(course_id):
if "username" in session:
cur = mysql.connection.cursor()
cur.execute("insert into used_courses values('%s','%s',null)" %(session["username"],course_id))
mysql.connection.commit()
cur.close()
return redirect(url_for('allcourses'))
else:
return redirect(url_for('register'))
@app.route("/unpincourse/<course_id>/<where_from>")
def unpincourse(course_id,where_from):
if "username" in session:
cur = mysql.connection.cursor()
cur.execute("delete from used_courses where username='%s' and courseid='%s';" %(session["username"],course_id))
mysql.connection.commit()
cur.close()
if where_from=='dash':
return redirect(url_for('dashboard'))
else:
return redirect(url_for('allcourses'))
else:
return redirect(url_for('register'))
@app.route("/profile")
def profile():
if "username" in session:
cur = mysql.connection.cursor()
result = cur.execute("select * from %s where username = '%s'" %(session['type'],session['username']))
user_profile = cur.fetchall()
return render_template('profile_details.html', user_profile=user_profile, type = session['type'])
else:
return redirect(url_for('register'))
@app.route('/choose')
def choose():
return render_template('profile.html')
@app.route("/course/<course_id>")
def course(course_id):
if "username" in session:
cur = mysql.connection.cursor()
result = cur.execute("select problemid,problem_name from problems where course='%s'" %(course_id))
all_problems = cur.fetchall()
result = cur.execute("select name from course where courseid='%s'" %(course_id))
course_name = cur.fetchall()[0][0]
return render_template('course.html', all_problems=all_problems, course_id=course_id, course_name=course_name,stype=session['type'])
else:
return redirect(url_for('register'))
@app.route("/course/<course_id>/<problem_id>", methods = ["GET","POST"])
def problem(course_id,problem_id):
solved = False
if "username" in session:
if session['type']=='student':
cur = mysql.connection.cursor()
result = cur.execute("select s.problemid from solved_problems s, problems p where s.problemid=p.problemid and username='%s' and course='%s'" %(session['username'],course_id))
solvedproblems=cur.fetchall()
if (problem_id,) in solvedproblems:
solved = True
if request.method=='POST' and session['type']=='student':
cur = mysql.connection.cursor()
result = cur.execute("select solution from problems where problemid='%s'" %(problem_id))
solution = cur.fetchall()[0][0]
submission = request.form
answer = submission['answer']
if solution==answer:
cur = mysql.connection.cursor()
cur.execute("insert into solved_problems values('%s','%s')" %(session['username'],problem_id))
mysql.connection.commit()
cur.close()
return redirect(url_for('problem',course_id=course_id,problem_id=problem_id))
else:
return 'Wrong Answer'
else:
cur = mysql.connection.cursor()
result = cur.execute("select problemid,problem_name from problems where course='%s'" %(course_id))
all_problems = cur.fetchall()
result = cur.execute("select * from problems where problemid='%s'" %(problem_id))
current_problem = cur.fetchall()
if current_problem[0][0]!=(all_problems[len(all_problems) - 1][0]):
next_Problem=all_problems[all_problems.index((current_problem[0][0],current_problem[0][4]))+1][0]
else:
next_Problem=0
return render_template('Ws.html',all_problems=all_problems,current_problem=current_problem,atype=session['type'], solved=solved, next_Problem=next_Problem)
else:
return redirect(url_for('register'))
@app.route("/deleteproblem/<course_id>/<problem_id>")
def deleteproblem(course_id,problem_id):
cur = mysql.connection.cursor()
cur.execute("delete from solved_problems where problemid='%s'" %(problem_id))
mysql.connection.commit()
cur.execute("delete from problems where problemid='%s'" %(problem_id))
mysql.connection.commit()
cur.close()
return redirect(url_for('course',course_id=course_id))
@app.route("/addproblem/<course_id>", methods = ["GET","POST"])
def addproblem(course_id):
if "username" in session:
if request.method=='POST':
new_problem=request.form
cur = mysql.connection.cursor()
result = cur.execute("select max(problemid) from problems")
fetch = cur.fetchall()
if fetch==((None,),):
new_id='00000'
else:
new_id = str(int(fetch[0][0])+1).rjust(5,'0')
name = new_problem["name"]
problem_statement = new_problem["subject"]
qtype = new_problem["qtype"]
solution = new_problem["Answer"]
cur = mysql.connection.cursor()
if qtype=='problem':
cur.execute("insert into problems values('%s','null','%s','%s','%s','%s','%s')" %(new_id,problem_statement,course_id,name,solution,qtype))
else:
cur.execute("insert into problems values('%s','null','%s','%s','%s','None','%s')" %(new_id,problem_statement,course_id,name,qtype))
mysql.connection.commit()
cur.close()
return redirect(url_for('problem',course_id=course_id,problem_id=new_id))
else:
return render_template('wst.html')
else:
return redirect(url_for('register'))
@app.route("/about")
def about():
if "username" in session:
return render_template('about.html', stype=session['type'])
else:
return render_template('about.html', stype='None')
if __name__=='__main__':
app.run(debug=True)