From f599dc6ca91206d643c24d81d004cb2bf9787b1e Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Mon, 17 Feb 2020 11:07:21 +0100 Subject: [PATCH] Create api.retrieve_mail_address_list --- musician/api.py | 31 +++++++++++++++++++++++++++++++ musician/views.py | 29 ++--------------------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/musician/api.py b/musician/api.py index bbf77a5..95443b8 100644 --- a/musician/api.py +++ b/musician/api.py @@ -1,6 +1,7 @@ import requests import urllib.parse +from itertools import groupby from django.conf import settings from django.http import Http404 from django.urls.exceptions import NoReverseMatch @@ -108,6 +109,36 @@ class Orchestra(object): raise Http404(_("No domain found matching the query")) return bill_pdf + def retrieve_mail_address_list(self, querystring=None): + def get_mailbox_id(value): + mailboxes = value.get('mailboxes') + + # forwarded address should not grouped + if len(mailboxes) == 0: + return value.get('name') + + return mailboxes[0]['id'] + + # retrieve mails applying filters (if any) + raw_data = self.retrieve_service_list( + MailService.api_name, + querystring=querystring, + ) + + # group addresses with the same mailbox + addresses = [] + for key, group in groupby(raw_data, get_mailbox_id): + aliases = [] + data = {} + for thing in group: + aliases.append(thing.pop('name')) + data = thing + + data['names'] = aliases + addresses.append(MailService.new_from_json(data)) + + return addresses + def retrieve_domain(self, pk): path = API_PATHS.get('domain-detail').format_map({'pk': pk}) diff --git a/musician/views.py b/musician/views.py index 102a66d..85a6d4e 100644 --- a/musician/views.py +++ b/musician/views.py @@ -1,5 +1,3 @@ -from itertools import groupby - from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.http import HttpResponse, HttpResponseRedirect @@ -170,34 +168,11 @@ class MailView(ServiceListView): } def get_queryset(self): - def retrieve_mailbox(value): - mailboxes = value.get('mailboxes') - - # forwarded address should not grouped - if len(mailboxes) == 0: - return value.get('name') - - return mailboxes[0]['id'] - # retrieve mails applying filters (if any) queryfilter = self.get_queryfilter() - raw_data = self.orchestra.retrieve_service_list( - self.service_class.api_name, - querystring=queryfilter, + addresses = self.orchestra.retrieve_mail_address_list( + querystring=queryfilter ) - - # group addresses with the same mailbox - addresses = [] - for key, group in groupby(raw_data, retrieve_mailbox): - aliases = [] - data = {} - for thing in group: - aliases.append(thing.pop('name')) - data = thing - - data['names'] = aliases - addresses.append(self.service_class.new_from_json(data)) - return addresses def get_queryfilter(self):