2023-01-28 18:42:28 +01:00
|
|
|
from urllib.request import Request, urlopen
|
2023-02-12 12:39:23 +01:00
|
|
|
from flask import Flask, render_template, url_for, request, redirect, session, flash, jsonify
|
2023-02-12 12:08:57 +01:00
|
|
|
from flask_pymongo import PyMongo
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
import bcrypt
|
|
|
|
import urllib
|
|
|
|
from pymongo import MongoClient
|
2023-01-07 12:44:09 +01:00
|
|
|
import logging
|
2023-01-28 18:42:28 +01:00
|
|
|
import json
|
2023-01-07 12:44:09 +01:00
|
|
|
import sys
|
2022-12-05 16:13:40 +01:00
|
|
|
import os
|
|
|
|
|
2023-02-12 12:08:57 +01:00
|
|
|
load_dotenv()
|
2023-01-07 12:44:09 +01:00
|
|
|
logging.basicConfig(
|
|
|
|
level=logging.INFO,
|
|
|
|
format="%(asctime)s [%(levelname)s] %(message)s",
|
|
|
|
handlers=[
|
|
|
|
logging.FileHandler("src/log/all.log"),
|
|
|
|
logging.StreamHandler(sys.stdout)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2022-12-05 16:13:40 +01:00
|
|
|
app = Flask(__name__)
|
2023-02-12 12:08:57 +01:00
|
|
|
app.secret_key = "testing"
|
|
|
|
PASSWORD_MONGODB = os.getenv('PASSWORD_MONGODB') #Password for MongoDB
|
|
|
|
URL_MONGODB = os.getenv('URL_MONGODB') #URL for MongoDB
|
2023-02-12 12:39:23 +01:00
|
|
|
client = MongoClient("mongodb+srv://elci:" + urllib.parse.quote_plus(PASSWORD_MONGODB) + URL_MONGODB) #Connect to MongoDB
|
|
|
|
database = client["website-class"] #Database name
|
|
|
|
collection = database["school-time-table"] #Collection school time table current
|
2022-12-05 16:13:40 +01:00
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def homepage():
|
2023-01-07 12:44:09 +01:00
|
|
|
logging.info("A user went up: Homepage")
|
2023-02-12 13:40:53 +01:00
|
|
|
return render_template('homepage.html')
|
2022-12-05 16:13:40 +01:00
|
|
|
#imageList = os.listdir('static/images')
|
|
|
|
#imageList = ['images/' + image for image in imageList]
|
|
|
|
#return render_template('go.html', imageList=imageList)
|
|
|
|
|
|
|
|
@app.route('/orario')
|
|
|
|
def orario():
|
2023-01-07 12:44:09 +01:00
|
|
|
logging.info("A user went up: Orario")
|
2023-02-12 15:21:42 +01:00
|
|
|
#url = "http://127.0.0.1:5000"
|
|
|
|
#response = Request(url, headers={"User-Agent": "Mozilla/5.0"})
|
|
|
|
#webpage = urlopen(response).read()
|
2023-02-12 12:39:23 +01:00
|
|
|
# Take all data from mongodb
|
2023-02-12 15:21:42 +01:00
|
|
|
dict = list(collection.find({}, {"_id": 0, "School Subject": 1}))
|
2023-01-28 16:28:50 +01:00
|
|
|
number = str(range(0,7))
|
|
|
|
day = str(["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"])
|
|
|
|
return render_template('orario/orario.html', data=dict, number=number, day=day)
|
2022-12-24 17:38:40 +01:00
|
|
|
|
2023-02-12 12:08:57 +01:00
|
|
|
# #connect to your Mongo DB database
|
|
|
|
def MongoDB():
|
|
|
|
client = MongoClient("mongodb+srv://elci:" + urllib.parse.quote_plus(PASSWORD_MONGODB) + URL_MONGODB)
|
|
|
|
db = client.get_database('website-class')
|
|
|
|
records = db.users
|
|
|
|
return records
|
|
|
|
|
|
|
|
records = MongoDB()
|
|
|
|
|
|
|
|
|
|
|
|
#assign URLs to have a particular route
|
|
|
|
@app.route("/register", methods=['post', 'get'])
|
|
|
|
def register():
|
|
|
|
message = ''
|
|
|
|
#if method post in index
|
|
|
|
if "email" in session:
|
|
|
|
return redirect(url_for("logged_in"))
|
|
|
|
if request.method == "POST":
|
|
|
|
user = request.form.get("fullname")
|
|
|
|
email = request.form.get("email")
|
|
|
|
password1 = request.form.get("password1")
|
|
|
|
password2 = request.form.get("password2")
|
|
|
|
#if found in database showcase that it's found
|
|
|
|
user_found = records.find_one({"name": user})
|
|
|
|
email_found = records.find_one({"email": email})
|
|
|
|
if user_found:
|
|
|
|
message = 'There already is a user by that name'
|
|
|
|
return render_template('index.html', message=message)
|
|
|
|
if email_found:
|
|
|
|
message = 'This email already exists in database'
|
|
|
|
return render_template('index.html', message=message)
|
|
|
|
if password1 != password2:
|
|
|
|
message = 'Passwords should match!'
|
|
|
|
return render_template('index.html', message=message)
|
|
|
|
else:
|
|
|
|
#hash the password and encode it
|
|
|
|
hashed = bcrypt.hashpw(password2.encode('utf-8'), bcrypt.gensalt())
|
|
|
|
#assing them in a dictionary in key value pairs
|
|
|
|
user_input = {'name': user, 'email': email, 'password': hashed}
|
|
|
|
#insert it in the record collection
|
|
|
|
records.insert_one(user_input)
|
|
|
|
|
|
|
|
#find the new created account and its email
|
|
|
|
user_data = records.find_one({"email": email})
|
|
|
|
new_email = user_data['email']
|
|
|
|
#if registered redirect to logged in as the registered user
|
2023-02-12 12:44:29 +01:00
|
|
|
return render_template('logged_in/logged_in.html', email=new_email)
|
2023-02-12 12:08:57 +01:00
|
|
|
return render_template('index.html')
|
|
|
|
|
|
|
|
@app.route("/login", methods=["POST", "GET"])
|
|
|
|
def login():
|
|
|
|
message = 'Please login to your account'
|
|
|
|
if "email" in session:
|
|
|
|
return redirect(url_for("logged_in"))
|
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
email = request.form.get("email")
|
|
|
|
password = request.form.get("password")
|
|
|
|
|
|
|
|
#check if email exists in database
|
|
|
|
email_found = records.find_one({"email": email})
|
|
|
|
if email_found:
|
|
|
|
email_val = email_found['email']
|
|
|
|
passwordcheck = email_found['password']
|
|
|
|
#encode the password and check if it matches
|
|
|
|
if bcrypt.checkpw(password.encode('utf-8'), passwordcheck):
|
|
|
|
session["email"] = email_val
|
|
|
|
return redirect(url_for('logged_in'))
|
|
|
|
else:
|
|
|
|
if "email" in session:
|
|
|
|
return redirect(url_for("logged_in"))
|
|
|
|
message = 'Wrong password'
|
2023-02-12 12:44:29 +01:00
|
|
|
return render_template('login/login.html', message=message)
|
2023-02-12 12:08:57 +01:00
|
|
|
else:
|
|
|
|
message = 'Email not found'
|
2023-02-12 12:44:29 +01:00
|
|
|
return render_template('login/login.html', message=message)
|
|
|
|
return render_template('login/login.html', message=message)
|
2023-02-12 12:08:57 +01:00
|
|
|
|
|
|
|
@app.route('/logged_in')
|
|
|
|
def logged_in():
|
|
|
|
if "email" in session:
|
|
|
|
email = session["email"]
|
2023-02-12 12:44:29 +01:00
|
|
|
return render_template('logged_in/logged_in.html', email=email)
|
2023-02-12 12:08:57 +01:00
|
|
|
else:
|
|
|
|
return redirect(url_for("login"))
|
|
|
|
|
|
|
|
@app.route("/logout", methods=["POST", "GET"])
|
|
|
|
def logout():
|
|
|
|
if "email" in session:
|
|
|
|
session.pop("email", None)
|
2023-02-12 12:44:29 +01:00
|
|
|
return render_template("signout/signout.html")
|
2023-02-12 12:08:57 +01:00
|
|
|
else:
|
|
|
|
return render_template('index.html')
|
|
|
|
|
2022-12-05 16:13:40 +01:00
|
|
|
if __name__ == '__main__':
|
2023-01-07 12:44:09 +01:00
|
|
|
logging.info("Web server started!")
|
2023-01-26 18:06:43 +01:00
|
|
|
app.run(port=4999, debug=True)
|