2024-04-18 10:26:13 +02:00
|
|
|
"""Admin classes and registrations for People's mailbox manager app."""
|
|
|
|
|
|
2024-10-09 17:18:38 +02:00
|
|
|
from django.contrib import admin, messages
|
2024-12-27 18:12:20 +01:00
|
|
|
from django.utils.html import format_html
|
2024-04-18 10:26:13 +02:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
2024-10-09 17:18:38 +02:00
|
|
|
from requests import exceptions
|
|
|
|
|
|
2024-12-27 18:12:20 +01:00
|
|
|
from mailbox_manager import enums, models
|
2024-10-09 17:18:38 +02:00
|
|
|
from mailbox_manager.utils.dimail import DimailAPIClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin.action(description=_("Synchronise from dimail"))
|
|
|
|
|
def sync_mailboxes_from_dimail(modeladmin, request, queryset): # pylint: disable=unused-argument
|
|
|
|
|
"""Admin action to synchronize existing mailboxes from dimail to our database."""
|
|
|
|
|
client = DimailAPIClient()
|
|
|
|
|
|
|
|
|
|
for domain in queryset:
|
|
|
|
|
try:
|
2024-11-20 17:56:27 +01:00
|
|
|
imported_mailboxes = client.import_mailboxes(domain)
|
2024-10-09 17:18:38 +02:00
|
|
|
except exceptions.HTTPError as err:
|
|
|
|
|
messages.error(
|
|
|
|
|
request,
|
|
|
|
|
_(f"Synchronisation failed for {domain.name} with message: [{err}]"),
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
messages.success(
|
|
|
|
|
request,
|
|
|
|
|
_(
|
|
|
|
|
f"Synchronisation succeed for {domain.name}. "
|
|
|
|
|
f"Imported mailboxes: {', '.join(imported_mailboxes)}"
|
|
|
|
|
),
|
|
|
|
|
)
|
2024-04-18 10:26:13 +02:00
|
|
|
|
|
|
|
|
|
2024-12-27 18:12:20 +01:00
|
|
|
@admin.action(description=_("Check and update status from dimail"))
|
|
|
|
|
def fetch_domain_status_from_dimail(modeladmin, request, queryset): # pylint: disable=unused-argument
|
|
|
|
|
"""Admin action to check domain health with dimail and update domain status."""
|
|
|
|
|
client = DimailAPIClient()
|
|
|
|
|
domains_updated, excluded_domains, msg_error = [], [], []
|
|
|
|
|
success = False
|
|
|
|
|
for domain in queryset:
|
|
|
|
|
# do not check disabled domains
|
|
|
|
|
if domain.status == enums.MailDomainStatusChoices.DISABLED:
|
|
|
|
|
excluded_domains.append(domain.name)
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
old_status = domain.status
|
|
|
|
|
try:
|
|
|
|
|
response = client.fetch_domain_status(domain)
|
|
|
|
|
except exceptions.HTTPError as err:
|
|
|
|
|
msg_error.append(_(f"""- <b>{domain.name}</b> with message: '{err}'"""))
|
|
|
|
|
else:
|
|
|
|
|
success = True
|
|
|
|
|
# temporary (or not?) display content of the dimail response to debug broken state
|
|
|
|
|
if domain.status == enums.MailDomainStatusChoices.FAILED:
|
|
|
|
|
messages.info(request, response.json())
|
|
|
|
|
if old_status != domain.status:
|
|
|
|
|
domains_updated.append(domain.name)
|
|
|
|
|
|
|
|
|
|
if success:
|
|
|
|
|
msg_success = [
|
|
|
|
|
_("Check domains done with success."),
|
|
|
|
|
_(f"Domains updated: {', '.join(domains_updated)}")
|
|
|
|
|
if domains_updated
|
|
|
|
|
else _("No domain updated."),
|
|
|
|
|
]
|
|
|
|
|
messages.success(request, format_html("<br> ".join(map(str, msg_success))))
|
|
|
|
|
if msg_error:
|
|
|
|
|
msg_error.insert(0, _("Check domain failed for:"))
|
|
|
|
|
messages.error(request, format_html("<br> ".join(map(str, msg_error))))
|
|
|
|
|
if excluded_domains:
|
|
|
|
|
messages.warning(
|
|
|
|
|
request,
|
|
|
|
|
_(
|
|
|
|
|
f"Domains disabled are excluded from check: {', '.join(excluded_domains)}"
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2024-10-10 09:59:54 +02:00
|
|
|
class UserMailDomainAccessInline(admin.TabularInline):
|
|
|
|
|
"""Inline admin class for mail domain accesses."""
|
|
|
|
|
|
|
|
|
|
extra = 0
|
|
|
|
|
model = models.MailDomainAccess
|
2024-12-12 16:14:28 +01:00
|
|
|
readonly_fields = ("created_at", "updated_at", "domain")
|
2024-10-10 09:59:54 +02:00
|
|
|
|
|
|
|
|
|
2024-04-18 10:26:13 +02:00
|
|
|
@admin.register(models.MailDomain)
|
|
|
|
|
class MailDomainAdmin(admin.ModelAdmin):
|
|
|
|
|
"""Mail domain admin interface declaration."""
|
|
|
|
|
|
|
|
|
|
list_display = (
|
|
|
|
|
"name",
|
|
|
|
|
"created_at",
|
|
|
|
|
"updated_at",
|
2024-06-19 16:48:07 +02:00
|
|
|
"slug",
|
2024-07-07 01:16:33 +02:00
|
|
|
"status",
|
2024-04-18 10:26:13 +02:00
|
|
|
)
|
|
|
|
|
search_fields = ("name",)
|
2024-06-19 16:48:07 +02:00
|
|
|
readonly_fields = ["created_at", "slug"]
|
2025-01-09 14:09:05 +01:00
|
|
|
list_filter = ("status",)
|
2024-10-10 09:59:54 +02:00
|
|
|
inlines = (UserMailDomainAccessInline,)
|
2024-12-27 18:12:20 +01:00
|
|
|
actions = (sync_mailboxes_from_dimail, fetch_domain_status_from_dimail)
|
2024-10-09 17:18:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(models.Mailbox)
|
|
|
|
|
class MailboxAdmin(admin.ModelAdmin):
|
|
|
|
|
"""Admin for mailbox model."""
|
|
|
|
|
|
2025-01-06 17:51:21 +01:00
|
|
|
list_display = ("__str__", "domain", "status")
|
|
|
|
|
list_filter = ("status",)
|
|
|
|
|
search_fields = ("local_part", "domain__name")
|
2024-04-18 10:26:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(models.MailDomainAccess)
|
|
|
|
|
class MailDomainAccessAdmin(admin.ModelAdmin):
|
|
|
|
|
"""Admin for mail domain accesses model."""
|
|
|
|
|
|
2024-08-02 16:46:46 +02:00
|
|
|
list_display = (
|
|
|
|
|
"user",
|
|
|
|
|
"domain",
|
|
|
|
|
"role",
|
|
|
|
|
"created_at",
|
|
|
|
|
"updated_at",
|
|
|
|
|
)
|
2024-04-18 10:26:13 +02:00
|
|
|
|
|
|
|
|
|
2024-08-27 10:00:28 +02:00
|
|
|
class MailDomainAccessInline(admin.TabularInline):
|
|
|
|
|
"""Inline admin class for mail domain accesses."""
|
|
|
|
|
|
|
|
|
|
extra = 0
|
|
|
|
|
autocomplete_fields = ["user", "domain"]
|
|
|
|
|
model = models.MailDomainAccess
|
|
|
|
|
readonly_fields = ("created_at", "updated_at")
|