Implement address delete

This commit is contained in:
Santiago L 2021-10-01 13:36:52 +02:00
parent 98dfa7a9f4
commit b0366ff1d0
7 changed files with 52 additions and 6 deletions

View file

@ -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]

View file

@ -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')

View file

@ -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']

View file

@ -0,0 +1,12 @@
{% extends "musician/base.html" %}
{% load i18n %}
{% block content %}
<form method="post">
{% csrf_token %}
<p>{% blocktrans with address_name=object.full_address_name %}Are you sure that you want remove the address: "{{ address_name }}"?{% endblocktrans %}</p>
<p><strong>{% trans 'NOTE: This action cannot be undone.' %}</strong></p>
<input class="btn btn-danger" type="submit" value="{% trans 'Delete' %}">
<a class="btn btn-secondary" href="{% url 'musician:address-update' view.kwargs.pk %}">{% trans 'Cancel' %}</a>
</form>
{% endblock %}

View file

@ -10,6 +10,11 @@
{% buttons %}
<a class="btn btn-light mr-2" href="{% url 'musician:address-list' %}">{% trans "Cancel" %}</a>
<button type="submit" class="btn btn-secondary">{% trans "Save" %}</button>
{% if form.instance %}
<div class="float-right">
<a class="btn btn-danger" href="{% url 'musician:address-delete' view.kwargs.pk %}">{% trans "Delete" %}</a>
</div>
{% endif %}
{% endbuttons %}
</form>
{% endblock %}

View file

@ -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/<int:pk>/', views.MailUpdateView.as_view(), name='address-update'),
path('address/<int:pk>/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'),

View file

@ -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"