From b0366ff1d047bc441451537b4215be15175a8233 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Fri, 1 Oct 2021 13:36:52 +0200 Subject: [PATCH] Implement address delete --- musician/api.py | 5 ++++ musician/forms.py | 6 ++--- musician/models.py | 5 ++++ .../musician/address_check_delete.html | 12 ++++++++++ .../{mail_form.html => address_form.html} | 5 ++++ musician/urls.py | 1 + musician/views.py | 24 ++++++++++++++++--- 7 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 musician/templates/musician/address_check_delete.html rename musician/templates/musician/{mail_form.html => address_form.html} (70%) diff --git a/musician/api.py b/musician/api.py index 8e38e7c..178012e 100644 --- a/musician/api.py +++ b/musician/api.py @@ -178,6 +178,11 @@ class Orchestra(object): return addresses + def delete_mail_address(self, pk): + path = API_PATHS.get('address-detail').format_map({'pk': pk}) + url = urllib.parse.urljoin(self.base_url, path) + return self.request("DELETE", url=url, render_as=None) + 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] diff --git a/musician/forms.py b/musician/forms.py index 076898d..3f90a74 100644 --- a/musician/forms.py +++ b/musician/forms.py @@ -31,9 +31,9 @@ class MailForm(forms.Form): forward = forms.EmailField(required=False) def __init__(self, *args, **kwargs): - instance = kwargs.pop('instance', None) - if instance is not None: - kwargs['initial'] = instance.deserialize() + self.instance = kwargs.pop('instance', None) + if self.instance is not None: + kwargs['initial'] = self.instance.deserialize() domains = kwargs.pop('domains') mailboxes = kwargs.pop('mailboxes') diff --git a/musician/models.py b/musician/models.py index 9742f8c..9e9a31e 100644 --- a/musician/models.py +++ b/musician/models.py @@ -233,6 +233,7 @@ class Address(OrchestraModel): fields = ('mail_address', 'aliases', 'type', 'type_detail') param_defaults = { "id": None, + "name": None, "domain": None, "mailboxes": [], "forward": None, @@ -260,6 +261,10 @@ class Address(OrchestraModel): name + '@' + self.data['domain']['name'] for name in self.data['names'][1:] ] + @property + def full_address_name(self): + return "{}@{}".format(self.name, self.domain['name']) + @property def mail_address(self): return self.data['names'][0] + '@' + self.data['domain']['name'] diff --git a/musician/templates/musician/address_check_delete.html b/musician/templates/musician/address_check_delete.html new file mode 100644 index 0000000..651b365 --- /dev/null +++ b/musician/templates/musician/address_check_delete.html @@ -0,0 +1,12 @@ +{% extends "musician/base.html" %} +{% load i18n %} + +{% block content %} +
+ {% csrf_token %} +

{% blocktrans with address_name=object.full_address_name %}Are you sure that you want remove the address: "{{ address_name }}"?{% endblocktrans %}

+

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

+ + {% trans 'Cancel' %} +
+{% endblock %} diff --git a/musician/templates/musician/mail_form.html b/musician/templates/musician/address_form.html similarity index 70% rename from musician/templates/musician/mail_form.html rename to musician/templates/musician/address_form.html index 2f0aa07..de21067 100644 --- a/musician/templates/musician/mail_form.html +++ b/musician/templates/musician/address_form.html @@ -10,6 +10,11 @@ {% buttons %} {% trans "Cancel" %} + {% if form.instance %} +
+ {% trans "Delete" %} +
+ {% endif %} {% endbuttons %} {% endblock %} diff --git a/musician/urls.py b/musician/urls.py index 308bf06..caa6799 100644 --- a/musician/urls.py +++ b/musician/urls.py @@ -22,6 +22,7 @@ urlpatterns = [ path('address/', views.MailView.as_view(), name='address-list'), path('address/new/', views.MailCreateView.as_view(), name='address-create'), path('address//', views.MailUpdateView.as_view(), name='address-update'), + path('address//delete/', views.AddressDeleteView.as_view(), name='address-delete'), path('mailboxes/', views.MailboxesView.as_view(), name='mailbox-list'), path('mailing-lists/', views.MailingListsView.as_view(), name='mailing-lists'), path('databases/', views.DatabasesView.as_view(), name='database-list'), diff --git a/musician/views.py b/musician/views.py index 4e524ac..6a1efc1 100644 --- a/musician/views.py +++ b/musician/views.py @@ -9,7 +9,7 @@ from django.utils.translation import gettext_lazy as _ from django.views import View from django.views.generic.base import RedirectView, TemplateView from django.views.generic.detail import DetailView -from django.views.generic.edit import FormView +from django.views.generic.edit import DeleteView, FormView from django.views.generic.list import ListView from requests.exceptions import HTTPError @@ -205,7 +205,7 @@ class MailView(ServiceListView): class MailCreateView(CustomContextMixin, UserTokenRequiredMixin, FormView): service_class = Address - template_name = "musician/mail_form.html" + template_name = "musician/address_form.html" form_class = MailForm success_url = reverse_lazy("musician:address-list") extra_context = {'service': service_class} @@ -230,7 +230,7 @@ class MailCreateView(CustomContextMixin, UserTokenRequiredMixin, FormView): class MailUpdateView(CustomContextMixin, UserTokenRequiredMixin, FormView): service_class = Address - template_name = "musician/mail_form.html" + template_name = "musician/address_form.html" form_class = MailForm success_url = reverse_lazy("musician:address-list") extra_context = {'service': service_class} @@ -259,6 +259,24 @@ class MailUpdateView(CustomContextMixin, UserTokenRequiredMixin, FormView): return super().form_valid(form) +class AddressDeleteView(CustomContextMixin, UserTokenRequiredMixin, DeleteView): + template_name = "musician/address_check_delete.html" + success_url = reverse_lazy("musician:address-list") + + def get_object(self, queryset=None): + obj = self.orchestra.retrieve_mail_address(self.kwargs['pk']) + return obj + + def delete(self, request, *args, **kwargs): + self.object = self.get_object() + try: + self.orchestra.delete_mail_address(self.object.id) + except HTTPError as e: + print(e) + + return HttpResponseRedirect(self.success_url) + + class MailingListsView(ServiceListView): service_class = MailinglistService template_name = "musician/mailinglists.html"