Files
mirror/app/routes/profiles.py
T
2026-06-03 17:33:19 +03:30

118 lines
4.1 KiB
Python

from flask import Blueprint, flash, redirect, render_template, request, url_for
from flask_login import login_required
from app.extensions import db
from app.guides import GUIDES
from app.models import Mirror, Profile, ProfileItem
profiles_bp = Blueprint("profiles", __name__, url_prefix="/profiles")
@profiles_bp.route("/")
@login_required
def list_profiles():
profiles = Profile.query.order_by(Profile.name).all()
return render_template("profiles/list.html", profiles=profiles, guide=GUIDES.get("profiles", {}))
@profiles_bp.route("/create", methods=["GET", "POST"])
@login_required
def create():
mirrors = Mirror.query.filter_by(enabled=True).order_by(Mirror.category, Mirror.priority).all()
mirrors_by_cat: dict[str, list] = {}
for m in mirrors:
mirrors_by_cat.setdefault(m.category, []).append(m)
if request.method == "POST":
profile = Profile(
name=request.form["name"].strip(),
description=request.form.get("description", "").strip() or None,
is_default=request.form.get("is_default") == "on",
)
if profile.is_default:
Profile.query.update({Profile.is_default: False})
db.session.add(profile)
db.session.flush()
for category in Mirror.CATEGORIES:
mirror_id = request.form.get(f"mirror_{category}")
if mirror_id:
db.session.add(
ProfileItem(
profile_id=profile.id,
mirror_id=int(mirror_id),
category=category,
order=0,
)
)
db.session.commit()
flash("پروفایل ایجاد شد.", "success")
return redirect(url_for("profiles.list_profiles"))
return render_template(
"profiles/form.html",
profile=None,
mirrors_by_cat=mirrors_by_cat,
categories=Mirror.CATEGORIES,
category_labels=Mirror.CATEGORY_LABELS,
current={},
guide=GUIDES.get("profiles", {}),
)
@profiles_bp.route("/<int:profile_id>/edit", methods=["GET", "POST"])
@login_required
def edit(profile_id):
profile = Profile.query.get_or_404(profile_id)
mirrors = Mirror.query.filter_by(enabled=True).order_by(Mirror.category, Mirror.priority).all()
mirrors_by_cat: dict[str, list] = {}
for m in mirrors:
mirrors_by_cat.setdefault(m.category, []).append(m)
current = {item.category: item.mirror_id for item in profile.items}
if request.method == "POST":
profile.name = request.form["name"].strip()
profile.description = request.form.get("description", "").strip() or None
if request.form.get("is_default") == "on":
Profile.query.filter(Profile.id != profile.id).update({Profile.is_default: False})
profile.is_default = True
else:
profile.is_default = False
ProfileItem.query.filter_by(profile_id=profile.id).delete()
for category in Mirror.CATEGORIES:
mirror_id = request.form.get(f"mirror_{category}")
if mirror_id:
db.session.add(
ProfileItem(
profile_id=profile.id,
mirror_id=int(mirror_id),
category=category,
order=0,
)
)
db.session.commit()
flash("پروفایل به‌روزرسانی شد.", "success")
return redirect(url_for("profiles.list_profiles"))
return render_template(
"profiles/form.html",
profile=profile,
mirrors_by_cat=mirrors_by_cat,
categories=Mirror.CATEGORIES,
category_labels=Mirror.CATEGORY_LABELS,
current=current,
guide=GUIDES.get("profiles", {}),
)
@profiles_bp.route("/<int:profile_id>/delete", methods=["POST"])
@login_required
def delete(profile_id):
profile = Profile.query.get_or_404(profile_id)
db.session.delete(profile)
db.session.commit()
flash("پروفایل حذف شد.", "warning")
return redirect(url_for("profiles.list_profiles"))