Improved domain quota display.

see #1146
This commit is contained in:
Antoine Nguyen
2017-06-22 18:51:16 +02:00
parent 6b576980ce
commit 188216b96e
5 changed files with 40 additions and 13 deletions
+6 -6
View File
@@ -163,8 +163,8 @@ class Domain(AdminObject):
return "success"
@cached_property
def quota_usage(self):
"""Return current quota usage."""
def allocated_quota(self):
"""Return current quota allocation."""
if not self.quota:
return 0
if not self.mailbox_set.exists():
@@ -173,11 +173,11 @@ class Domain(AdminObject):
total=models.Sum("quota"))["total"]
@cached_property
def quota_usage_in_percent(self):
"""Return quota usage in percent."""
if not self.quota_usage:
def allocated_quota_in_percent(self):
"""Return allocated quota in percent."""
if not self.allocated_quota:
return 0
return int(self.quota_usage / float(self.quota) * 100)
return int(self.allocated_quota / float(self.quota) * 100)
def add_admin(self, account):
"""Add a new administrator to this domain.
+14 -1
View File
@@ -24,6 +24,17 @@ from .base import AdminObject
from .domain import Domain
class QuotaManager(models.Manager):
"""Custom manager for Quota."""
def get_domain_usage(self, domain):
"""Return current usage for domain."""
qset = self.get_queryset().filter(
username__endswith="@{}".format(domain.name))
result = qset.aggregate(usage=models.Sum("bytes"))
return result.get("usage", 0)
class Quota(models.Model):
"""Keeps track of Mailbox current quota."""
@@ -32,6 +43,8 @@ class Quota(models.Model):
bytes = models.BigIntegerField(default=0)
messages = models.IntegerField(default=0)
objects = QuotaManager()
class Meta:
app_label = "admin"
@@ -242,7 +255,7 @@ class Mailbox(AdminObject):
if self.domain.quota and not override_rules:
raise lib_exceptions.BadRequest(_("A quota is required"))
elif self.domain.quota:
quota_usage = self.domain.quota_usage
quota_usage = self.domain.allocated_quota
if old_quota:
quota_usage -= old_quota
if quota_usage + self.quota > self.domain.quota:
@@ -60,8 +60,13 @@
{% if domain.quota %}
<tr>
<th>{% trans "Usage" %}</a></th>
<td>{% include "common/progressbar.html" with id="quotabar" value=domain.quota_usage_in_percent %}</td>
</tr>
<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>
</td>
</tr>
{% endif %}
<tr>
<th>{% trans "Default mailbox quota" %}</a></th>
+3 -3
View File
@@ -205,11 +205,11 @@ class AccountTestCase(ModoTestCase):
dom.quota = 100
dom.save(update_fields=["quota"])
# 2 x 10MB
self.assertEqual(dom.quota_usage, 20)
self.assertEqual(dom.allocated_quota, 20)
self._set_quota("user@test.com", 80)
del dom.quota_usage
del dom.allocated_quota
# 10 + 80 < 100 => ok
self.assertEqual(dom.quota_usage, 90)
self.assertEqual(dom.allocated_quota, 90)
# 30 + 80 > 100 => failure
self._set_quota("admin@test.com", 30, 400)
+10 -1
View File
@@ -28,7 +28,7 @@ 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
from ..models import Domain, Mailbox, Quota
from .. import signals
@@ -176,10 +176,19 @@ class DomainDetailView(
result = signals.extra_domain_dashboard_widgets.send(
self.__class__, user=self.request.user, domain=self.object)
parameters = self.request.localconfig.parameters
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({
"templates": {"left": [], "right": []},
"enable_mx_checks": parameters.get_value("enable_mx_checks"),
"enable_dnsbl_checks": parameters.get_value("enable_dnsbl_checks"),
"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: