From a0808896b4ebf8fa6b05d2816b40c0401b81fe8b Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Wed, 6 Oct 2021 11:07:22 +0200 Subject: [PATCH] Allow deleting a mailbox (mark as inactive) --- musician/api.py | 17 ++++++++++++++ .../musician/mailbox_check_delete.html | 12 ++++++++++ musician/templates/musician/mailboxes.html | 10 +++++++-- musician/urls.py | 1 + musician/views.py | 22 ++++++++++++++++++- 5 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 musician/templates/musician/mailbox_check_delete.html diff --git a/musician/api.py b/musician/api.py index 1365b91..a106040 100644 --- a/musician/api.py +++ b/musician/api.py @@ -24,6 +24,7 @@ API_PATHS = { 'address-list': 'addresses/', 'address-detail': 'addresses/{pk}/', 'mailbox-list': 'mailboxes/', + 'mailbox-detail': 'mailboxes/{pk}/', 'mailinglist-list': 'lists/', 'saas-list': 'saas/', 'website-list': 'websites/', @@ -168,10 +169,26 @@ class Orchestra(object): resource = '{}-list'.format(Mailbox.api_name) return self.request("POST", resource=resource, data=data, raise_exception=False) + def retrieve_mailbox(self, pk): + path = API_PATHS.get('mailbox-detail').format_map({'pk': pk}) + + url = urllib.parse.urljoin(self.base_url, path) + status, data_json = self.request("GET", url=url, raise_exception=False) + if status == 404: + raise Http404(_("No mailbox found matching the query")) + return Mailbox.new_from_json(data_json) + def retrieve_mailbox_list(self): mailboxes = self.retrieve_service_list(Mailbox.api_name) return [Mailbox.new_from_json(mailbox_data) for mailbox_data in mailboxes] + def delete_mailbox(self, pk): + path = API_PATHS.get('mailbox-detail').format_map({'pk': pk}) + url = urllib.parse.urljoin(self.base_url, path) + # Mark as inactive instead of deleting + # return self.request("DELETE", url=url, render_as=None) + return self.request("PATCH", url=url, data={"is_active": False}) + def retrieve_domain(self, pk): path = API_PATHS.get('domain-detail').format_map({'pk': pk}) diff --git a/musician/templates/musician/mailbox_check_delete.html b/musician/templates/musician/mailbox_check_delete.html new file mode 100644 index 0000000..e0fa701 --- /dev/null +++ b/musician/templates/musician/mailbox_check_delete.html @@ -0,0 +1,12 @@ +{% extends "musician/base.html" %} +{% load i18n %} + +{% block content %} +
+ {% csrf_token %} +

{% blocktrans with name=object.name %}Are you sure that you want remove the mailbox: "{{ name }}"?{% endblocktrans %}

+

{% trans 'NOTE: This action cannot be undone.' %}

+ + {% trans 'Cancel' %} +
+{% endblock %} diff --git a/musician/templates/musician/mailboxes.html b/musician/templates/musician/mailboxes.html index 0b1f8b0..8260b75 100644 --- a/musician/templates/musician/mailboxes.html +++ b/musician/templates/musician/mailboxes.html @@ -15,11 +15,13 @@ {% trans "Name" %} {% trans "Filtering" %} {% trans "Addresses" %} - {% trans "Active" %} + {% for mailbox in object_list %} + {# #} + {% if mailbox.is_active %} {{ mailbox.name }} {{ mailbox.filtering }} @@ -30,8 +32,12 @@
{% endfor %} - + + + + + {% endif %}{# #} {% endfor %} {% include "musician/components/table_paginator.html" %} diff --git a/musician/urls.py b/musician/urls.py index a3740ab..0902ae6 100644 --- a/musician/urls.py +++ b/musician/urls.py @@ -25,6 +25,7 @@ urlpatterns = [ path('address//delete/', views.AddressDeleteView.as_view(), name='address-delete'), path('mailboxes/', views.MailboxesView.as_view(), name='mailbox-list'), path('mailboxes/new/', views.MailboxCreateView.as_view(), name='mailbox-create'), + path('mailboxes//delete/', views.MailboxDeleteView.as_view(), name='mailbox-delete'), path('mailing-lists/', views.MailingListsView.as_view(), name='mailing-lists'), path('databases/', views.DatabasesView.as_view(), name='database-list'), path('saas/', views.SaasView.as_view(), name='saas-list'), diff --git a/musician/views.py b/musician/views.py index a6e2542..8683261 100644 --- a/musician/views.py +++ b/musician/views.py @@ -277,7 +277,8 @@ class AddressDeleteView(CustomContextMixin, UserTokenRequiredMixin, DeleteView): try: self.orchestra.delete_mail_address(self.object.id) except HTTPError as e: - print(e) + # TODO(@slamora): show error message to user + logger.error(e) return HttpResponseRedirect(self.success_url) @@ -355,6 +356,25 @@ class MailboxCreateView(CustomContextMixin, UserTokenRequiredMixin, FormView): return super().form_valid(form) +class MailboxDeleteView(CustomContextMixin, UserTokenRequiredMixin, DeleteView): + template_name = "musician/mailbox_check_delete.html" + success_url = reverse_lazy("musician:mailbox-list") + + def get_object(self, queryset=None): + obj = self.orchestra.retrieve_mailbox(self.kwargs['pk']) + return obj + + def delete(self, request, *args, **kwargs): + self.object = self.get_object() + try: + self.orchestra.delete_mailbox(self.object.id) + except HTTPError as e: + # TODO(@slamora): show error message to user + logger.error(e) + + return HttpResponseRedirect(self.success_url) + + class DatabasesView(ServiceListView): template_name = "musician/databases.html" service_class = DatabaseService