diff --git a/application/auth/forms.py b/application/auth/forms.py index 90c31fb..4db03b1 100644 --- a/application/auth/forms.py +++ b/application/auth/forms.py @@ -3,16 +3,21 @@ from wtforms import StringField, SubmitField, PasswordField, BooleanField from wtforms.validators import DataRequired +# Default Form that inherits from FlaskForm and +# contains a username, password and submit button class defaultForm(FlaskForm): username = StringField("Username", validators=[DataRequired()]) password = PasswordField("Password", validators=[DataRequired()]) submit = SubmitField("Submit") +# LoginForm, contains exactly the same as defaultForm class LoginForm(defaultForm): pass +# RegisterForm that inherits from the default. +# Adds a password confirmation and if the user is an admin or not. class RegisterForm(defaultForm): confirm_password = PasswordField( "Confirm Password", validators=[DataRequired()] @@ -20,6 +25,8 @@ class RegisterForm(defaultForm): is_admin = BooleanField("Admin") +# Form to update password information. +# Needs a confirmation password and the current password class UpdateForm(defaultForm): confirm_password = PasswordField( "Confirm Password", validators=[DataRequired()] diff --git a/application/auth/models.py b/application/auth/models.py index 554a4fc..086f42c 100644 --- a/application/auth/models.py +++ b/application/auth/models.py @@ -2,14 +2,18 @@ from application import db from flask_login import UserMixin # type: ignore +# User model class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(150), unique=True, nullable=False) password = db.Column(db.String(150), nullable=False) is_admin = db.Column(db.Boolean, default=False) + # Purely a relationship not a column, + # makes all the services accessible through User.services services = db.relationship("Service", backref="user", lazy="joined") + # Initialize user, prevents red stuff def __init__(self, username: str, password: str, is_admin: bool = False): self.username = username self.password = password diff --git a/application/auth/views.py b/application/auth/views.py index 7652eea..54cd670 100644 --- a/application/auth/views.py +++ b/application/auth/views.py @@ -16,8 +16,8 @@ from application.auth.forms import RegisterForm, UpdateForm auth_blueprint = Blueprint("auth", __name__, template_folder="templates") -# Routes -@auth_blueprint.route("/register", methods=["GET", "POST"]) +# Add user +@auth_blueprint.route("/register_user", methods=["GET", "POST"]) @admin_required def register(): register_form = RegisterForm() @@ -59,6 +59,7 @@ def register(): ) +# Update user (specifically password) @auth_blueprint.route("/update_user", methods=["GET", "POST"]) @login_required def update(): @@ -89,6 +90,7 @@ def update(): return render_template("update_user.html", form=form, active_page="update") +# Login as user or admin @auth_blueprint.route("/login", methods=["GET", "POST"]) def login(): login_form = LoginForm() @@ -110,6 +112,7 @@ def login(): return render_template("login.html", form=login_form, feedback=feedback) +# Logout @auth_blueprint.route("/logout") @login_required def logout(): diff --git a/application/dash/forms.py b/application/dash/forms.py index 69bfe06..d55eda3 100644 --- a/application/dash/forms.py +++ b/application/dash/forms.py @@ -4,9 +4,11 @@ from wtforms.validators import DataRequired from flask_wtf.file import FileField, FileAllowed # type: ignore +# Form for service on dashboard, connected to database through ORM class ServiceForm(FlaskForm): name = StringField("Service name:", validators=[DataRequired()]) url = URLField("Service URL:", validators=[DataRequired()]) + # File field that only allows jpg, jpeg or png image = FileField( "Icon:", validators=[ diff --git a/application/dash/models.py b/application/dash/models.py index 588a913..a152b40 100644 --- a/application/dash/models.py +++ b/application/dash/models.py @@ -1,14 +1,17 @@ from application import db +# Service class for dashboard class Service(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) url = db.Column(db.String, nullable=False) icon = db.Column(db.String, default="google.png") + # Foreign key to connect to User table user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False) + # Initialize the service (prevents ugly red lines) def __init__( self, name: str, url: str, user_id: int, icon: str = "google.png" ):