From f53d50ef7a92b3d986460730e36f2a0cceec5ef5 Mon Sep 17 00:00:00 2001 From: Stef Date: Wed, 16 Apr 2025 08:50:52 +0200 Subject: [PATCH] Added dashboard per user functionality --- application/auth/models.py | 2 ++ application/dash/models.py | 5 ++++- application/dash/views.py | 14 ++++++-------- seed.py | 10 ++-------- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/application/auth/models.py b/application/auth/models.py index ab4ce40..554a4fc 100644 --- a/application/auth/models.py +++ b/application/auth/models.py @@ -8,6 +8,8 @@ class User(db.Model, UserMixin): password = db.Column(db.String(150), nullable=False) is_admin = db.Column(db.Boolean, default=False) + services = db.relationship("Service", backref="user", lazy="joined") + def __init__(self, username: str, password: str, is_admin: bool = False): self.username = username self.password = password diff --git a/application/dash/models.py b/application/dash/models.py index 8789483..bd95027 100644 --- a/application/dash/models.py +++ b/application/dash/models.py @@ -6,6 +6,9 @@ class Service(db.Model): name = db.Column(db.String, nullable=False) url = db.Column(db.String, nullable=False) - def __init__(self, name: str, url: str): + user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False) + + def __init__(self, name: str, url: str, user_id: int): self.name = name self.url = url + self.user_id = user_id diff --git a/application/dash/views.py b/application/dash/views.py index 60dd485..48c9322 100644 --- a/application/dash/views.py +++ b/application/dash/views.py @@ -1,7 +1,7 @@ from application import db from flask import Blueprint, render_template from application.dash.forms import RegisterForm, ServiceForm -from flask_login import login_required # type: ignore +from flask_login import login_required, current_user # type: ignore from application.dash.models import Service from application.auth.models import User from application.decorators import admin_required @@ -15,7 +15,7 @@ dash_blueprint = Blueprint("dash", __name__, template_folder="templates") @dash_blueprint.route("/", methods=["GET", "POST"]) @login_required def index(): - services = Service.query.all() # type: ignore + services = current_user.services # type: ignore return render_template("dashboard.html", services=services) @@ -24,7 +24,7 @@ def index(): def admin(): register_form = RegisterForm() - if register_form.validate_on_submit(): + if register_form.validate_on_submit(): # type: ignore username = register_form.username.data password = register_form.password.data confirm_password = register_form.confirm_password.data @@ -55,6 +55,7 @@ def admin(): ) return render_template("admin.html", form=register_form) + @dash_blueprint.route("/service", methods=["GET", "POST"]) @login_required def service(): @@ -63,15 +64,12 @@ def service(): if service_form.validate_on_submit(): name = service_form.name.data url = service_form.url.data - new_service = Service( - name=name, - url=url, - ) + new_service = Service(name=name, url=url, user_id=current_user.id) db.session.add(new_service) db.session.commit() return render_template( "add_service.html", form=ServiceForm(formdata=None), - feedback="Service succesfully added" + feedback="Service succesfully added", ) return render_template("add_service.html", form=service_form) diff --git a/seed.py b/seed.py index 41ba238..f85fce5 100644 --- a/seed.py +++ b/seed.py @@ -25,14 +25,8 @@ new_users = [ ] new_services = [ - Service( - name="test123", - url="http://google.com", - ), - Service( - name="Netflix", - url="https://www.netflix.com", - ), + Service(name="test123", url="http://google.com", user_id=1), + Service(name="Netflix", url="https://www.netflix.com", user_id=2), ] with app.app_context():