diff --git a/app.py b/app.py index 09173a5..d136358 100644 --- a/app.py +++ b/app.py @@ -19,12 +19,14 @@ from oauth2client.service_account import ServiceAccountCredentials import os from werkzeug.utils import secure_filename from camera import VideoCamera -# import pyaudio +import pyaudio import sqlite3 from flask_mail import Mail, Message from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header +from datetime import datetime +from dateutil import parser main = Blueprint('main', __name__) @@ -138,7 +140,7 @@ DRIVE_SCOPES = "https://www.googleapis.com/auth/drive" @socketio.on('message') def handleMessage(msg): - print('Message: ' + msg) + print('Message: ' + msg.decode("utf-8")) send(msg, broadcast=True) @app.route("/") @@ -155,6 +157,15 @@ def main(): creds = tools.run_flow(flow, store) drive_service = build('drive', 'v3', http=creds.authorize(Http())) + connection = sqlite3.connect('database.db', isolation_level = None) + cur = connection.cursor() + cursor = cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='logins'") + results = cursor.fetchall() + if len(results) > 0: + pass + else: + cur.execute("CREATE TABLE logins (login TEXT PRIMARY KEY)") + connection.close() return render_template('index.html', info="Witaj!") @app.route("/indexENG") @@ -239,7 +250,7 @@ def signUpENG(): cursor.close() conn.close() -@app.route('/validateLogin',methods=['POST']) +@app.route('/validateLogin', methods=['POST']) def validateLogin(): #con, cursor = None, None #try: @@ -273,9 +284,20 @@ def validateLogin(): else: test = False else: - test = False + test = False + cursor = cur.execute("SELECT login from logins") + logins = cursor.fetchall() + print (logins) + emails = [] + for login in logins: + emails.append(login[0]) + print (emails) + if session["user"].split("@")[0] not in emails: + cur.execute("INSERT INTO logins(login) values(?)", (session["user"].split("@")[0],)) + cursor = cur.execute("SELECT login from logins") + logins = cursor.fetchall() connection.close() - return render_template('userHome.html', user=session['user'].split('@')[0], calendar_src="https://calendar.google.com/calendar/embed?src=" + session.get("user"), test = test) + return render_template('userHome.html', user=session['user'].split('@')[0], calendar_src="https://calendar.google.com/calendar/embed?src=" + session.get("user"), test = test, logins = logins) # else: # return render_template('error.html',error = 'Zły email lub hasło!') except smtplib.SMTPAuthenticationError: @@ -335,7 +357,17 @@ def validateLoginENG(): @app.route('/userHome') def userHome(): if session.get('user'): - return render_template('userHome.html', user=session['user'].split('@')[0], calendar_src="https://calendar.google.com/calendar/embed?src=" + session.get("user"), test = test) + connection = sqlite3.connect('database.db', isolation_level = None) + cur = connection.cursor() + cursor = cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='tests'") + results = cursor.fetchall() + cursor = cur.execute("SELECT login from logins") + logins = cursor.fetchall() + connection.close() + if len(results) > 0: + return render_template('userHome.html', user=session['user'].split('@')[0], calendar_src="https://calendar.google.com/calendar/embed?src=" + session.get("user"), test = True, logins = logins) + else: + return render_template('userHome.html', user=session['user'].split('@')[0], calendar_src="https://calendar.google.com/calendar/embed?src=" + session.get("user"), test = False, logins = logins) else: return render_template('error.html',error = 'Nieautoryzowany dostęp!') @@ -548,9 +580,8 @@ def signInWithFace(): cv2.destroyAllWindows() connection = sqlite3.connect('database.db', isolation_level = None) cur = connection.cursor() - cursor = cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='tests") + cursor = cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='tests'") results = cursor.fetchall() - connection.close() if len(results) != 0: cursor = cur.execute("SELECT title from tests") results = cursor.fetchone() @@ -560,7 +591,10 @@ def signInWithFace(): test = False else: test = False - return render_template('userHome.html', info="Zapisano twarz", user=session.get('user').split('@')[0], calendar_src="https://calendar.google.com/calendar/embed?src=" + session.get("user"), test = test) + cursor = cur.execute("SELECT login from logins") + logins = cursor.fetchall() + connection.close() + return render_template('userHome.html', info="Zapisano twarz", user=session.get('user').split('@')[0], calendar_src="https://calendar.google.com/calendar/embed?src=" + session.get("user"), test = test, logins = logins) @app.route('/signInWithFaceENG') def signInWithFaceENG(): @@ -633,12 +667,19 @@ def addEvent(): summary = request.form["inputSummary"] location = request.form["inputLocation"] description = request.form["inputDescription"] - description = request.form["inputDescription"] - description = request.form["inputDescription"] start = request.form["inputStart"] end = request.form["inputEnd"] attendees = request.form["inputAttendees"].split(',') + datetime_start = parser.parse(start) + datetime_end = parser.parse(end) + + print (datetime_start) + print (datetime_end) + + if datetime_start > datetime_end: + return render_template('error.html',error = 'Nieprawidłowy termin') + processed_attendees = [] for attendee in attendees: attendee = attendee.strip() @@ -683,9 +724,8 @@ def addEvent(): service.events().insert(calendarId='primary', body=event).execute() connection = sqlite3.connect('database.db', isolation_level = None) cur = connection.cursor() - cursor = cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='tests") + cursor = cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='tests'") results = cursor.fetchall() - connection.close() if len(results) != 0: cursor = cur.execute("SELECT title from tests") results = cursor.fetchone() @@ -694,8 +734,11 @@ def addEvent(): else: test = False else: - test = False - return render_template('userHome.html', user=session['user'].split('@')[0], calendar_src="https://calendar.google.com/calendar/embed?src=" + session.get("user"), test = test) + test = False + cursor = cur.execute("SELECT login from logins") + logins = cursor.fetchall() + connection.close() + return render_template('userHome.html', user=session['user'].split('@')[0], calendar_src="https://calendar.google.com/calendar/embed?src=" + session.get("user"), test = test, logins = logins) @app.route('/streaming') def streaming(): @@ -716,6 +759,20 @@ def gen(camera): @app.route('/stream') def stream(): + connection = sqlite3.connect('database.db', isolation_level = None) + cur = connection.cursor() + cursor = cur.execute("SELECT login from logins") + logins = cursor.fetchall() + emails = [] + for login in logins: + emails.append(login[0] + "@gmail.com") + print (emails) + msg = mail.send_message( + 'Nowy stream', + sender='wirtualna.uczelnia.2018@gmail.com', + recipients=emails, + body="Został uruchomiony nowy stream pod adresem:\n" + "http://192.168.8.100:5000/streaming" + ) return Response(gen(VideoCamera()), mimetype='multipart/x-mixed-replace; boundary=frame') @@ -774,7 +831,11 @@ def resources(): #file = request.form["fileToUpload"] filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) - return render_template('userHome.html', user=session['user'].split('@')[0], calendar_src="https://calendar.google.com/calendar/embed?src=" + session.get("user"), test = False) + connection = sqlite3.connect('database.db', isolation_level = None) + cur = connection.cursor() + cursor = cur.execute("SELECT login from logins") + logins = cursor.fetchall() + return render_template('userHome.html', user=session['user'].split('@')[0], calendar_src="https://calendar.google.com/calendar/embed?src=" + session.get("user"), test = False, logins = logins) else: return redirect(url_for('toResources')) @@ -912,7 +973,7 @@ def create_test(): 'Zostałeś przydzielony do nowego testu', sender='wirtualna.uczelnia.2018@gmail.com', recipients=emails, - body="Pojawił się nowy test, zaloguj się i go rozwiąż:\n" + "http://150.254.78.161:5000/showSignIn" + body="Pojawił się nowy test, zaloguj się i go rozwiąż:\n" + "http://192.168.8.100:5000/showSignIn" ) connection = sqlite3.connect('database.db', isolation_level = None) cur = connection.cursor() @@ -976,8 +1037,10 @@ def add_question(): cursor = cur.execute("SELECT * from questions") print (cursor.fetchone()) # print (cursor) - # connection.close() - return render_template("userHome.html", user=session['user'].split('@')[0], calendar_src="https://calendar.google.com/calendar/embed?src=" + session.get("user"), test = True) + cursor = cur.execute("SELECT login from logins") + logins = cursor.fetchall() + connection.close() + return render_template("userHome.html", user=session['user'].split('@')[0], calendar_src="https://calendar.google.com/calendar/embed?src=" + session.get("user"), test = True, logins = logins) else: return render_template("error.html", error = "Brak dostępu!") @@ -985,24 +1048,22 @@ def add_question(): def show_test(): connection = sqlite3.connect('database.db', isolation_level = None) cur = connection.cursor() - cursor = cur.execute("SELECT name from sqlite_master where type='table' and name='results'") - if len(cursor.fetchall()) > 0: - cursor = cur.execute("SELECT nickname from results") - users = cursor.fetchall() - # if session["user"] + "@gmail.com" in users: - if (session["user"].split("@")[0],) not in users: - cursor = cur.execute("SELECT * from questions") - questions = cursor.fetchall() - print (questions) - cursor = cur.execute("SELECT title from tests") - title = cursor.fetchone() - print (title) - connection.close() - return render_template("test.html", questions = questions, test = title[0]) - else: - return render_template("error.html", error = "Nie możesz wypełnić tego testu!") + cursor = cur.execute("SELECT nickname from results") + users = cursor.fetchall() + # if session["user"] + "@gmail.com" in users: + print (users) + if (session["user"].split("@")[0],) not in users: + cursor = cur.execute("SELECT * from questions") + questions = cursor.fetchall() + print (questions) + cursor = cur.execute("SELECT title from tests") + title = cursor.fetchone() + print (title) + connection.close() + return render_template("test.html", questions = questions, test = title[0]) else: - return redirect("userHome") + return render_template("error.html", error = "Nie możesz wypełnić tego testu!") + @app.route("/test", methods = ["POST"]) def test(): @@ -1022,14 +1083,17 @@ def test(): if answers[j] == correct_answers[j][0]: points += 1 cursor.execute("INSERT INTO results values(?, ?)", (session["user"].split("@")[0], points)) - connection.close() if points == 1: sentence = "punkt" elif points == 2 or points == 3 or points == 4: sentence = "punkty" elif points > 4 or points == 0: sentence = "punktów" - return render_template("userHome.html", user=session['user'].split('@')[0] + ". Twój wynik wyniósł " + str(points) + sentence, calendar_src="https://calendar.google.com/calendar/embed?src=" + session.get("user"), test = False) + cur = connection.cursor() + c = cur.execute("SELECT login from logins") + logins = c.fetchall() + connection.close() + return render_template("userHome.html", user=session['user'].split('@')[0] + ". Twój wynik wyniósł " + str(points) + sentence, calendar_src="https://calendar.google.com/calendar/embed?src=" + session.get("user"), test = False, logins = logins) # if answer == correct_answer: # cursor = connection.execute('SELECT points from results where nickname = ?', session["user"]) @@ -1047,25 +1111,29 @@ def test(): def end_test(): connection = sqlite3.connect('database.db', isolation_level = None) cur = connection.cursor() - if session["role"] == "admin": - cursor = cur.execute('SELECT MAX(points), nickname from results group by nickname') - results = cursor.fetchall() - cur.execute('DROP TABLE IF EXISTS tests') - cur.execute('DROP TABLE IF EXISTS questions') - cur.execute('DROP TABLE IF EXISTS users') - cur.execute('DROP TABLE IF EXISTS results') - connection.close() - if len(results) != 0: - print (results) - if results[0][0] == 1: - sentence = "punkt, a uzyskał go: " - elif results[0][0] == 2 or results[0][0] == 3 or results[0][0] == 4: - sentence = "punkty, a uzyskał go: " - elif results[0][0] > 4 or results[0][0] == 0: - sentence = "punktów, a uzyskał go: " - return render_template("userHome.html", user=session['user'].split('@')[0] + "\nNajlepszy wynik z ostatniego testu wyniósł " + str(results[0][0]) + sentence + str(results[0][1]), calendar_src="https://calendar.google.com/calendar/embed?src=" + session.get("user"), test = False) - else: - return render_template("userHome.html", user=session['user'].split('@')[0], calendar_src="https://calendar.google.com/calendar/embed?src=" + session.get("user"), test = False) + if session["role"] == "admin": + cursor = cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='results'") + if len(cursor.fetchall()) > 0: + cursor = cur.execute('SELECT MAX(points), nickname from results group by nickname') + results = cursor.fetchall() + cur.execute('DROP TABLE IF EXISTS tests') + cur.execute('DROP TABLE IF EXISTS questions') + cur.execute('DROP TABLE IF EXISTS users') + cur.execute('DROP TABLE IF EXISTS results') + cursor = cur.execute("SELECT login from logins") + logins = cursor.fetchall() + connection.close() + if len(results) != 0: + print (results) + if results[0][0] == 1: + sentence = "punkt, a uzyskał go: " + elif results[0][0] == 2 or results[0][0] == 3 or results[0][0] == 4: + sentence = "punkty, a uzyskał go: " + elif results[0][0] > 4 or results[0][0] == 0: + sentence = "punktów, a uzyskał go: " + return render_template("userHome.html", user=session['user'].split('@')[0] + "\nNajlepszy wynik z ostatniego testu wyniósł " + str(results[0][0]) + sentence + str(results[0][1]), calendar_src="https://calendar.google.com/calendar/embed?src=" + session.get("user"), test = False, logins = logins) + else: + return render_template("userHome.html", user=session['user'].split('@')[0], calendar_src="https://calendar.google.com/calendar/embed?src=" + session.get("user"), test = False, logins = logins) else: return render_template("error.html", error = "Brak dostępu!") diff --git a/database.db b/database.db index d3df4b9..f0c4506 100644 Binary files a/database.db and b/database.db differ diff --git a/static/logo.jpg b/static/logo.jpg index 515456b..82b6245 100644 Binary files a/static/logo.jpg and b/static/logo.jpg differ diff --git a/templates/addEvent.html b/templates/addEvent.html index cebcf5e..27e22a8 100644 --- a/templates/addEvent.html +++ b/templates/addEvent.html @@ -1,43 +1,45 @@ +
- -@@ -61,29 +149,16 @@
-+ + + + Użytkownicy uczelni: + {% for login in logins %} + {{ login[0] }} + + {% endfor %} + Dodaj wydarzenie + Czat grupowy + + Podejrzyj materiały + Uruchom testowy stream + {% if session['role'] == 'admin' %} {% if test %}
{% endif %} - -