Commit b2480c3d authored by Aurélien Bompard's avatar Aurélien Bompard

Merge branch 'advertised-lists' into 'master'

Improve perf when getting advertised lists only

This change makes use of the new `advertised` filter in Mailman's REST API.

See merge request !191
parents 8564115d f174e616
Pipeline #5292080 passed with stage
in 3 minutes and 1 second
......@@ -129,24 +129,18 @@ class MailmanListManager(MailmanRestManager):
def __init__(self):
super(MailmanListManager, self).__init__('list', 'lists')
def all(self, only_public=False):
def all(self, advertised=False):
try:
objects = getattr(get_mailman_client(), self.resource_name_plural)
method = getattr(
get_mailman_client(), 'get_' + self.resource_name_plural)
return method(advertised=advertised)
except AttributeError:
raise MailmanApiError
except MailmanConnectionError as e:
raise MailmanApiError(e)
if only_public:
public = []
for obj in objects:
if obj.settings.get('advertised', False):
public.append(obj)
return public
else:
return objects
def by_mail_host(self, mail_host, only_public=False):
objects = self.all(only_public)
def by_mail_host(self, mail_host, advertised=False):
objects = self.all(advertised)
host_objects = []
for obj in objects:
if obj.mail_host == mail_host:
......
......@@ -36,7 +36,7 @@
<tr>
<td>
<a href="{% url 'list_summary' list_id=list.list_id %}">{{ list.display_name }}</a>
{% if not list.settings.advertised %} ({% trans 'unadvertised' %}*){% endif %}
{% if user.is_superuser and not list.settings.advertised %} ({% trans 'unadvertised' %}*){% endif %}
</td>
<td>{{ list.fqdn_listname }}</td>
<td>{{ list.settings.description }}</td>
......
......@@ -39,3 +39,15 @@ class ListIndexPageTest(ViewTestCase):
# The lists should be sorted by address
self.assertEqual([l.fqdn_listname for l in response.context['lists']],
['bar@example.com', 'foo@example.com'])
def test_list_index_only_contains_advertised_lists(self):
# The list index page should contain only contain the advertised lists
baz_list = self.domain.create_list('baz')
baz_list.settings['advertised'] = False
baz_list.settings.save()
response = self.client.get(reverse('list_index'))
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.context['lists']), 2)
self.assertNotIn(
'baz.example.com',
[ml.list_id for ml in response.context['lists']])
......@@ -550,25 +550,21 @@ def list_index(request, template='postorius/index.html'):
"""
if request.method == 'POST':
return redirect("list_summary", list_id=request.POST["list"])
lists = []
error = None
only_public = True
if request.user.is_superuser:
only_public = False
def _get_list_page(count, page):
client = get_mailman_client()
advertised = not request.user.is_superuser
return client.get_list_page(
advertised=advertised, count=count, page=page)
try:
# FIXME: this is not paginated, all lists will
# always be retrieved.
lists = sorted(List.objects.all(only_public=only_public),
key=lambda l: l.fqdn_listname)
logger.debug(lists)
lists = paginate(
_get_list_page, request.GET.get('page'), request.GET.get('count'),
paginator_class=MailmanPaginator)
except MailmanApiError:
return utils.render_api_error(request)
choosable_domains = _get_choosable_domains(request)
return render(request, template,
{'error': error,
'lists': paginate(
lists, request.GET.get('page'),
request.GET.get('count', 10)),
{'lists': lists,
'domain_count': len(choosable_domains)})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment