From 3dc7a1f861631c58e91d54e3abe9b48c6e55b8de Mon Sep 17 00:00:00 2001 From: Luke Else Date: Tue, 13 Feb 2024 22:48:23 +0000 Subject: [PATCH] #10 Added ability for users to be removed from the site. --- controllers/database/user.py | 9 +++-- controllers/web/admin.py | 4 +-- controllers/web/user.py | 14 ++++++++ scripts/test_data.sql | 4 +-- static/css/buttons.css | 66 ++++++++++++++++++++++++++++++++++++ static/css/products.css | 5 +++ templates/admin.html | 43 +++++++++++++++++++++-- templates/content.html | 9 ++++- templates/header.html | 22 ++++++++---- templates/index.html | 1 + templates/product.html | 25 +++++++++++--- 11 files changed, 182 insertions(+), 20 deletions(-) create mode 100644 static/css/buttons.css diff --git a/controllers/database/user.py b/controllers/database/user.py index eadaad3..46b7a84 100644 --- a/controllers/database/user.py +++ b/controllers/database/user.py @@ -72,5 +72,10 @@ class UserController(DatabaseController): def update(self): print("Doing work") - def delete(self): - print("Doing work") + def delete(self, id: int): + params = [ + id + ] + query = """ DELETE FROM Users WHERE id = ? """ + + return self.do(query, params) diff --git a/controllers/web/admin.py b/controllers/web/admin.py index 5f47ed2..99d16bb 100644 --- a/controllers/web/admin.py +++ b/controllers/web/admin.py @@ -11,13 +11,13 @@ blueprint = Blueprint("admin", __name__, url_prefix="/admin") @blueprint.route('/') -def main_admin(): +def main(): """ Function responsible for delivering the admin page for the site """ return "Hello, World" @blueprint.route('/users/') -def admin_users(): +def users(): """ Endpoint responsible for managing a users permissions """ # Get all users to create admin table on frontend db = UserController() diff --git a/controllers/web/user.py b/controllers/web/user.py index d619b16..8bffa53 100644 --- a/controllers/web/user.py +++ b/controllers/web/user.py @@ -7,6 +7,7 @@ from flask import render_template, redirect, request, session, flash from controllers.database.user import UserController from models.users.customer import Customer from models.users.seller import Seller +from utils.user_utils import is_role from hashlib import sha512 # Blueprint to append user endpoints to @@ -96,3 +97,16 @@ def logout(): # Clear the current user from the session if they are logged in session.pop('user_id', None) return redirect("/") + + +# DELETE USER FUNCTIONALITY +@blueprint.post('/delete/') +def delete(id: int): + """ Function responsible for deleting users from the site """ + if not is_role("Admin"): + flash("You must be logged in an admin to remove users!", "error") + return redirect("/") + + db = UserController() + db.delete(id) + return redirect("/admin/users/") diff --git a/scripts/test_data.sql b/scripts/test_data.sql index ec62cbd..59328c3 100644 --- a/scripts/test_data.sql +++ b/scripts/test_data.sql @@ -1,6 +1,6 @@ -INSERT INTO Users (first_name, last_name, username, email, phone, password, role) VALUES ("Luke", "Else", "luke-else", "test@test.com", "07498 289321", "cbe0cd68cbca3868250c0ba545c48032f43eb0e8a5e6bab603d109251486f77a91e46a3146d887e37416c6bdb6cbe701bd514de778573c9b0068483c1c626aec", "Seller"); -INSERT INTO Users (first_name, last_name, username, email, phone, password, role) VALUES ("Luke", "Else", "test-customer", "test@test.net", "07498 289322", "cbe0cd68cbca3868250c0ba545c48032f43eb0e8a5e6bab603d109251486f77a91e46a3146d887e37416c6bdb6cbe701bd514de778573c9b0068483c1c626aec", "Customer"); INSERT INTO Users (first_name, last_name, username, email, phone, password, role) VALUES ("Luke", "Else", "test-seller", "test@test.not", "07498 289323", "cbe0cd68cbca3868250c0ba545c48032f43eb0e8a5e6bab603d109251486f77a91e46a3146d887e37416c6bdb6cbe701bd514de778573c9b0068483c1c626aec", "Seller"); +INSERT INTO Users (first_name, last_name, username, email, phone, password, role) VALUES ("Luke", "Else", "test-customer", "test@test.net", "07498 289322", "cbe0cd68cbca3868250c0ba545c48032f43eb0e8a5e6bab603d109251486f77a91e46a3146d887e37416c6bdb6cbe701bd514de778573c9b0068483c1c626aec", "Customer"); +INSERT INTO Users (first_name, last_name, username, email, phone, password, role) VALUES ("Luke", "Else", "luke-else", "test@test.com", "07498 289321", "cbe0cd68cbca3868250c0ba545c48032f43eb0e8a5e6bab603d109251486f77a91e46a3146d887e37416c6bdb6cbe701bd514de778573c9b0068483c1c626aec", "Admin"); INSERT INTO Products (name, image, description, cost, sellerID, categoryID, quantityAvailable, postedDate) VALUES ("12' Brake Disks", "brake-disks.bmp", "this is a product", 20.99, 1, 1, 10, datetime()); INSERT INTO Products (name, image, description, cost, sellerID, categoryID, quantityAvailable, postedDate) VALUES ("Exhaust Manifold", "manifold.bmp", "This is a super cool product that can be installed into your car to take the gasses from the inside all the way to the outside. Mad I know.", 20.99, 1, 1, 9, datetime()); diff --git a/static/css/buttons.css b/static/css/buttons.css new file mode 100644 index 0000000..db520f4 --- /dev/null +++ b/static/css/buttons.css @@ -0,0 +1,66 @@ +@import url(https://fonts.googleapis.com/css?family=Roboto:700); + +:root { + --btn-width: 100%; +} + +.button { + font-size: medium; + font-weight: bold; + width : var(--btn-width); + height : 50px; + overflow: hidden; + text-align : center; + transition : .2s; + cursor : pointer; + border-radius: 3px; + box-shadow: 0px 1px 2px rgba(0,0,0,.2); +} +.btnTwo { + position : relative; + width : 200px; + height : 100px; + margin-top: -100px; + padding-top: 2px; + background : rgba(0, 0, 0, 0.1); + left : -250px; + transition : .3s; +} +.btnText { + color : white; + transition : .3s; +} +.btnText2 { + margin-top : 63px; + margin-right : -130px; + color : #FFF; +} +.button:hover .btnTwo{ /*When hovering over .button change .btnTwo*/ + left: -130px; +} +.button:hover .btnText{ /*When hovering over .button change .btnText*/ + margin-left : 65px; +} +.button:active { /*Clicked and held*/ + box-shadow: 0px 5px 6px rgba(0,0,0,0.3); +} + +.button.error { + background-color: var(--red); +} + +.button.success { + background: var(--green); +} + +.button.info { + background-color: #2879c550; +} + +.button.wmgzon { + background-color: orange; +} + +.button.neutral { + background-color: rgba(0, 0, 0, 0.3); +} diff --git a/static/css/products.css b/static/css/products.css index 920dc50..8ad60de 100644 --- a/static/css/products.css +++ b/static/css/products.css @@ -96,6 +96,10 @@ gap: 1rem 1rem; } +.product-selection { + width: 60%; +} + .product-description { font-size: 70%; } @@ -140,6 +144,7 @@ justify-content: space-around; flex-wrap: wrap; align-items: center; + gap: .5em; } .product-quantity { font-size: 50%; diff --git a/templates/admin.html b/templates/admin.html index 5bafd60..09eb90c 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -1,4 +1,6 @@ + + {% if users != None %}

Showing {{users|count}} users

@@ -6,20 +8,57 @@ + - + + {% for user in users %} + - + + + + + +
# Username E-Mail Phone NumberBUTTONRoleActions
{{user.id}} {{user.username}} {{user.email}} {{user.phone}}{{user.phone}}{{user.role}} +
+
+

Edit User

+
+

{{user.id}}

+
+
+ +
+