InQuire/App.py
2019-01-13 13:01:17 +01:00

132 lines
4.0 KiB
Python

# coding: utf-8
from flask import Flask, render_template, session, request, redirect, flash, g, url_for
import os
import sqlite3
import ldap
import py_ldap
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__)
'''
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')
@app.route('/student',methods=['GET', 'POST'])
def student():
db = sqlite3.connect(DATABASE)
#db.row_factory = sql.Row
db_conn = db.cursor()
db_conn.execute("select * from question;")
questions = db_conn.fetchall()
return render_template('student.html', questions=questions)
@app.route('/ask', methods=['POST'])
def ask():
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 (subject_id, question_text, upvotes) VALUES (?, ?,?);",(1,question,0))
db.commit()
return redirect(url_for('student'))
@app.route('/lecturer')
def lecturer():
db = sqlite3.connect(DATABASE)
#db.row_factory = sql.Row
db_conn = db.cursor()
db_conn.execute("select * from question;")
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)
if auth==True and login == 's396355':
session['logged_in'] = True
session['user_type'] = 'lecturer'
elif auth==True and login == 's441471':
session['logged_in'] = True
session['user_type'] = 'student'
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))
db = sqlite3.connect(DATABASE)
db_conn = db.cursor()
db_conn.execute("UPDATE question SET upvotes = upvotes + 1 WHERE question_id = ?;",(quest_id,))
#db_conn.commit()
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 redirect(url_for('student'))
@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(): #
return render_template('courses.html')
@app.route('/lectures/<lect_id>', methods=['POST', 'GET'])
def lectures(lect_id):
return render_template('lectures.html')
@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.secret_key = os.urandom(12)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug = True)