Applied auto pep 8 changes
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
from .database import DatabaseController
|
||||
from models.category import Category
|
||||
|
||||
|
||||
class CategoryController(DatabaseController):
|
||||
FIELDS = ['id', 'name']
|
||||
|
||||
@ -18,7 +19,6 @@ class CategoryController(DatabaseController):
|
||||
)
|
||||
self._conn.commit()
|
||||
|
||||
|
||||
def read(self, id: int = 0) -> Category | None:
|
||||
params = [
|
||||
id
|
||||
@ -32,12 +32,11 @@ class CategoryController(DatabaseController):
|
||||
|
||||
if row == None:
|
||||
return None
|
||||
|
||||
|
||||
params = dict(zip(self.FIELDS, row))
|
||||
obj = self.new_instance(Category, params)
|
||||
|
||||
return obj
|
||||
|
||||
return obj
|
||||
|
||||
def read_all(self) -> list[Category] | None:
|
||||
cursor = self._conn.execute(
|
||||
@ -47,18 +46,18 @@ class CategoryController(DatabaseController):
|
||||
|
||||
if rows == None:
|
||||
return None
|
||||
|
||||
|
||||
categories = list()
|
||||
|
||||
for category in rows:
|
||||
params = dict(zip(self.FIELDS, category))
|
||||
obj = self.new_instance(Category, params)
|
||||
categories.append(obj)
|
||||
|
||||
|
||||
return categories
|
||||
|
||||
def update(self):
|
||||
print("Doing work")
|
||||
|
||||
|
||||
def delete(self):
|
||||
print("Doing work")
|
||||
print("Doing work")
|
||||
|
@ -1,8 +1,9 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Mapping, Any
|
||||
from typing import Mapping, Any
|
||||
import sqlite3
|
||||
import os
|
||||
|
||||
|
||||
class DatabaseController(ABC):
|
||||
__data_dir = "./data/"
|
||||
__db_name = "wmgzon.db"
|
||||
@ -13,13 +14,13 @@ class DatabaseController(ABC):
|
||||
|
||||
__sqlitefile = __data_dir + __db_name
|
||||
|
||||
|
||||
def __init__(self):
|
||||
self._conn = None
|
||||
try:
|
||||
# Creates a connection and specifies a flag to parse all types back down into
|
||||
# Python declared types e.g. date & time
|
||||
self._conn = sqlite3.connect(self.__sqlitefile, detect_types=sqlite3.PARSE_DECLTYPES)
|
||||
self._conn = sqlite3.connect(
|
||||
self.__sqlitefile, detect_types=sqlite3.PARSE_DECLTYPES)
|
||||
except sqlite3.Error as e:
|
||||
# Close the connection if still open
|
||||
if self._conn:
|
||||
@ -27,17 +28,18 @@ class DatabaseController(ABC):
|
||||
print(e)
|
||||
|
||||
def __del__(self):
|
||||
if self._conn != None:
|
||||
if self._conn != None:
|
||||
self._conn.close()
|
||||
|
||||
""" Takes a dictionary of fields and returns the object
|
||||
with those fields populated """
|
||||
|
||||
def new_instance(self, of: type, with_fields: Mapping[str, Any]):
|
||||
obj = of.__new__(of)
|
||||
for attr, value in with_fields.items():
|
||||
setattr(obj, attr, value)
|
||||
return obj
|
||||
|
||||
|
||||
"""
|
||||
Set of CRUD methods to allow for Data manipulation on the backend
|
||||
"""
|
||||
@ -53,7 +55,7 @@ class DatabaseController(ABC):
|
||||
@abstractmethod
|
||||
def update(self):
|
||||
pass
|
||||
|
||||
|
||||
@abstractmethod
|
||||
def delete(self):
|
||||
pass
|
||||
pass
|
||||
|
@ -1,8 +1,10 @@
|
||||
from .database import DatabaseController
|
||||
from models.products.product import Product
|
||||
|
||||
|
||||
class ProductController(DatabaseController):
|
||||
FIELDS = ['id', 'name', 'image', 'description', 'cost', 'category', 'sellerID', 'postedDate', 'quantityAvailable']
|
||||
FIELDS = ['id', 'name', 'image', 'description', 'cost',
|
||||
'category', 'sellerID', 'postedDate', 'quantityAvailable']
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
@ -25,7 +27,6 @@ class ProductController(DatabaseController):
|
||||
)
|
||||
self._conn.commit()
|
||||
|
||||
|
||||
def read(self, name: str = "") -> list[Product] | None:
|
||||
params = [
|
||||
"%" + name + "%"
|
||||
@ -39,24 +40,23 @@ class ProductController(DatabaseController):
|
||||
|
||||
if rows == None:
|
||||
return None
|
||||
|
||||
|
||||
products = list()
|
||||
|
||||
|
||||
# Create an object for each row
|
||||
for product in rows:
|
||||
params = dict(zip(self.FIELDS, product))
|
||||
obj = self.new_instance(Product, params)
|
||||
products.append(obj)
|
||||
|
||||
return products
|
||||
|
||||
return products
|
||||
|
||||
def read_all(self, category: str = "", search_term: str = "") -> list[Product] | None:
|
||||
params = [
|
||||
"%" + category + "%",
|
||||
"%" + search_term + "%"
|
||||
]
|
||||
|
||||
|
||||
cursor = self._conn.execute(
|
||||
"""SELECT * FROM Products
|
||||
INNER JOIN Categories ON Products.categoryID = Categories.id
|
||||
@ -69,19 +69,19 @@ class ProductController(DatabaseController):
|
||||
|
||||
if len(rows) == 0:
|
||||
return None
|
||||
|
||||
|
||||
products = list()
|
||||
|
||||
|
||||
# Create an object for each row
|
||||
for product in rows:
|
||||
params = dict(zip(self.FIELDS, product))
|
||||
obj = self.new_instance(Product, params)
|
||||
products.append(obj)
|
||||
|
||||
|
||||
return products
|
||||
|
||||
def update(self):
|
||||
print("Doing work")
|
||||
|
||||
|
||||
def delete(self):
|
||||
print("Doing work")
|
||||
print("Doing work")
|
||||
|
@ -3,8 +3,10 @@ from models.users.user import User
|
||||
from models.users.customer import Customer
|
||||
from models.users.seller import Seller
|
||||
|
||||
|
||||
class UserController(DatabaseController):
|
||||
FIELDS = ['id', 'username', 'password', 'firstName', 'lastName', 'email', 'phone', 'role']
|
||||
FIELDS = ['id', 'username', 'password', 'firstName',
|
||||
'lastName', 'email', 'phone', 'role']
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
@ -44,13 +46,12 @@ class UserController(DatabaseController):
|
||||
type = Customer
|
||||
if row[7] == "Seller":
|
||||
type = Seller
|
||||
|
||||
|
||||
obj = self.new_instance(type, params)
|
||||
return obj
|
||||
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def read_id(self, id: int) -> User | None:
|
||||
params = [
|
||||
id
|
||||
@ -69,13 +70,14 @@ class UserController(DatabaseController):
|
||||
type = Customer
|
||||
if row[7] == "Seller":
|
||||
type = Seller
|
||||
|
||||
|
||||
obj = self.new_instance(type, params)
|
||||
return obj
|
||||
|
||||
|
||||
return None
|
||||
|
||||
def update(self):
|
||||
print("Doing work")
|
||||
|
||||
|
||||
def delete(self):
|
||||
print("Doing work")
|
||||
print("Doing work")
|
||||
|
Reference in New Issue
Block a user