Create api.retrieve_mail_address_list

This commit is contained in:
Santiago Lamora 2020-02-17 11:07:21 +01:00
parent 977c952cfd
commit f599dc6ca9
2 changed files with 33 additions and 27 deletions

View file

@ -1,6 +1,7 @@
import requests import requests
import urllib.parse import urllib.parse
from itertools import groupby
from django.conf import settings from django.conf import settings
from django.http import Http404 from django.http import Http404
from django.urls.exceptions import NoReverseMatch from django.urls.exceptions import NoReverseMatch
@ -108,6 +109,36 @@ class Orchestra(object):
raise Http404(_("No domain found matching the query")) raise Http404(_("No domain found matching the query"))
return bill_pdf 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): def retrieve_domain(self, pk):
path = API_PATHS.get('domain-detail').format_map({'pk': pk}) path = API_PATHS.get('domain-detail').format_map({'pk': pk})

View file

@ -1,5 +1,3 @@
from itertools import groupby
from django.conf import settings from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponse, HttpResponseRedirect from django.http import HttpResponse, HttpResponseRedirect
@ -170,34 +168,11 @@ class MailView(ServiceListView):
} }
def get_queryset(self): 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) # retrieve mails applying filters (if any)
queryfilter = self.get_queryfilter() queryfilter = self.get_queryfilter()
raw_data = self.orchestra.retrieve_service_list( addresses = self.orchestra.retrieve_mail_address_list(
self.service_class.api_name, querystring=queryfilter
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 return addresses
def get_queryfilter(self): def get_queryfilter(self):