import datetime import sqlite3 import locale import random import hashlib import uuid from werkzeug.datastructures import Authorization from models import User locale.setlocale(locale.LC_TIME, "nl_NL.utf-8") class Services: def __init__(self, db) -> None: self.db = db 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, date STRING UNIQUE, groups STRING ); """ self.cursor.execute(query) def AddService(self, date, groups): query = f""" INSERT INTO services (date, groups) VALUES('{date}', '{groups}'); """ 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}' """ self.cursor.execute(query) self.db.commit() 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, name STRING UNIQUE, 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 if __name__ == "__main__": #fill db db = sqlite3.connect("sqlite.db", check_same_thread=False) ser = Services(db) us = UserManager(db) us.AddUser("Eljakim", "Kunnenwe?") 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)