2019-12-02 01:13:39 +01:00
|
|
|
from flask import render_template, request, redirect, url_for, flash
|
|
|
|
from flask_login import login_required, current_user
|
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from wtforms import StringField, validators
|
|
|
|
|
2023-01-11 22:08:52 +01:00
|
|
|
from app import parallel_limiter
|
2020-10-15 16:45:28 +02:00
|
|
|
from app.config import EMAIL_SERVERS_WITH_PRIORITY
|
2024-09-17 10:30:55 +02:00
|
|
|
from app.custom_domain_utils import create_custom_domain
|
2019-12-02 01:13:39 +01:00
|
|
|
from app.dashboard.base import dashboard_bp
|
2024-09-17 10:30:55 +02:00
|
|
|
from app.models import CustomDomain
|
2019-12-02 01:13:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
class NewCustomDomainForm(FlaskForm):
|
2020-05-05 12:46:11 +02:00
|
|
|
domain = StringField(
|
|
|
|
"domain", validators=[validators.DataRequired(), validators.Length(max=128)]
|
|
|
|
)
|
2019-12-02 01:13:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/custom_domain", methods=["GET", "POST"])
|
|
|
|
@login_required
|
2023-01-11 22:08:52 +01:00
|
|
|
@parallel_limiter.lock(only_when=lambda: request.method == "POST")
|
2019-12-02 01:13:39 +01:00
|
|
|
def custom_domain():
|
2021-11-17 11:52:33 +01:00
|
|
|
custom_domains = CustomDomain.filter_by(
|
|
|
|
user_id=current_user.id, is_sl_subdomain=False
|
|
|
|
).all()
|
2019-12-02 01:13:39 +01:00
|
|
|
new_custom_domain_form = NewCustomDomainForm()
|
|
|
|
|
|
|
|
if request.method == "POST":
|
2019-12-27 23:50:09 +01:00
|
|
|
if request.form.get("form-name") == "create":
|
2020-01-16 22:21:19 +01:00
|
|
|
if not current_user.is_premium():
|
|
|
|
flash("Only premium plan can add custom domain", "warning")
|
|
|
|
return redirect(url_for("dashboard.custom_domain"))
|
|
|
|
|
2019-12-02 01:13:39 +01:00
|
|
|
if new_custom_domain_form.validate():
|
2024-09-17 10:30:55 +02:00
|
|
|
res = create_custom_domain(
|
|
|
|
user=current_user, domain=new_custom_domain_form.domain.data
|
|
|
|
)
|
|
|
|
if res.success:
|
|
|
|
flash(f"New domain {res.instance.domain} is created", "success")
|
2020-01-01 23:51:40 +01:00
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"dashboard.domain_detail_dns",
|
2024-09-17 10:30:55 +02:00
|
|
|
custom_domain_id=res.instance.id,
|
2020-01-01 23:51:40 +01:00
|
|
|
)
|
2019-12-02 01:13:39 +01:00
|
|
|
)
|
2024-09-17 10:30:55 +02:00
|
|
|
else:
|
|
|
|
flash(res.message, res.message_category)
|
|
|
|
if res.redirect:
|
|
|
|
return redirect(url_for(res.redirect))
|
2019-12-02 01:13:39 +01:00
|
|
|
|
|
|
|
return render_template(
|
|
|
|
"dashboard/custom_domain.html",
|
|
|
|
custom_domains=custom_domains,
|
|
|
|
new_custom_domain_form=new_custom_domain_form,
|
|
|
|
EMAIL_SERVERS_WITH_PRIORITY=EMAIL_SERVERS_WITH_PRIORITY,
|
|
|
|
)
|