diff --git a/app.py b/app.py index d261a78..b1bf1b4 100644 --- a/app.py +++ b/app.py @@ -14,7 +14,7 @@ def main(): # Set app secret key to sign session cookies secret_key = environ.get("APPSECRET") - if secret_key == None: + if secret_key is None: # NO Secret Key set! print("No app secret set, please set one before deploying in production") app.secret_key = "DEFAULTKEY" diff --git a/controllers/database/category.py b/controllers/database/category.py index eeb8525..71fa9b3 100644 --- a/controllers/database/category.py +++ b/controllers/database/category.py @@ -30,7 +30,7 @@ class CategoryController(DatabaseController): ) row = cursor.fetchone() - if row == None: + if row is None: return None params = dict(zip(self.FIELDS, row)) @@ -44,7 +44,7 @@ class CategoryController(DatabaseController): ) rows = cursor.fetchall() - if rows == None: + if rows is None: return None categories = list() diff --git a/controllers/database/database.py b/controllers/database/database.py index aa873e0..86970aa 100644 --- a/controllers/database/database.py +++ b/controllers/database/database.py @@ -17,8 +17,8 @@ class DatabaseController(ABC): 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 + # 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) except sqlite3.Error as e: @@ -28,10 +28,10 @@ class DatabaseController(ABC): print(e) def __del__(self): - if self._conn != None: + if self._conn is not None: self._conn.close() - """ Takes a dictionary of fields and returns the object + """ Takes a dictionary of fields and returns the object with those fields populated """ def new_instance(self, of: type, with_fields: Mapping[str, Any]): diff --git a/controllers/database/product.py b/controllers/database/product.py index 96bbdc3..ac22d2d 100644 --- a/controllers/database/product.py +++ b/controllers/database/product.py @@ -22,7 +22,11 @@ class ProductController(DatabaseController): ] self._conn.execute( - "INSERT INTO Products (name, image, description, cost, categoryID, sellerID, postedDate, quantityAvailable) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", + """ + INSERT INTO Products + (name, image, description, cost, categoryID, + sellerID, postedDate, quantityAvailable) + VALUES (?, ?, ?, ?, ?, ?, ?, ?)""", params ) self._conn.commit() @@ -38,7 +42,7 @@ class ProductController(DatabaseController): ) rows = cursor.fetchmany() - if rows == None: + if rows is None: return None products = list() @@ -51,16 +55,17 @@ class ProductController(DatabaseController): return products - def read_all(self, category: str = "", search_term: str = "") -> list[Product] | None: + 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 - WHERE Categories.name LIKE ? + """SELECT * FROM Products + INNER JOIN Categories ON Products.categoryID = Categories.id + WHERE Categories.name LIKE ? AND Products.name LIKE ? """, params diff --git a/controllers/database/user.py b/controllers/database/user.py index 8534a48..04d7692 100644 --- a/controllers/database/user.py +++ b/controllers/database/user.py @@ -23,7 +23,9 @@ class UserController(DatabaseController): ] self._conn.execute( - "INSERT INTO Users (username, password, first_name, last_name, email, phone, role) VALUES (?, ?, ?, ?, ?, ?, ?)", + """INSERT INTO Users + (username, password, first_name, last_name, email, phone, role) + VALUES (?, ?, ?, ?, ?, ?, ?)""", params ) self._conn.commit() @@ -39,7 +41,7 @@ class UserController(DatabaseController): ) row = cursor.fetchone() - if row != None: + if row is not None: params = dict(zip(self.FIELDS, row)) # Is user a seller @@ -63,7 +65,7 @@ class UserController(DatabaseController): ) row = cursor.fetchone() - if row != None: + if row is not None: params = dict(zip(self.FIELDS, row)) # Is user a seller diff --git a/controllers/web/endpoints.py b/controllers/web/endpoints.py index 533b3d7..1aaed23 100644 --- a/controllers/web/endpoints.py +++ b/controllers/web/endpoints.py @@ -12,7 +12,7 @@ blueprint.register_blueprint(user.blueprint) blueprint.register_blueprint(product.blueprint) -### CONTEXTS ### +# CONTEXTS # # Function that returns a given user class based on the ID in the session @blueprint.context_processor @@ -21,7 +21,7 @@ def get_user() -> dict[User | None]: user_id = session.get('user_id') user = None - if user_id != None: + if user_id is not None: db = UserController() user = db.read_id(user_id) diff --git a/controllers/web/product.py b/controllers/web/product.py index 2c4e317..5513bae 100644 --- a/controllers/web/product.py +++ b/controllers/web/product.py @@ -42,10 +42,13 @@ def index(): products = database.read_all() # No Products visible - if products == None: + if products is None: flash("No Products available") - return render_template('index.html', content="content.html", products=products) + return render_template('index.html', + content="content.html", + products=products + ) # Loads a given product category page @@ -56,17 +59,21 @@ def category(category: str): # Check to see if there is a custome search term search_term = request.args.get("search", type=str) - if search_term != None: + if search_term is not None: print(f"Search Term {search_term}") products = database.read_all(category, search_term) else: products = database.read_all(category) # No Products visible - if products == None: + if products is None: flash(f"No Products available in {category}") - return render_template('index.html', content="content.html", products=products, category=category) + return render_template('index.html', + content="content.html", + products=products, + category=category + ) # Loads a given product based on ID @@ -82,13 +89,13 @@ def display_add_product(): user_id = session.get('user_id') # User must be logged in to view this page - if user_id == None: + if user_id is None: flash("Please Login to view this page") return redirect('/login') db = UserController() user = db.read_id(user_id) - if user == None or user.role != "Seller": + if user is None or user.role != "Seller": flash("You must be logged in as a Seller to view this page") return redirect('/') @@ -101,20 +108,20 @@ def add_product(): user_id = session.get('user_id') # User must be logged in to view this page - if user_id == None: + if user_id is None: flash("Please Login to view this page") return redirect('/login', code=302) db = UserController() user = db.read_id(user_id) - if user == None or user.role != "Seller": + if user is None or user.role != "Seller": flash("You must be logged in as a Seller to perform this action") return redirect('/', code=302) file = request.files.get('image') # Ensure that the correct file type is uploaded - if file == None or not allowed_file(file.filename): + if file is None or not allowed_file(file.filename): flash("Invalid File Uploaded") return redirect("/add") diff --git a/controllers/web/user.py b/controllers/web/user.py index 721e7a3..8efb15b 100644 --- a/controllers/web/user.py +++ b/controllers/web/user.py @@ -28,7 +28,7 @@ def login(): error = None # No user found - if user == None: + if user is None: error = "No user found with the username " + request.form['username'] flash(error) return redirect("/login") @@ -57,7 +57,7 @@ def signup(): database = UserController() # User already exists - if database.read(request.form['username']) != None: + if database.read(request.form['username']) is not None: error = "User, " + request.form['username'] + " already exists" flash(error) return redirect("/signup") diff --git a/models/products/product.py b/models/products/product.py index 6229182..57f9a66 100644 --- a/models/products/product.py +++ b/models/products/product.py @@ -23,8 +23,10 @@ class Product: No additional properties are assigned to the customer ''' - def __init__(self, name: str, image: str, description: str, cost: float, category: int, - seller_id: int, posted_date: datetime, quantity_available: int): + def __init__(self, name: str, image: str, description: str, + cost: float, category: int, seller_id: int, + posted_date: datetime, quantity_available: int + ): self.id = 0 self.name = name self.image = image diff --git a/models/users/user.py b/models/users/user.py index 146bed7..7a3e6f5 100644 --- a/models/users/user.py +++ b/models/users/user.py @@ -2,11 +2,14 @@ from abc import ABC class User(ABC): - """ Functional Class constructor to initialise all properties in the base object - with a value """ + """ Functional Class constructor to initialise all properties in + the base object with a value + """ - def __init__(self, username: str, password: str, firstname: str, - lastname: str, email: str, phone: str, role: str): + def __init__(self, username: str, password: str, + firstname: str, lastname: str, + email: str, phone: str, role: str + ): self.id = 0 self.username = username self.password = password diff --git a/templates/content.html b/templates/content.html index 234e95c..74786fe 100644 --- a/templates/content.html +++ b/templates/content.html @@ -1,7 +1,7 @@