82 lines
2.3 KiB
Python
82 lines
2.3 KiB
Python
# coding: utf-8
|
|
from flask import Flask, render_template, session, request, redirect, flash, g
|
|
import os
|
|
import sqlite3
|
|
|
|
DATABASE = 'inquire.db'
|
|
|
|
|
|
|
|
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:
|
|
if session.get('user_type') == 'lecturer':
|
|
return redirect('/lecturer')
|
|
else:
|
|
return redirect('/student')
|
|
|
|
@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 do_add():
|
|
if request.method == 'POST':
|
|
question = request.get_data().decode("utf-8")
|
|
question = question.split('=')
|
|
print(question[1])
|
|
db = sqlite3.connect(DATABASE)
|
|
db_conn = db.cursor()
|
|
db_conn.execute("INSERT INTO question (subject_id, question_text, upvotes) VALUES (?, ?,?);",1,question[1],0)
|
|
|
|
return '200'
|
|
|
|
@app.route('/lecturer')
|
|
def lecturer():
|
|
return render_template('lecturer.html')
|
|
|
|
@app.route('/login', methods=['POST'])
|
|
def do_login():
|
|
if request.form['password'] == 'lecturer' and request.form['name'] == 'lecturer':
|
|
session['logged_in'] = True
|
|
session['user_type'] = 'lecturer'
|
|
elif request.form['password'] == 'student' and request.form['name'] == 'student':
|
|
session['logged_in'] = True
|
|
session['user_type'] = 'student'
|
|
else:
|
|
flash('Niepoprawne hasło/nazwa użytkownika!'.decode('utf-8'))
|
|
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
|
|
return redirect('/student')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.secret_key = os.urandom(12)
|
|
app.run(host='0.0.0.0', debug = True)
|
|
|