Skip to content

Commit

Permalink
add admin action
Browse files Browse the repository at this point in the history
  • Loading branch information
sdemagny committed Dec 27, 2024
1 parent 8a06244 commit f8d5332
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/backend/mailbox_manager/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@
from mailbox_manager.utils.dimail import DimailAPIClient


@admin.action(description=_("Check domain health"))
def check_domain_health(modeladmin, request, queryset): # pylint: disable=unused-argument
"""Admin action to check domain health with dimail and update domain status."""
client = DimailAPIClient()
domains_updated = []
for domain in queryset:
old_status = domain.status
try:
response = client.check_domain(domain)
except exceptions.HTTPError as err:
messages.error(
request,
_(f"Check domain health failed for {domain.name} with message: [{err}]"),
)
else:
new_status = domain.status
if old_status != new_status:
domains_updated.append(domain)
messages.success(
request,
f"Domain updated {','.join(domains_updated)} {response.status_code} {response.json()}"
)


@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."""
Expand Down Expand Up @@ -54,7 +78,7 @@ class MailDomainAdmin(admin.ModelAdmin):
search_fields = ("name",)
readonly_fields = ["created_at", "slug"]
inlines = (UserMailDomainAccessInline,)
actions = (sync_mailboxes_from_dimail,)
actions = (sync_mailboxes_from_dimail, check_domain_health)


@admin.register(models.Mailbox)
Expand Down

0 comments on commit f8d5332

Please sign in to comment.