initial build
This commit is contained in:
0
models/__init__.py
Normal file
0
models/__init__.py
Normal file
58
models/city.py
Normal file
58
models/city.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import sqlite3
|
||||
|
||||
class City:
|
||||
def __init__(self, connection : sqlite3.Connection, id, name):
|
||||
self.connection = connection
|
||||
self.id = id
|
||||
self.name = name
|
||||
|
||||
def load_by_id(self):
|
||||
cur = self.connection.cursor()
|
||||
row = cur.execute("SELECT * FROM cities WHERE id = ?", (self.id,)).fetchone()
|
||||
|
||||
if row == None:
|
||||
return False
|
||||
|
||||
self._set_row_values(row)
|
||||
|
||||
cur.close()
|
||||
return True
|
||||
|
||||
def load_by_name(self):
|
||||
cur = self.connection.cursor()
|
||||
row = cur.execute("SELECT * FROM cities WHERE name = ?", (self.name,)).fetchone()
|
||||
|
||||
if row == None:
|
||||
return False
|
||||
|
||||
self._set_row_values(row)
|
||||
|
||||
cur.close()
|
||||
return True
|
||||
|
||||
def save(self):
|
||||
cur = self.connection.cursor()
|
||||
cur.execute("""
|
||||
INSERT INTO cities
|
||||
(id, name) VALUES (?, ?)
|
||||
""", (self.id, self.name))
|
||||
|
||||
self.connection.commit()
|
||||
cur.close()
|
||||
return True
|
||||
|
||||
def update(self):
|
||||
cur = self.connection.cursor()
|
||||
cur.execute("""
|
||||
UPDATE cities SET
|
||||
name = ?,
|
||||
WHERE id = ?
|
||||
""", (self.name, self.id))
|
||||
|
||||
self.connection.commit()
|
||||
cur.close()
|
||||
return True
|
||||
|
||||
def _set_row_values(self, row):
|
||||
self.id = row[0]
|
||||
self.name = row[1]
|
63
models/database.py
Normal file
63
models/database.py
Normal file
@@ -0,0 +1,63 @@
|
||||
from multiprocessing import connection
|
||||
import sqlite3
|
||||
|
||||
from models.city import City
|
||||
|
||||
class Database:
|
||||
connection: sqlite3.Connection = None
|
||||
|
||||
@staticmethod
|
||||
def init():
|
||||
# Open connection with database
|
||||
con = sqlite3.connect("./database.db")
|
||||
Database.connection = con
|
||||
|
||||
Database._init_log_table()
|
||||
Database._init_user_table()
|
||||
Database._init_city_table()
|
||||
|
||||
@staticmethod
|
||||
def _init_log_table():
|
||||
cursor = Database.connection.cursor()
|
||||
q = cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS logs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username TEXT,
|
||||
date TEXT,
|
||||
time TEXT,
|
||||
description TEXT,
|
||||
additional_information TEXT,
|
||||
suspicious BOOL
|
||||
)
|
||||
""")
|
||||
cursor.close()
|
||||
|
||||
@staticmethod
|
||||
def _init_user_table():
|
||||
cursor = Database.connection.cursor()
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY,
|
||||
firstname TEXT,
|
||||
lastname TEXT,
|
||||
address TEXT,
|
||||
zipcode TEXT,
|
||||
city_id INTEGER,
|
||||
email TEXT,
|
||||
phone TEXT,
|
||||
password TEXT,
|
||||
role TEXT
|
||||
)
|
||||
""")
|
||||
cursor.close()
|
||||
|
||||
@staticmethod
|
||||
def _init_city_table():
|
||||
cursor = Database.connection.cursor()
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS cities (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT
|
||||
)
|
||||
""")
|
||||
cursor.close()
|
61
models/log.py
Normal file
61
models/log.py
Normal file
@@ -0,0 +1,61 @@
|
||||
import sqlite3
|
||||
|
||||
|
||||
class Log:
|
||||
def __init__(self, connection : sqlite3.Connection, id = None, username = None, date = None, time = None, description = None, additional_information = None, suspicious = None):
|
||||
self.connection = connection
|
||||
self.id = id
|
||||
self.username = username
|
||||
self.date = date
|
||||
self.time = time
|
||||
self.description = description
|
||||
self.additional_information = additional_information
|
||||
self.suspicious = suspicious
|
||||
|
||||
def load_by_id(self):
|
||||
cur = self.connection.cursor()
|
||||
row = cur.execute("SELECT * FROM logs WHERE id = ?", (self.id,)).fetchone()
|
||||
|
||||
if row == None:
|
||||
return False
|
||||
|
||||
self._set_row_values(row)
|
||||
|
||||
cur.close()
|
||||
return True
|
||||
|
||||
def save(self):
|
||||
cur = self.connection.cursor()
|
||||
cur.execute("""
|
||||
INSERT INTO logs
|
||||
(id, username, date, time, description, additional_information, suspicious) VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
""", (self.id, self.username, self.date, self.time, self.description, self.additional_information, self.suspicious))
|
||||
|
||||
self.connection.commit()
|
||||
cur.close()
|
||||
return True
|
||||
|
||||
def update(self):
|
||||
cur = self.connection.cursor()
|
||||
cur.execute("""
|
||||
UPDATE logs SET
|
||||
username = ?,
|
||||
date = ?,
|
||||
description = ?,
|
||||
additional_information = ?,
|
||||
suspicious = ?,
|
||||
WHERE id = ?
|
||||
""", (self.firstname, self.lastname, self.zipcode, self.city_id, self.email, self.phone, self.password, self.role, self.id))
|
||||
|
||||
self.connection.commit()
|
||||
cur.close()
|
||||
return True
|
||||
|
||||
def _set_row_values(self, row):
|
||||
self.id = row[0]
|
||||
self.username = row[1]
|
||||
self.date = row[2]
|
||||
self.time = row[3]
|
||||
self.description = row[4]
|
||||
self.additional_information = row[5]
|
||||
self.suspicious = row[6]
|
72
models/user.py
Normal file
72
models/user.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import sqlite3
|
||||
from models.database import Database
|
||||
|
||||
class User:
|
||||
def __init__(self, connection : sqlite3.Connection, id = None, firstname = None, lastname = None, address = None, zipcode = None, city_id = None, email = None, phone = None, password = None, role = None):
|
||||
self.connection = connection
|
||||
self.id = id
|
||||
self.firstname = firstname
|
||||
self.lastname = lastname
|
||||
self.address = address
|
||||
self.zipcode = zipcode
|
||||
self.city_id = city_id
|
||||
self.email = email
|
||||
self.phone = phone
|
||||
self.password = password
|
||||
self.role = role
|
||||
|
||||
def load_by_id(self):
|
||||
cur = Database.connection.cursor()
|
||||
row = cur.execute("SELECT * FROM users WHERE id = ?", (self.id,)).fetchone()
|
||||
|
||||
if row == None:
|
||||
return False
|
||||
|
||||
self._set_row_values(row)
|
||||
|
||||
cur.close()
|
||||
return True
|
||||
|
||||
def save(self):
|
||||
cur = Database.connection.cursor()
|
||||
cur.execute("""
|
||||
INSERT INTO users
|
||||
(id, fistname, lastname, address, zipcode, city_id, email, phone, password, role) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""", (self.id, self.firstname, self.lastname, self.zipcode, self.city_id, self.email, self.phone, self.password, self.role))
|
||||
|
||||
Database.connection.commit()
|
||||
cur.close()
|
||||
return True
|
||||
|
||||
def update(self):
|
||||
cur = Database.connection.cursor()
|
||||
cur.execute("""
|
||||
UPDATE users SET
|
||||
fistname = ?,
|
||||
lastname = ?,
|
||||
address = ?,
|
||||
zipcode = ?,
|
||||
city_id = ?,
|
||||
email = ?,
|
||||
phone = ?,
|
||||
password = ?,
|
||||
role = ?,
|
||||
WHERE id = ?
|
||||
""", (self.firstname, self.lastname, self.zipcode, self.city_id, self.email, self.phone, self.password, self.role, self.id))
|
||||
|
||||
Database.connection.commit()
|
||||
cur.close()
|
||||
return True
|
||||
|
||||
def _set_row_values(self, row):
|
||||
self.id = row[0]
|
||||
self.firstname = row[1]
|
||||
self.lastname = row[2]
|
||||
self.address = row[3]
|
||||
self.zipcode = row[4]
|
||||
self.city_id = row[5]
|
||||
self.email = row[6]
|
||||
self.phone = row[7]
|
||||
self.password = row[8]
|
||||
self.role = row[9]
|
||||
|
46
models/utils.py
Normal file
46
models/utils.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from models.city import City
|
||||
from models.database import Database
|
||||
|
||||
class DatabaseUtils:
|
||||
|
||||
@staticmethod
|
||||
def init_city_data():
|
||||
new_city = City(Database.connection, None, "Haastrecht")
|
||||
if not new_city.load_by_name():
|
||||
new_city.save()
|
||||
|
||||
new_city = City(Database.connection, None, "Gouda")
|
||||
if not new_city.load_by_name():
|
||||
new_city.save()
|
||||
|
||||
new_city = City(Database.connection, None, "Rotterdam")
|
||||
if not new_city.load_by_name():
|
||||
new_city.save()
|
||||
|
||||
new_city = City(Database.connection, None, "Bruinisse")
|
||||
if not new_city.load_by_name():
|
||||
new_city.save()
|
||||
|
||||
new_city = City(Database.connection, None, "Amsterdam")
|
||||
if not new_city.load_by_name():
|
||||
new_city.save()
|
||||
|
||||
new_city = City(Database.connection, None, "Goes")
|
||||
if not new_city.load_by_name():
|
||||
new_city.save()
|
||||
|
||||
new_city = City(Database.connection, None, "Zoetermeer")
|
||||
if not new_city.load_by_name():
|
||||
new_city.save()
|
||||
|
||||
new_city = City(Database.connection, None, "Breda")
|
||||
if not new_city.load_by_name():
|
||||
new_city.save()
|
||||
|
||||
new_city = City(Database.connection, None, "Eindhoven")
|
||||
if not new_city.load_by_name():
|
||||
new_city.save()
|
||||
|
||||
new_city = City(Database.connection, None, "Almere")
|
||||
if not new_city.load_by_name():
|
||||
new_city.save()
|
Reference in New Issue
Block a user