bump project
This commit is contained in:
1
app/admin/__init__.py
Normal file
1
app/admin/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
45
app/admin/forms.py
Normal file
45
app/admin/forms.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import EmailField, PasswordField, SelectField, StringField, SubmitField
|
||||
from wtforms.validators import DataRequired, Email, EqualTo, Length, ValidationError
|
||||
|
||||
from app.models import Account, Role
|
||||
|
||||
|
||||
class AccountCreateForm(FlaskForm):
|
||||
full_name = StringField("Full name", validators=[DataRequired(), Length(max=160)])
|
||||
username = StringField("Username", validators=[DataRequired(), Length(min=3, max=80)])
|
||||
email = EmailField("Email", validators=[DataRequired(), Email(), Length(max=255)])
|
||||
role = SelectField(
|
||||
"Role",
|
||||
choices=[(Role.OPERATOR.value, "Operator"), (Role.VIEWER.value, "Viewer"), (Role.ADMIN.value, "Admin")],
|
||||
validators=[DataRequired()],
|
||||
)
|
||||
password = PasswordField("Password", validators=[DataRequired(), Length(min=12, max=128)])
|
||||
confirm_password = PasswordField(
|
||||
"Confirm password",
|
||||
validators=[DataRequired(), EqualTo("password", message="Passwords must match.")],
|
||||
)
|
||||
submit = SubmitField("Create account")
|
||||
|
||||
def validate_username(self, field) -> None:
|
||||
username = field.data.strip().lower()
|
||||
if not re.fullmatch(r"[a-z0-9_.-]+", username):
|
||||
raise ValidationError("Use only letters, numbers, dots, hyphens, and underscores.")
|
||||
if Account.query.filter_by(username=username).first():
|
||||
raise ValidationError("That username is already in use.")
|
||||
field.data = username
|
||||
|
||||
def validate_email(self, field) -> None:
|
||||
email = field.data.strip().lower()
|
||||
if Account.query.filter_by(email=email).first():
|
||||
raise ValidationError("That email is already in use.")
|
||||
field.data = email
|
||||
|
||||
def validate_password(self, field) -> None:
|
||||
password = field.data
|
||||
if not re.search(r"[A-Z]", password) or not re.search(r"[a-z]", password) or not re.search(r"\d", password):
|
||||
raise ValidationError("Use at least one uppercase letter, one lowercase letter, and one number.")
|
||||
52
app/admin/routes.py
Normal file
52
app/admin/routes.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from flask import Blueprint, abort, flash, redirect, render_template, url_for
|
||||
from flask_login import current_user
|
||||
|
||||
from app.admin.forms import AccountCreateForm
|
||||
from app.extensions import db
|
||||
from app.models import Account, Role
|
||||
from app.services.security import roles_required
|
||||
|
||||
bp = Blueprint("admin", __name__, url_prefix="/admin")
|
||||
|
||||
|
||||
@bp.get("/accounts")
|
||||
@roles_required(Role.ADMIN)
|
||||
def accounts():
|
||||
items = Account.query.order_by(Account.created_at.desc()).all()
|
||||
return render_template("admin/accounts.html", accounts=items)
|
||||
|
||||
|
||||
@bp.route("/accounts/new", methods=["GET", "POST"])
|
||||
@roles_required(Role.ADMIN)
|
||||
def new_account():
|
||||
form = AccountCreateForm()
|
||||
if form.validate_on_submit():
|
||||
account = Account(
|
||||
full_name=form.full_name.data.strip(),
|
||||
username=form.username.data.strip().lower(),
|
||||
email=form.email.data.strip().lower(),
|
||||
role=form.role.data,
|
||||
)
|
||||
account.set_password(form.password.data)
|
||||
db.session.add(account)
|
||||
db.session.commit()
|
||||
flash(f"Account created for {account.full_name}.", "success")
|
||||
return redirect(url_for("admin.accounts"))
|
||||
return render_template("admin/account_form.html", form=form)
|
||||
|
||||
|
||||
@bp.post("/accounts/<int:account_id>/toggle")
|
||||
@roles_required(Role.ADMIN)
|
||||
def toggle_account(account_id: int):
|
||||
account = db.session.get(Account, account_id)
|
||||
if not account:
|
||||
abort(404)
|
||||
if account.id == current_user.id:
|
||||
flash("You cannot deactivate your own account.", "warning")
|
||||
return redirect(url_for("admin.accounts"))
|
||||
account.active = not account.active
|
||||
db.session.commit()
|
||||
flash(f"{account.full_name} is now {'active' if account.active else 'inactive'}.", "success")
|
||||
return redirect(url_for("admin.accounts"))
|
||||
Reference in New Issue
Block a user