2021-11-07 11:56:41 +00:00
|
|
|
import datetime
|
|
|
|
import sqlite3
|
|
|
|
import locale
|
|
|
|
import random
|
2021-11-07 21:40:34 +00:00
|
|
|
import hashlib
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
from werkzeug.datastructures import Authorization
|
|
|
|
from models import User
|
|
|
|
locale.setlocale(locale.LC_TIME, "nl_NL.utf-8")
|
|
|
|
|
|
|
|
|
2021-11-07 11:56:41 +00:00
|
|
|
|
|
|
|
class Services:
|
2021-11-07 21:40:34 +00:00
|
|
|
def __init__(self, db) -> None:
|
|
|
|
self.db = db
|
2021-11-07 11:56:41 +00:00
|
|
|
self.cursor = self.db.cursor()
|
|
|
|
self.PopulateDB()
|
|
|
|
|
|
|
|
def GetAllServices(self):
|
|
|
|
'''
|
|
|
|
Get all services. Returns a dictionary with, for each key a tuple with (date, groups)
|
|
|
|
'''
|
|
|
|
serv = self.cursor.execute("select * from services").fetchall()
|
|
|
|
res = {}
|
|
|
|
for s in serv:
|
|
|
|
date = datetime.datetime.strptime(s[1], "%Y-%m-%d %H:%M:%S")
|
|
|
|
groups = s[2].split(":")
|
|
|
|
res[s[0]] = (date, groups)
|
|
|
|
return res
|
|
|
|
|
|
|
|
def GetServices(self, number):
|
|
|
|
serv = self.GetAllServices()
|
|
|
|
res = {}
|
|
|
|
for i in serv:
|
|
|
|
if(number in serv[i][1]):
|
|
|
|
res[i] = serv[i]
|
|
|
|
return res
|
|
|
|
|
|
|
|
def PopulateDB(self):
|
|
|
|
'''
|
|
|
|
Populate simple db
|
|
|
|
'''
|
|
|
|
query = f"""CREATE TABLE IF NOT EXISTS services
|
|
|
|
(
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
2021-11-10 21:00:18 +00:00
|
|
|
date STRING UNIQUE,
|
2021-11-07 11:56:41 +00:00
|
|
|
groups STRING
|
|
|
|
);
|
|
|
|
"""
|
|
|
|
self.cursor.execute(query)
|
|
|
|
|
|
|
|
def AddService(self, date, groups):
|
|
|
|
query = f"""
|
|
|
|
INSERT INTO services (date, groups)
|
|
|
|
VALUES('{date}', '{groups}');
|
|
|
|
"""
|
2021-11-10 21:00:18 +00:00
|
|
|
try:
|
|
|
|
self.cursor.execute(query)
|
|
|
|
self.db.commit()
|
|
|
|
return "Success"
|
|
|
|
except:
|
|
|
|
return "Service already exists?"
|
|
|
|
|
|
|
|
def RemoveService(self, service):
|
|
|
|
query = f"""
|
|
|
|
DELETE FROM services
|
|
|
|
WHERE date == '{service}'
|
|
|
|
"""
|
2021-11-07 11:56:41 +00:00
|
|
|
self.cursor.execute(query)
|
|
|
|
self.db.commit()
|
|
|
|
|
2021-11-07 21:40:34 +00:00
|
|
|
class UserManager():
|
|
|
|
def __init__(self, db) -> None:
|
|
|
|
self.db = db
|
|
|
|
self.cursor = self.db.cursor()
|
|
|
|
self.PopulateDB()
|
|
|
|
|
|
|
|
def PopulateDB(self):
|
|
|
|
'''
|
|
|
|
Create db for user managment. Password is stored as a hash
|
|
|
|
'''
|
|
|
|
query = f"""CREATE TABLE IF NOT EXISTS users
|
|
|
|
(
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
2021-11-10 21:00:18 +00:00
|
|
|
name STRING UNIQUE,
|
2021-11-07 21:40:34 +00:00
|
|
|
password STRING,
|
|
|
|
salt STRING
|
|
|
|
);
|
|
|
|
"""
|
|
|
|
self.cursor.execute(query)
|
|
|
|
|
|
|
|
def AddUser(self, name, password):
|
|
|
|
salt = uuid.uuid4().hex
|
|
|
|
m = hashlib.sha512()
|
|
|
|
m.update((password + salt).encode("utf-8"))
|
|
|
|
password = m.digest().hex()
|
|
|
|
query = f"""
|
|
|
|
INSERT INTO users (name, password, salt)
|
|
|
|
VALUES('{name}', '{password}', '{salt}');
|
|
|
|
"""
|
|
|
|
self.cursor.execute(query)
|
|
|
|
self.db.commit()
|
|
|
|
|
|
|
|
def Authenticate(self, name, password):
|
|
|
|
us = self.cursor.execute(f"select * from users where name == '{name}';").fetchall()
|
|
|
|
if(us != None and len(us) > 0):
|
|
|
|
us = us[0]
|
|
|
|
m = hashlib.sha512()
|
|
|
|
m.update((password + us[3]).encode("utf-8"))
|
|
|
|
password = m.digest().hex()
|
|
|
|
if(password == us[2]):
|
|
|
|
return User(us[1], us[2], us[0], salt=us[3])
|
|
|
|
return None
|
|
|
|
|
|
|
|
def GetUserByID(self, id):
|
|
|
|
us = self.cursor.execute(f"select * from users where id == {id};").fetchall()
|
|
|
|
if(us != None):
|
|
|
|
us = us[0]
|
|
|
|
return User(us[1], us[2], us[0], salt=us[3])
|
|
|
|
return None
|
|
|
|
|
2021-11-07 11:56:41 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
#fill db
|
2021-11-07 21:40:34 +00:00
|
|
|
db = sqlite3.connect("sqlite.db", check_same_thread=False)
|
|
|
|
ser = Services(db)
|
|
|
|
us = UserManager(db)
|
|
|
|
|
2021-11-10 21:00:18 +00:00
|
|
|
us.AddUser("Eljakim", "Kunnenwe?")
|
2021-11-07 21:40:34 +00:00
|
|
|
us.Authenticate("Eljakim", "Kunnenwe?")
|
|
|
|
us.Authenticate("Eljakim", "Kunnenwe2?")
|
|
|
|
# for i in range(100):
|
|
|
|
# d = datetime.datetime.now()
|
|
|
|
# d = d.replace(hour=random.randint(1, 23), day = random.randint(1, 28), month = random.randint(1, 12))
|
|
|
|
# d = d.strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
# g = ""
|
|
|
|
# for j in range(10):
|
|
|
|
# g += str(random.randint(1, 30))
|
|
|
|
# g += ":"
|
|
|
|
# ser.AddService(d, g)
|
|
|
|
# r = ser.GetAllServices()
|
|
|
|
# d = ser.GetServices(str(4))
|
|
|
|
# print(d)
|
2021-11-07 11:56:41 +00:00
|
|
|
|