Skip to content

Commit

Permalink
chore: extract delete custom domain from controller
Browse files Browse the repository at this point in the history
  • Loading branch information
cquintana92 committed Sep 20, 2024
1 parent d0ba767 commit 570f1a9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
16 changes: 15 additions & 1 deletion app/custom_domain_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import arrow
import re

from dataclasses import dataclass
from enum import Enum
from typing import Optional

from app.config import JOB_DELETE_DOMAIN
from app.db import Session
from app.email_utils import get_email_domain_part
from app.log import LOG
from app.models import User, CustomDomain, SLDomain, Mailbox
from app.models import User, CustomDomain, SLDomain, Mailbox, Job

_ALLOWED_DOMAIN_REGEX = re.compile(r"^(?!-)[A-Za-z0-9-]{1,63}(?<!-)$")

Expand Down Expand Up @@ -126,3 +128,15 @@ def create_custom_domain(
success=True,
instance=new_custom_domain,
)


def delete_custom_domain(domain: CustomDomain):
# Schedule delete domain job
LOG.w("schedule delete domain job for %s", domain)
domain.pending_deletion = True
Job.create(
name=JOB_DELETE_DOMAIN,
payload={"custom_domain_id": domain.id},
run_at=arrow.now(),
commit=True,
)
19 changes: 4 additions & 15 deletions app/dashboard/views/domain_detail.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import re

import arrow
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, IntegerField

from app.constants import DMARC_RECORD
from app.config import EMAIL_SERVERS_WITH_PRIORITY, EMAIL_DOMAIN, JOB_DELETE_DOMAIN
from app.config import EMAIL_SERVERS_WITH_PRIORITY, EMAIL_DOMAIN
from app.custom_domain_utils import delete_custom_domain
from app.custom_domain_validation import CustomDomainValidation
from app.dashboard.base import dashboard_bp
from app.db import Session
from app.log import LOG
from app.models import (
CustomDomain,
Alias,
Expand All @@ -20,7 +19,6 @@
DomainMailbox,
AutoCreateRule,
AutoCreateRuleMailbox,
Job,
)
from app.regex_utils import regex_match
from app.utils import random_string, CSRFValidationForm
Expand Down Expand Up @@ -263,17 +261,8 @@ def domain_detail(custom_domain_id):

elif request.form.get("form-name") == "delete":
name = custom_domain.domain
LOG.d("Schedule deleting %s", custom_domain)

# Schedule delete domain job
LOG.w("schedule delete domain job for %s", custom_domain)
custom_domain.pending_deletion = True
Job.create(
name=JOB_DELETE_DOMAIN,
payload={"custom_domain_id": custom_domain.id},
run_at=arrow.now(),
commit=True,
)

delete_custom_domain(custom_domain)

flash(
f"{name} scheduled for deletion."
Expand Down

0 comments on commit 570f1a9

Please sign in to comment.