2018-12-04 17:21:08 +01:00
|
|
|
# coding: utf-8
|
2019-01-14 17:28:38 +01:00
|
|
|
from flask import Flask, render_template, session, request, redirect, flash, g, url_for,jsonify
|
2018-12-04 17:21:08 +01:00
|
|
|
import os
|
2018-12-11 16:53:51 +01:00
|
|
|
import sqlite3
|
2018-12-19 14:22:39 +01:00
|
|
|
import ldap
|
|
|
|
import py_ldap
|
2019-01-14 17:28:38 +01:00
|
|
|
import json
|
2018-12-11 16:53:51 +01:00
|
|
|
|
2018-12-12 08:11:04 +01:00
|
|
|
DATABASE = 'inquire.db'
|
2018-12-19 14:18:21 +01:00
|
|
|
servers = ["ldap://dc1.labs.wmi.amu.edu.pl", "ldap://dc2.labs.wmi.amu.edu.pl"]
|
|
|
|
suffix = "@labs.wmi.amu.edu.pl";
|
|
|
|
port = 636;
|
|
|
|
root = "DC=labs,DC=wmi,DC=amu,DC=edu,DC=pl";
|
2018-12-11 16:53:51 +01:00
|
|
|
|
2018-11-28 11:08:23 +01:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
2019-01-14 21:06:20 +01:00
|
|
|
app.secret_key = '846f1df9f7b9ead65e1a9c32a8c3522ebb6c823a6498a0f8'
|
2018-12-12 08:28:30 +01:00
|
|
|
'''
|
|
|
|
with app.app_context():
|
|
|
|
db = g._database = sqlite3.connect(DATABASE)
|
|
|
|
db_conn = db.cursor()
|
|
|
|
'''
|
2018-12-04 17:21:08 +01:00
|
|
|
@app.route('/')
|
|
|
|
def home():
|
|
|
|
if not session.get('logged_in'):
|
|
|
|
return render_template('login.html')
|
|
|
|
else:
|
2019-01-23 12:39:46 +01:00
|
|
|
return redirect('/courses')
|
|
|
|
'''
|
2019-01-16 12:27:19 +01:00
|
|
|
if session['user_type'] == 'lecturer':
|
|
|
|
return redirect('/lecturer')
|
|
|
|
elif session['user_type'] == 'student':
|
|
|
|
return redirect('/courses')
|
2019-01-23 12:39:46 +01:00
|
|
|
'''
|
2018-12-04 17:21:08 +01:00
|
|
|
|
2019-01-23 12:39:46 +01:00
|
|
|
@app.route('/student/<lect_id>',methods=['GET', 'POST'])
|
|
|
|
def student(lect_id):
|
2018-12-12 09:12:36 +01:00
|
|
|
db = sqlite3.connect(DATABASE)
|
|
|
|
#db.row_factory = sql.Row
|
|
|
|
db_conn = db.cursor()
|
2019-01-23 12:39:46 +01:00
|
|
|
db_conn.execute("select * from question where lecture_id = ?;",(lect_id,))
|
2018-12-12 09:12:36 +01:00
|
|
|
questions = db_conn.fetchall()
|
|
|
|
return render_template('student.html', questions=questions)
|
2018-12-12 08:28:30 +01:00
|
|
|
|
2019-01-23 12:39:46 +01:00
|
|
|
@app.route('/ask/<lect_id>', methods=['POST'])
|
|
|
|
def ask(lect_id):
|
2018-12-05 10:57:01 +01:00
|
|
|
if request.method == 'POST':
|
|
|
|
question = request.get_data().decode("utf-8")
|
2018-12-12 11:22:33 +01:00
|
|
|
db = sqlite3.connect(DATABASE)
|
|
|
|
db_conn = db.cursor()
|
2018-12-12 12:29:00 +01:00
|
|
|
|
2019-01-23 12:39:46 +01:00
|
|
|
db_conn.execute("INSERT INTO question (lecture_id, question_text, upvotes) VALUES (?, ?,?);",(lect_id,question,0))
|
2018-12-12 11:38:46 +01:00
|
|
|
db.commit()
|
2019-01-14 17:28:38 +01:00
|
|
|
return json.dumps({'success':True}), 200, {'ContentType':'application/json'}
|
2018-11-28 11:08:23 +01:00
|
|
|
|
2019-01-23 12:39:46 +01:00
|
|
|
@app.route('/lecturer/<lect_id>')
|
|
|
|
def lecturer(lect_id):
|
2018-12-17 21:24:44 +01:00
|
|
|
db = sqlite3.connect(DATABASE)
|
|
|
|
#db.row_factory = sql.Row
|
|
|
|
db_conn = db.cursor()
|
2019-01-23 12:39:46 +01:00
|
|
|
db_conn.execute("select * from question where lecture_id = ?;",(lect_id,))
|
2018-12-17 21:24:44 +01:00
|
|
|
questions = db_conn.fetchall()
|
|
|
|
return render_template('lecturer.html', questions=questions)
|
2019-01-14 17:28:38 +01:00
|
|
|
|
2018-12-04 17:21:08 +01:00
|
|
|
@app.route('/login', methods=['POST'])
|
|
|
|
def do_login():
|
2018-12-19 14:18:21 +01:00
|
|
|
login=request.form['name']
|
|
|
|
password=request.form['password']
|
|
|
|
ldapquery = py_ldap.LdapQuery(servers, port, root)
|
|
|
|
auth=ldapquery.authenticate(login+suffix, password)
|
2019-01-22 16:06:22 +01:00
|
|
|
searchstring=ldapquery.search_user(login);
|
2019-01-23 12:39:46 +01:00
|
|
|
if auth==True and (searchstring[0][0].lower().find('ou=faculty')!=-1 or login == 's396355' or login == 's441471'):
|
2018-12-04 17:21:08 +01:00
|
|
|
session['logged_in'] = True
|
|
|
|
session['user_type'] = 'lecturer'
|
2019-01-22 16:06:22 +01:00
|
|
|
elif auth==True and searchstring[0][0].lower().find('ou=students')!=-1:
|
2018-12-04 17:21:08 +01:00
|
|
|
session['logged_in'] = True
|
|
|
|
session['user_type'] = 'student'
|
|
|
|
else:
|
2018-12-19 14:18:21 +01:00
|
|
|
flash('Niepoprawne hasło/nazwa użytkownika!')
|
2018-12-04 17:21:08 +01:00
|
|
|
return redirect('/')
|
2018-11-28 11:08:23 +01:00
|
|
|
|
2018-12-04 17:21:08 +01:00
|
|
|
@app.route('/logout', methods=['GET', 'POST'])
|
|
|
|
def logout():
|
|
|
|
session['logged_in'] = False
|
|
|
|
return redirect('/')
|
2018-11-28 11:08:23 +01:00
|
|
|
|
2018-12-12 11:22:33 +01:00
|
|
|
@app.route('/upvote/<quest_id>', methods=['POST'])
|
|
|
|
def upvote(quest_id):
|
|
|
|
print("upvoted {}".format(quest_id))
|
2018-12-12 08:28:30 +01:00
|
|
|
db = sqlite3.connect(DATABASE)
|
|
|
|
db_conn = db.cursor()
|
2018-12-12 12:29:00 +01:00
|
|
|
db_conn.execute("UPDATE question SET upvotes = upvotes + 1 WHERE question_id = ?;",(quest_id,))
|
2018-12-12 11:38:46 +01:00
|
|
|
#db_conn.commit()
|
|
|
|
db.commit()
|
2018-12-12 12:29:00 +01:00
|
|
|
return '200'
|
2018-12-05 11:26:32 +01:00
|
|
|
|
2018-12-18 16:58:01 +01:00
|
|
|
@app.route('/answer/<quest_id>', methods=['POST'])
|
|
|
|
def answer(quest_id):
|
|
|
|
answer = request.get_data().decode("utf-8")
|
|
|
|
db = sqlite3.connect(DATABASE)
|
|
|
|
db_conn = db.cursor()
|
|
|
|
db_conn.execute("UPDATE question SET has_answer = 1 , answer = ? WHERE question_id = ?;",(answer,quest_id))
|
|
|
|
db.commit()
|
|
|
|
return redirect(url_for('student'))
|
|
|
|
|
|
|
|
|
2018-12-19 12:43:36 +01:00
|
|
|
@app.route('/delete', methods=['POST'])
|
|
|
|
def do_delete():
|
|
|
|
quest_id = request.get_data().decode("utf-8")
|
|
|
|
db = sqlite3.connect(DATABASE)
|
|
|
|
db_conn = db.cursor()
|
|
|
|
db_conn.execute("DELETE FROM question WHERE question_id = ?;",(quest_id,))
|
|
|
|
db.commit()
|
|
|
|
return '200'
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-12-17 15:37:50 +01:00
|
|
|
############################################################
|
|
|
|
@app.route('/courses') #
|
|
|
|
def courses(): #
|
2019-01-23 12:39:46 +01:00
|
|
|
db = sqlite3.connect(DATABASE)
|
|
|
|
#db.row_factory = sql.Row
|
|
|
|
db_conn = db.cursor()
|
|
|
|
db_conn.execute("select * from course;")
|
|
|
|
courses = db_conn.fetchall()
|
|
|
|
return render_template('courses.html', courses=courses)
|
2018-12-17 15:37:50 +01:00
|
|
|
|
|
|
|
@app.route('/lectures/<lect_id>', methods=['POST', 'GET'])
|
2019-01-23 12:39:46 +01:00
|
|
|
def lectures(lect_id):
|
|
|
|
db = sqlite3.connect(DATABASE)
|
|
|
|
#db.row_factory = sql.Row
|
|
|
|
db_conn = db.cursor()
|
|
|
|
db_conn.execute("select * from lecture where course_id = ?;",(lect_id,))
|
|
|
|
lectures = db_conn.fetchall()
|
|
|
|
return render_template('lectures.html', lectures = lectures)
|
2018-12-17 15:37:50 +01:00
|
|
|
|
|
|
|
@app.route('/account', methods=['GET', 'POST'])
|
|
|
|
def account():
|
|
|
|
if session.get('user_type') == 'lecturer':
|
|
|
|
return redirect(url_for('lecturer'))
|
|
|
|
else: #
|
|
|
|
return redirect(url_for('student')) #
|
|
|
|
############################################################
|
2018-12-05 11:26:32 +01:00
|
|
|
|
2019-01-23 12:39:46 +01:00
|
|
|
@app.route('/questions/<lect_id>')
|
|
|
|
def goto_questions(lect_id):
|
|
|
|
if session.get('user_type') == 'lecturer':
|
|
|
|
return redirect('lecturer/{}'.format(lect_id))
|
|
|
|
else: #
|
|
|
|
return redirect('student/{}'.format(lect_id))
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-01-13 13:09:14 +01:00
|
|
|
|
2019-01-14 21:06:20 +01:00
|
|
|
|
2019-01-13 13:01:17 +01:00
|
|
|
|
2018-11-28 11:08:23 +01:00
|
|
|
if __name__ == '__main__':
|
2019-01-13 13:01:17 +01:00
|
|
|
|
2018-12-12 08:11:04 +01:00
|
|
|
app.run(host='0.0.0.0', debug = True)
|
|
|
|
|