Skip to content

Commit

Permalink
refactor: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
s-aga-r committed Oct 30, 2024
1 parent 91815c1 commit c1b16da
Show file tree
Hide file tree
Showing 20 changed files with 139 additions and 118 deletions.
6 changes: 1 addition & 5 deletions mail_server/api/auth.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import frappe
from frappe import _

from mail_server.utils.user import has_role, is_system_manager


@frappe.whitelist(methods=["POST"])
def validate() -> None:
"""Validate the user is a domain owner or system manager."""

user = frappe.session.user
if not has_role(user, "Domain Owner") and not is_system_manager(user):
frappe.throw(_("Not permitted"), frappe.PermissionError)
frappe.only_for(["Domain Owner", "System Manager", "Administrator"])
2 changes: 0 additions & 2 deletions mail_server/api/inbound.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
)


from email.utils import formataddr

from mail_server.utils import convert_to_utc


Expand Down
2 changes: 2 additions & 0 deletions mail_server/api/outbound.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

@frappe.whitelist(methods=["POST"])
def send() -> str:
"""Sends the outgoing mail."""

data = json.loads(frappe.request.data.decode())
log = create_outgoing_mail_log(data["outgoing_mail"], data["recipients"], data["message"])
return log.name
Expand Down
16 changes: 3 additions & 13 deletions mail_server/mail_server/doctype/dns_record/dns_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from frappe.utils import cint, now

from mail_server.mail_server.doctype.dns_record.dns_provider import DNSProvider
from mail_server.utils import enqueue_job
from mail_server.utils import enqueue_job, verify_dns_record
from mail_server.utils.cache import get_root_domain_name


class DNSRecord(Document):
Expand Down Expand Up @@ -92,16 +93,12 @@ def delete_record_from_dns_provider(self) -> None:
def get_fqdn(self) -> str:
"""Returns the Fully Qualified Domain Name"""

from mail_server.utils.cache import get_root_domain_name

return f"{self.host}.{get_root_domain_name()}"

@frappe.whitelist()
def verify_dns_record(self, save: bool = False) -> None:
"""Verifies the DNS Record"""

from mail_server.utils import verify_dns_record

self.is_verified = 0
self.last_checked_at = now()
if verify_dns_record(self.get_fqdn(), self.type, self.value):
Expand Down Expand Up @@ -163,7 +160,7 @@ def create_or_update_dns_record(
dns_record.ttl = ttl
dns_record.priority = priority
dns_record.category = category
dns_record.insert(ignore_permissions=True)
dns_record.save(ignore_permissions=True)

return dns_record

Expand All @@ -177,12 +174,5 @@ def verify_all_dns_records() -> None:
dns_record.verify_dns_record(save=True)


@frappe.whitelist()
def enqueue_verify_all_dns_records() -> None:
"Called by the scheduler to enqueue the `verify_all_dns_records` job."

enqueue_job(verify_all_dns_records, queue="long")


def after_doctype_insert() -> None:
frappe.db.add_unique("DNS Record", ["host", "type"])
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ frappe.listview_settings["DNS Record"] = {

function verify_all_dns_records(listview) {
frappe.call({
method: "mail_server.mail_server.doctype.dns_record.dns_record.enqueue_verify_all_dns_records",
method: "mail_server.tasks.enqueue_verify_all_dns_records",
freeze: true,
freeze_message: __("Creating Job..."),
callback: () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
from frappe.utils import cint, now, time_diff_in_seconds, validate_email_address
from uuid_utils import uuid7

from mail_server.mail_server.doctype.spam_check_log.spam_check_log import create_spam_check_log
from mail_server.rabbitmq import INCOMING_MAIL_QUEUE, rabbitmq_context
from mail_server.utils import parse_iso_datetime
from mail_server.utils.email_parser import EmailParser, extract_ip_and_host
from mail_server.utils.validation import is_domain_registry_exists


class IncomingMailLog(Document):
def autoname(self) -> None:
Expand Down Expand Up @@ -43,11 +49,6 @@ def enqueue_process_message(self) -> None:
def process_message(self) -> None:
"""Process the email message and update the log."""

from mail_server.mail_server.doctype.spam_check_log.spam_check_log import create_spam_check_log
from mail_server.utils import parse_iso_datetime
from mail_server.utils.email_parser import EmailParser, extract_ip_and_host
from mail_server.utils.validation import is_domain_registry_exists

parser = EmailParser(self.message)
self.display_name, self.sender = parser.get_sender()
self.receiver = parser.get_header("Delivered-To")
Expand Down Expand Up @@ -122,8 +123,6 @@ def create_incoming_mail_log(agent: str, message: str) -> "IncomingMailLog":
def fetch_emails_from_queue() -> None:
"""Fetch emails from queue and create Incoming Mail Log."""

from mail_server.rabbitmq import INCOMING_MAIL_QUEUE, rabbitmq_context

max_failures = 3
total_failures = 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,14 @@
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-10-21 10:09:50.083574",
"links": [
{
"group": "Reference",
"link_doctype": "Mail Agent",
"link_fieldname": "agent_group"
}
],
"modified": "2024-10-30 16:39:27.695709",
"modified_by": "Administrator",
"module": "Mail Server",
"name": "Mail Agent Group",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ frappe.ui.form.on("Mail Domain Registry", {

set_queries(frm) {
frm.set_query("domain_owner", () => ({
query: "mail_server.mail_server.doctype.mail_domain_registry.mail_domain_registry.get_users_with_domain_owner_role",
query: "mail_server.utils.query.get_users_with_domain_owner_role",
filters: {
enabled: 1,
role: "Domain Owner",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,14 @@
"group": "Reference",
"link_doctype": "Outgoing Mail Log",
"link_fieldname": "domain_name"
},
{
"group": "Reference",
"link_doctype": "Incoming Mail Log",
"link_fieldname": "domain_name"
}
],
"modified": "2024-10-22 14:16:18.250625",
"modified": "2024-10-30 18:19:40.233197",
"modified_by": "Administrator",
"module": "Mail Server",
"name": "Mail Domain Registry",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from mail_server.mail_server.doctype.mail_server_settings.mail_server_settings import (
validate_mail_server_settings,
)
from mail_server.utils import verify_dns_record
from mail_server.utils.cache import delete_cache
from mail_server.utils.user import has_role, is_system_manager

Expand Down Expand Up @@ -143,8 +144,6 @@ def get_dns_records(self) -> list[dict]:
def verify_dns_records(self) -> None:
"""Verifies DNS Records"""

from mail_server.utils import verify_dns_record

errors = []
for record in self.get_dns_records():
if not verify_dns_record(record["host"], record["type"], record["value"]):
Expand Down Expand Up @@ -183,34 +182,6 @@ def _db_set(
self.notify_update()


@frappe.whitelist()
@frappe.validate_and_sanitize_search_inputs
def get_users_with_domain_owner_role(
doctype: str | None = None,
txt: str | None = None,
searchfield: str | None = None,
start: int = 0,
page_len: int = 20,
filters: dict | None = None,
) -> list:
"""Returns a list of User(s) who have Domain Owner role."""

USER = frappe.qb.DocType("User")
HAS_ROLE = frappe.qb.DocType("Has Role")
return (
frappe.qb.from_(USER)
.left_join(HAS_ROLE)
.on(USER.name == HAS_ROLE.parent)
.select(USER.name)
.where(
(USER.enabled == 1)
& (USER.name.like(f"%{txt}%"))
& (HAS_ROLE.role == "Domain Owner")
& (HAS_ROLE.parenttype == "User")
)
).run(as_dict=False)


def get_permission_query_condition(user: str | None = None) -> str:
if not user:
user = frappe.session.user
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Copyright (c) 2024, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt

import socket

import frappe
from frappe import _
from frappe.model.document import Document
from frappe.utils import cint

from mail_server.rabbitmq import rabbitmq_context
from mail_server.utils.cache import delete_cache
from mail_server.utils.validation import is_valid_host

Expand Down Expand Up @@ -49,6 +52,8 @@ def validate_spf_host(self) -> None:
if not self.has_value_changed("spf_host"):
return

from mail_server.mail_server.doctype.mail_agent.mail_agent import create_or_update_spf_dns_record

self.spf_host = self.spf_host.lower()
if not is_valid_host(self.spf_host):
msg = _(
Expand All @@ -63,8 +68,6 @@ def validate_spf_host(self) -> None:
):
frappe.delete_doc("DNS Record", spf_dns_record, ignore_permissions=True)

from mail_server.mail_server.doctype.mail_agent.mail_agent import create_or_update_spf_dns_record

create_or_update_spf_dns_record(self.spf_host)

def validate_default_dkim_key_size(self) -> None:
Expand All @@ -83,10 +86,6 @@ def validate_rmq_host(self) -> None:
def test_rabbitmq_connection(self) -> None:
"""Tests the connection to the RabbitMQ server."""

import socket

from mail_server.rabbitmq import rabbitmq_context

try:
with rabbitmq_context():
frappe.msgprint(_("Connection Successful"), alert=True, indicator="green")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def get_mail_server_sync_history(source: str, user: str, domain_name: str) -> "M
return create_mail_server_sync_history(source, user, domain_name, commit=True)


def on_doctype_update():
def on_doctype_update() -> None:
frappe.db.add_unique(
"Mail Server Sync History",
["source", "user", "domain_name"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
import requests
from frappe import _
from frappe.model.document import Document
from frappe.query_builder.functions import GroupConcat
from frappe.utils import cint, now, time_diff_in_seconds
from pypika import Order
from uuid_utils import uuid7

from mail_server.mail_server.doctype.spam_check_log.spam_check_log import create_spam_check_log
from mail_server.rabbitmq import OUTGOING_MAIL_QUEUE, OUTGOING_MAIL_STATUS_QUEUE, rabbitmq_context
from mail_server.utils import convert_to_utc, parse_iso_datetime
from mail_server.utils.cache import get_user_owned_domains
from mail_server.utils.cache import get_root_domain_name, get_user_owned_domains
from mail_server.utils.email_parser import EmailParser
from mail_server.utils.user import is_system_manager


Expand Down Expand Up @@ -47,8 +51,6 @@ def set_ip_address(self) -> None:
def validate_message(self) -> None:
"""Validate message and extract domain name."""

from mail_server.utils.email_parser import EmailParser

parser = EmailParser(self.message)
parser.update_header("X-FM-OML", self.name)
self.priority = cint(parser.get_header("X-Priority"))
Expand Down Expand Up @@ -260,8 +262,6 @@ def push_to_queue(self) -> None:
if not (self.status == "Accepted" and self.failed_count < 3):
return

from mail_server.rabbitmq import OUTGOING_MAIL_QUEUE, rabbitmq_context

transfer_started_at = now()
transfer_started_after = time_diff_in_seconds(transfer_started_at, self.processed_at)
self._db_set(
Expand Down Expand Up @@ -329,12 +329,6 @@ def is_spam_detection_enabled_for_outbound() -> bool:
def push_emails_to_queue() -> None:
"""Pushes emails to the queue for sending."""

from frappe.query_builder.functions import GroupConcat
from pypika import Order

from mail_server.rabbitmq import OUTGOING_MAIL_QUEUE, rabbitmq_context
from mail_server.utils.cache import get_root_domain_name

batch_size = 1000
max_failures = 3
total_failures = 0
Expand Down Expand Up @@ -518,8 +512,6 @@ def delivered(data: dict) -> None:
except Exception:
frappe.log_error(title="Update Delivery Status - Delivered", message=frappe.get_traceback())

from mail_server.rabbitmq import OUTGOING_MAIL_STATUS_QUEUE, rabbitmq_context

max_failures = 3
total_failures = 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class SpamCheckLog(Document):
@staticmethod
def clear_old_logs(days=7):
def clear_old_logs(days=7) -> None:
log = frappe.qb.DocType("Spam Check Log")
frappe.db.delete(log, filters=(log.creation < (Now() - Interval(days=days))))

Expand Down
21 changes: 13 additions & 8 deletions mail_server/tasks.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import frappe

from mail_server.mail_server.doctype.dns_record.dns_record import verify_all_dns_records
from mail_server.mail_server.doctype.incoming_mail_log.incoming_mail_log import fetch_emails_from_queue
from mail_server.mail_server.doctype.outgoing_mail_log.outgoing_mail_log import (
fetch_and_update_delivery_statuses,
push_emails_to_queue,
)
from mail_server.utils import enqueue_job


def enqueue_push_emails_to_queue() -> None:
"Called by the scheduler to enqueue the `push_emails_to_queue` job."

from mail_server.mail_server.doctype.outgoing_mail_log.outgoing_mail_log import push_emails_to_queue

frappe.session.user = "Administrator"
enqueue_job(push_emails_to_queue, queue="long")

Expand All @@ -16,10 +20,6 @@ def enqueue_push_emails_to_queue() -> None:
def enqueue_fetch_and_update_delivery_statuses() -> None:
"Called by the scheduler to enqueue the `fetch_and_update_delivery_statuses` job."

from mail_server.mail_server.doctype.outgoing_mail_log.outgoing_mail_log import (
fetch_and_update_delivery_statuses,
)

frappe.session.user = "Administrator"
enqueue_job(fetch_and_update_delivery_statuses, queue="long")

Expand All @@ -28,7 +28,12 @@ def enqueue_fetch_and_update_delivery_statuses() -> None:
def enqueue_fetch_emails_from_queue() -> None:
"Called by the scheduler to enqueue the `fetch_emails_from_queue` job."

from mail_server.mail_server.doctype.incoming_mail_log.incoming_mail_log import fetch_emails_from_queue

frappe.session.user = "Administrator"
enqueue_job(fetch_emails_from_queue, queue="long")


@frappe.whitelist()
def enqueue_verify_all_dns_records() -> None:
"Called by the scheduler to enqueue the `verify_all_dns_records` job."

enqueue_job(verify_all_dns_records, queue="long")
Loading

0 comments on commit c1b16da

Please sign in to comment.