Project_Fish/app.py
2021-11-10 22:00:18 +01:00

120 lines
3.3 KiB
Python

from flask import *
from flask_login import LoginManager , login_required , UserMixin , login_user, current_user, logout_user
from forms import LoginForm
from services import Services, UserManager
from models import User
import sqlite3
import re
import datetime
#Setup
app = Flask("Project Fish")
app.config.from_pyfile('config.py')
login_manager = LoginManager()
login_manager.login_view = "login"
login_manager.init_app(app)
sqlpath="sqlite.db"
db = sqlite3.connect(sqlpath, check_same_thread=False)
services = Services(db)
usermanager = UserManager(db)
def is_string_sanitized(data):
sanitized_string = re.sub('[^a-zA-Z0-9_@#$.\s]', '', data)
if len(data) != len(sanitized_string):
return False
return True
@login_manager.user_loader
def load_user(user_id):
return usermanager.GetUserByID(user_id)
@app.route('/')
def index():
return render_template("index.html")
@app.route("/GetServices", methods=['GET', 'POST'])
def GetServices():
nummer = (request.json)["nummer"]
if(nummer == "-1"):
ser=services.GetAllServices()
return jsonify(render_template("results.html", services=ser),)
if(not is_string_sanitized(nummer)):
return abort(401)
if(nummer != None and nummer != ""):
ser=services.GetServices(nummer)
return jsonify(render_template("results.html", services=ser),)
ser = services.GetAllServices()
return render_template("results.html", services=ser)
@app.route('/admin')
@login_required
def admin():
return render_template("admin.html")
@app.route("/logout")
@login_required
def logout():
logout_user()
return render_template("index.html")
@app.route('/login' , methods=['GET' , 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if(not (is_string_sanitized(username) and is_string_sanitized(username))):
return abort(401)
registeredUser = usermanager.Authenticate(username, password)
if registeredUser != None:
login_user(registeredUser)
return redirect(url_for('admin'))
else:
return abort(401)
else:
return render_template("login.html")
@app.route("/AddService", methods=["POST"])
@login_required
def AddService():
datum = (request.json)["datum"]
tijd = (request.json)["tijd"]
van = (request.json)["van"]
tot = (request.json)["tot"]
try:
van = int(van)
tot = int(tot)
groepen = ""
if(van < tot):
for i in range(van, tot, 1):
groepen += str(i)
groepen += ":"
else:
return "Invalid groups"
groepen += str(tot)
tijd = datetime.datetime.strptime(f"{datum}|{tijd}" , "%d-%m-%Y|%H:%M")
if(tijd != None):
services.AddService(tijd, str(groepen))
except:
return "500"
return "200"
@app.route("/DelService", methods=["POST"])
@login_required
def DelService():
datum = (request.json)["datum"]
tijd = (request.json)["tijd"]
try:
tijd = datetime.datetime.strptime(f"{datum}|{tijd}" , "%Y-%m-%d|%H:%M:%S")
if(tijd != None):
services.RemoveService(tijd)
except:
return "Invalid date/time"
return "200"
def RunWeb():
app.run(debug=True)