Merge pull request #1264 from modoboa/feature/domain_quota_list_filters

Added sorting filters to quota list.
This commit is contained in:
Antoine Nguyen
2017-10-06 18:19:45 +02:00
committed by GitHub
4 changed files with 23 additions and 19 deletions
@@ -62,8 +62,8 @@
<th>{% trans "Usage" %}</a></th>
<td>
<div class="progress">
<div class="{% progress_color used_quota_in_percent %}" style="width: {{ used_quota_in_percent }}%" title="{{ used_quota_in_percent }}% ({{ used_quota }} {% trans "MB" %}) {% trans 'used' context 'quota' %}"></div>
<div class="{% progress_color domain.allocated_quota_in_percent %}" style="width: {{ allocated_quota }}%" title="{{ domain.allocated_quota_in_percent }}% ({{ domain.allocated_quota }} {% trans "MB" %}) {% trans 'allocated' %}"></div>
<div class="{% progress_color domain.used_quota_in_percent %}" style="width: {{ domain.used_quota_in_percent }}%" title="{{ domain.used_quota_in_percent }}% ({{ domain.used_quota }} {% trans "MB" %}) {% trans 'used' context 'quota' %}"></div>
<div class="{% progress_color domain.allocated_quota_in_percent %}" style="width: {{ domain.allocated_quota_in_percent }}%" title="{{ domain.allocated_quota_in_percent }}% ({{ domain.allocated_quota }} {% trans "MB" %}) {% trans 'allocated' %}"></div>
</div>
</td>
</tr>
@@ -1,6 +1,6 @@
{% load i18n %}
<th width="50%" class="sortable" data-sort_order="domain">{% trans "Domain" %}</th>
<th width="50%" class="sortable" data-sort_order="name">{% trans "Domain" %}</th>
<th class="sortable" data-sort_order="quota">{% trans "Limit" %}</th>
<th class="sortable" data-sort_order="quota_value__used">{% trans "Used" %}</th>
<th class="sortable" data-sort_order="quota_value__allocated">{% trans "Allocated" %}</th>
<th class="sortable" data-sort_order="allocated_quota">{% trans "Allocated" %}</th>
<th class="sortable" data-sort_order="quota_usage" id="quotabar">{% trans "Usage (%)" %}</th>
+8
View File
@@ -310,12 +310,20 @@ class DomainTestCase(ModoTestCase):
def test_domain_quota_list_view(self):
"""Test quota list view."""
factories.DomainFactory(name="test3.com", quota=100)
url = reverse("admin:domain_quota_list")
response = self.ajax_get(url)
self.assertIn("50M", response["rows"])
self.assertIn('title="40% (20 MB) allocated"', response["rows"])
self.assertIn("test.com", response["rows"])
old_rows = response["rows"]
response = self.ajax_get("{}?sort_order=-name".format(url))
self.assertNotEqual(old_rows, response["rows"])
response = self.ajax_get("{}?sort_order=allocated_quota".format(url))
self.assertNotEqual(old_rows, response["rows"])
def test_statitics_widget(self):
"""Test statistics display in dashboard."""
url = reverse("core:dashboard")
+11 -15
View File
@@ -5,6 +5,7 @@ from __future__ import unicode_literals
from functools import reduce
from django.core.urlresolvers import reverse
from django.db.models import Sum
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.template.loader import render_to_string
@@ -22,13 +23,13 @@ from reversion import revisions as reversion
from modoboa.core import signals as core_signals
from modoboa.lib.web_utils import render_to_json_response
from modoboa.lib.exceptions import (
PermDeniedException
BadRequest, PermDeniedException
)
from modoboa.lib.listing import get_sort_order, get_listing_page
from ..forms import DomainForm, DomainWizard
from ..lib import get_domains
from ..models import Domain, Mailbox, Quota
from ..models import Domain, Mailbox
from .. import signals
@@ -122,9 +123,16 @@ def domains_list(request):
def list_quotas(request):
from modoboa.lib.db_utils import db_type
sort_order, sort_dir = get_sort_order(request.GET, "domain")
sort_order, sort_dir = get_sort_order(request.GET, "name")
domains = Domain.objects.get_for_admin(request.user)
domains = domains.exclude(quota=0)
if sort_order in ["name", "quota"]:
domains = domains.order_by("{}{}".format(sort_dir, sort_order))
elif sort_order == "allocated_quota":
domains = (
domains.annotate(allocated_quota=Sum("mailbox__quota"))
.order_by("{}{}".format(sort_dir, sort_order))
)
page = get_listing_page(domains, request.GET.get("page", 1))
context = {
"headers": render_to_string(
@@ -212,18 +220,6 @@ class DomainDetailView(
"enable_mx_checks": parameters.get_value("enable_mx_checks"),
"enable_dnsbl_checks": parameters.get_value("enable_dnsbl_checks"),
})
if self.object.quota:
used_quota = int(
Quota.objects.get_domain_usage(self.object) / 1048576)
used_quota_in_percent = int(
used_quota / float(self.object.quota) * 100)
allocated_quota = (
self.object.allocated_quota_in_percent - used_quota_in_percent)
context.update({
"used_quota": used_quota,
"used_quota_in_percent": used_quota_in_percent,
"allocated_quota": allocated_quota
})
for receiver, widgets in result:
for widget in widgets:
context["templates"][widget["column"]].append(