mirror of
https://github.com/StefBuwalda/WebTech.git
synced 2025-10-30 11:19:58 +00:00
Added dashboard per user functionality
This commit is contained in:
@@ -8,6 +8,8 @@ class User(db.Model, UserMixin):
|
|||||||
password = db.Column(db.String(150), nullable=False)
|
password = db.Column(db.String(150), nullable=False)
|
||||||
is_admin = db.Column(db.Boolean, default=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):
|
def __init__(self, username: str, password: str, is_admin: bool = False):
|
||||||
self.username = username
|
self.username = username
|
||||||
self.password = password
|
self.password = password
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ class Service(db.Model):
|
|||||||
name = db.Column(db.String, nullable=False)
|
name = db.Column(db.String, nullable=False)
|
||||||
url = 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.name = name
|
||||||
self.url = url
|
self.url = url
|
||||||
|
self.user_id = user_id
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from application import db
|
from application import db
|
||||||
from flask import Blueprint, render_template
|
from flask import Blueprint, render_template
|
||||||
from application.dash.forms import RegisterForm, ServiceForm
|
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.dash.models import Service
|
||||||
from application.auth.models import User
|
from application.auth.models import User
|
||||||
from application.decorators import admin_required
|
from application.decorators import admin_required
|
||||||
@@ -15,7 +15,7 @@ dash_blueprint = Blueprint("dash", __name__, template_folder="templates")
|
|||||||
@dash_blueprint.route("/", methods=["GET", "POST"])
|
@dash_blueprint.route("/", methods=["GET", "POST"])
|
||||||
@login_required
|
@login_required
|
||||||
def index():
|
def index():
|
||||||
services = Service.query.all() # type: ignore
|
services = current_user.services # type: ignore
|
||||||
return render_template("dashboard.html", services=services)
|
return render_template("dashboard.html", services=services)
|
||||||
|
|
||||||
|
|
||||||
@@ -24,7 +24,7 @@ def index():
|
|||||||
def admin():
|
def admin():
|
||||||
register_form = RegisterForm()
|
register_form = RegisterForm()
|
||||||
|
|
||||||
if register_form.validate_on_submit():
|
if register_form.validate_on_submit(): # type: ignore
|
||||||
username = register_form.username.data
|
username = register_form.username.data
|
||||||
password = register_form.password.data
|
password = register_form.password.data
|
||||||
confirm_password = register_form.confirm_password.data
|
confirm_password = register_form.confirm_password.data
|
||||||
@@ -55,6 +55,7 @@ def admin():
|
|||||||
)
|
)
|
||||||
return render_template("admin.html", form=register_form)
|
return render_template("admin.html", form=register_form)
|
||||||
|
|
||||||
|
|
||||||
@dash_blueprint.route("/service", methods=["GET", "POST"])
|
@dash_blueprint.route("/service", methods=["GET", "POST"])
|
||||||
@login_required
|
@login_required
|
||||||
def service():
|
def service():
|
||||||
@@ -63,15 +64,12 @@ def service():
|
|||||||
if service_form.validate_on_submit():
|
if service_form.validate_on_submit():
|
||||||
name = service_form.name.data
|
name = service_form.name.data
|
||||||
url = service_form.url.data
|
url = service_form.url.data
|
||||||
new_service = Service(
|
new_service = Service(name=name, url=url, user_id=current_user.id)
|
||||||
name=name,
|
|
||||||
url=url,
|
|
||||||
)
|
|
||||||
db.session.add(new_service)
|
db.session.add(new_service)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return render_template(
|
return render_template(
|
||||||
"add_service.html",
|
"add_service.html",
|
||||||
form=ServiceForm(formdata=None),
|
form=ServiceForm(formdata=None),
|
||||||
feedback="Service succesfully added"
|
feedback="Service succesfully added",
|
||||||
)
|
)
|
||||||
return render_template("add_service.html", form=service_form)
|
return render_template("add_service.html", form=service_form)
|
||||||
|
|||||||
10
seed.py
10
seed.py
@@ -25,14 +25,8 @@ new_users = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
new_services = [
|
new_services = [
|
||||||
Service(
|
Service(name="test123", url="http://google.com", user_id=1),
|
||||||
name="test123",
|
Service(name="Netflix", url="https://www.netflix.com", user_id=2),
|
||||||
url="http://google.com",
|
|
||||||
),
|
|
||||||
Service(
|
|
||||||
name="Netflix",
|
|
||||||
url="https://www.netflix.com",
|
|
||||||
),
|
|
||||||
]
|
]
|
||||||
|
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
|
|||||||
Reference in New Issue
Block a user