178 lines
5.9 KiB
Python
178 lines
5.9 KiB
Python
# coding: utf-8
|
|
from flask import Flask, render_template, session, request, redirect, flash, g, url_for,jsonify
|
|
import os
|
|
import sqlite3
|
|
import ldap
|
|
import py_ldap
|
|
import json
|
|
|
|
DATABASE = 'inquire.db'
|
|
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";
|
|
|
|
|
|
app = Flask(__name__)
|
|
app.secret_key = '846f1df9f7b9ead65e1a9c32a8c3522ebb6c823a6498a0f8'
|
|
'''
|
|
with app.app_context():
|
|
db = g._database = sqlite3.connect(DATABASE)
|
|
db_conn = db.cursor()
|
|
'''
|
|
@app.route('/')
|
|
def home():
|
|
if not session.get('logged_in'):
|
|
return render_template('login.html')
|
|
else:
|
|
return redirect('/courses')
|
|
'''
|
|
if session['user_type'] == 'lecturer':
|
|
return redirect('/lecturer')
|
|
elif session['user_type'] == 'student':
|
|
return redirect('/courses')
|
|
'''
|
|
|
|
@app.route('/student/<lect_id>',methods=['GET', 'POST'])
|
|
def student(lect_id):
|
|
db = sqlite3.connect(DATABASE)
|
|
#db.row_factory = sql.Row
|
|
db_conn = db.cursor()
|
|
db_conn.execute("select * from question where lecture_id = ?;",(lect_id,))
|
|
questions = db_conn.fetchall()
|
|
return render_template('student.html', questions=questions)
|
|
|
|
@app.route('/ask/<lect_id>', methods=['POST'])
|
|
def ask(lect_id):
|
|
if request.method == 'POST':
|
|
question = request.get_data().decode("utf-8")
|
|
db = sqlite3.connect(DATABASE)
|
|
db_conn = db.cursor()
|
|
|
|
db_conn.execute("INSERT INTO question (lecture_id, question_text, upvotes) VALUES (?, ?,?);",(lect_id,question,0))
|
|
db.commit()
|
|
return json.dumps({'success':True}), 200, {'ContentType':'application/json'}
|
|
|
|
@app.route('/lecturer/<lect_id>')
|
|
def lecturer(lect_id):
|
|
db = sqlite3.connect(DATABASE)
|
|
#db.row_factory = sql.Row
|
|
db_conn = db.cursor()
|
|
db_conn.execute("select * from question where lecture_id = ?;",(lect_id,))
|
|
questions = db_conn.fetchall()
|
|
return render_template('lecturer.html', questions=questions)
|
|
|
|
@app.route('/login', methods=['POST'])
|
|
def do_login():
|
|
login=request.form['name']
|
|
password=request.form['password']
|
|
ldapquery = py_ldap.LdapQuery(servers, port, root)
|
|
auth=ldapquery.authenticate(login+suffix, password)
|
|
searchstring=ldapquery.search_user(login);
|
|
if auth==True and (searchstring[0][0].lower().find('ou=faculty')!=-1 or login == 's396355' or login == 's407283'):
|
|
session['logged_in'] = True
|
|
session['user_type'] = 'lecturer'
|
|
session['user_login'] = login
|
|
|
|
|
|
elif auth==True and searchstring[0][0].lower().find('ou=students')!=-1:
|
|
session['logged_in'] = True
|
|
session['user_type'] = 'student'
|
|
session['user_login'] = login
|
|
print(session)
|
|
|
|
else:
|
|
flash('Niepoprawne hasło/nazwa użytkownika!')
|
|
return redirect('/')
|
|
|
|
@app.route('/logout', methods=['GET', 'POST'])
|
|
def logout():
|
|
session['logged_in'] = False
|
|
return redirect('/')
|
|
|
|
@app.route('/upvote/<quest_id>', methods=['POST'])
|
|
def upvote(quest_id):
|
|
print("upvoted {}".format(quest_id))
|
|
login = session.get('user_login')
|
|
print(login)
|
|
db = sqlite3.connect(DATABASE)
|
|
db_conn = db.cursor()
|
|
db_conn.execute("SELECT * FROM upvote WHERE question_id = ? AND user_login = ?;",(quest_id,login))
|
|
upvotes = db_conn.fetchall()
|
|
if len(upvotes) == 0:
|
|
db_conn.execute("UPDATE question SET upvotes = upvotes + 1 WHERE question_id = ?;",(quest_id,))
|
|
db.commit()
|
|
db_conn.execute("INSERT INTO upvote (question_id, user_login) VALUES (?,?);",(quest_id,login))
|
|
db.commit()
|
|
else:
|
|
db_conn.execute("UPDATE question SET upvotes = upvotes - 1 WHERE question_id = ?;",(quest_id,))
|
|
db.commit()
|
|
db_conn.execute("DELETE FROM upvote WHERE question_id = ? AND user_login = ?;",(quest_id,login))
|
|
db.commit()
|
|
return '200'
|
|
|
|
@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 '200'
|
|
|
|
|
|
@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'
|
|
|
|
|
|
|
|
############################################################
|
|
@app.route('/courses') #
|
|
def courses(): #
|
|
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)
|
|
|
|
@app.route('/lectures/<lect_id>', methods=['POST', 'GET'])
|
|
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)
|
|
|
|
@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')) #
|
|
############################################################
|
|
|
|
@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))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(host='0.0.0.0', debug = True)
|
|
|