104 lines
3.8 KiB
Python
104 lines
3.8 KiB
Python
import sqlite3
|
|
from datetime import datetime
|
|
from models.database import Database
|
|
from services.encryption import Encryption
|
|
|
|
class User:
|
|
def __init__(self, connection : sqlite3.Connection, id = None, username = 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.username = username
|
|
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 = self.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 load_by_username(self):
|
|
cur = self.connection.cursor()
|
|
rows = cur.execute("SELECT * FROM users").fetchall()
|
|
|
|
found_user = None
|
|
for row in rows:
|
|
user = User(Database.connection)._set_row_values(row)
|
|
if user.username == self.username:
|
|
self._set_row_values(row)
|
|
found_user = user
|
|
break
|
|
|
|
cur.close()
|
|
if found_user:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
def save(self):
|
|
cur = self.connection.cursor()
|
|
current_date = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
|
|
cur.execute("""
|
|
INSERT INTO users
|
|
(id, username, firstname, lastname, address, zipcode, city_id, email, phone, password, role, created) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
""", (self.id, Encryption.encrypt(self.username), Encryption.encrypt(self.firstname), Encryption.encrypt(self.lastname), Encryption.encrypt(self.address), Encryption.encrypt(self.zipcode), self.city_id, Encryption.encrypt(self.email), Encryption.encrypt(self.phone), Encryption.encrypt(self.password), Encryption.encrypt(self.role), current_date))
|
|
|
|
self.connection.commit()
|
|
cur.close()
|
|
return True
|
|
|
|
def update(self):
|
|
cur = self.connection.cursor()
|
|
cur.execute("""
|
|
UPDATE users SET
|
|
username = ?,
|
|
firstname = ?,
|
|
lastname = ?,
|
|
address = ?,
|
|
zipcode = ?,
|
|
city_id = ?,
|
|
email = ?,
|
|
phone = ?,
|
|
password = ?,
|
|
role = ?
|
|
WHERE id = ?
|
|
""", (Encryption.encrypt(self.username), Encryption.encrypt(self.firstname), Encryption.encrypt(self.lastname), Encryption.encrypt(self.address), Encryption.encrypt(self.zipcode), self.city_id, Encryption.encrypt(self.email), Encryption.encrypt(self.phone), Encryption.encrypt(self.password), self.role, self.id))
|
|
|
|
self.connection.commit()
|
|
cur.close()
|
|
return True
|
|
|
|
def delete(self):
|
|
cur = self.connection.cursor()
|
|
cur.execute("""DELETE FROM users WHERE id = ?""", (self.id))
|
|
self.connection.commit()
|
|
cur.close()
|
|
return True
|
|
|
|
def _set_row_values(self, row):
|
|
self.id = row[0]
|
|
self.username = Encryption.decrypt(row[1])
|
|
self.firstname = Encryption.decrypt(row[2])
|
|
self.lastname = Encryption.decrypt(row[3])
|
|
self.address = Encryption.decrypt(row[4])
|
|
self.zipcode = Encryption.decrypt(row[5])
|
|
self.city_id = row[6]
|
|
self.email = Encryption.decrypt(row[7])
|
|
self.phone = Encryption.decrypt(row[8])
|
|
self.password = Encryption.decrypt(row[9])
|
|
self.role = Encryption.decrypt(row[10])
|
|
self.created = row[11]
|
|
return self |