Modify userinfo output to indicate when domain defaults are displayed v0.6.x
authormartin f. krafft <madduck@madduck.net>
Fri, 13 Apr 2012 23:24:12 +0200
branchv0.6.x
changeset 527 e09139525580
parent 526 85517c8fde36
child 528 4b8c3f51d7da
Modify userinfo output to indicate when domain defaults are displayed When Account instances reference NULL tid/qid/ssid, the data must come from the associated domain, and this should be indicated. For transport and services, this is easy to do as the string passed in the info dict can simply be modified. For quotalimit, however, another method must be used due to the CLI-side formatting. All approaches use a common formatter outsourced to the common.py file.
VirtualMailManager/account.py
VirtualMailManager/cli/subcommands.py
VirtualMailManager/common.py
--- a/VirtualMailManager/account.py	Fri Apr 13 23:09:40 2012 +0200
+++ b/VirtualMailManager/account.py	Fri Apr 13 23:24:12 2012 +0200
@@ -8,7 +8,8 @@
     Virtual Mail Manager's Account class to manage e-mail accounts.
 """
 
-from VirtualMailManager.common import version_str
+from VirtualMailManager.common import version_str, \
+     format_domain_default
 from VirtualMailManager.constants import \
      ACCOUNT_EXISTS, ACCOUNT_MISSING_PASSWORD, ALIAS_PRESENT, \
      INVALID_ARGUMENT, INVALID_MAIL_LOCATION, NO_SUCH_ACCOUNT, \
@@ -349,7 +350,8 @@
 
         The keys of the dict are: 'address', 'gid', 'home', 'imap'
         'mail_location', 'name', 'pop3', 'sieve', 'smtp', transport', 'uid',
-        'uq_bytes', 'uq_messages', 'ql_bytes', and 'ql_messages'.
+        'uq_bytes', 'uq_messages', 'ql_bytes', 'ql_messages', and
+        'ql_domaindefault'.
         """
         self._chk_state()
         dbc = self._dbh.cursor()
@@ -366,9 +368,15 @@
             info['gid'] = self._domain.gid
             info['home'] = '%s/%s' % (self._domain.directory, self._uid)
             info['mail_location'] = self._mail.mail_location
-            info['ql_bytes'] = self._qlimit.bytes
-            info['ql_messages'] = self._qlimit.messages
-            info['transport'] = self._transport.transport
+            if self._qlimit:
+                info['ql_bytes'] = self._qlimit.bytes
+                info['ql_messages'] = self._qlimit.messages
+                info['ql_domaindefault'] = False
+            else:
+                info['ql_bytes'] = self._domain.quotalimit.bytes
+                info['ql_messages'] = self._domain.quotalimit.messages
+                info['ql_domaindefault'] = True
+            info['transport'] = self._get_info_transport()
             info['uid'] = self._uid
             return info
         # nearly impossibleā€½
--- a/VirtualMailManager/cli/subcommands.py	Fri Apr 13 23:09:40 2012 +0200
+++ b/VirtualMailManager/cli/subcommands.py	Fri Apr 13 23:24:12 2012 +0200
@@ -16,7 +16,8 @@
 
 from VirtualMailManager import ENCODING
 from VirtualMailManager.cli import get_winsize, prog, w_err, w_std
-from VirtualMailManager.common import human_size, size_in_bytes, version_str
+from VirtualMailManager.common import human_size, size_in_bytes, \
+     version_str, format_domain_default
 from VirtualMailManager.constants import __copyright__, __date__, \
      __version__, ACCOUNT_EXISTS, ALIAS_EXISTS, ALIASDOMAIN_ISDOMAIN, \
      DOMAIN_ALIAS_EXISTS, INVALID_ARGUMENT, EX_MISSING_ARGS, RELOCATED_EXISTS
@@ -572,15 +573,17 @@
     else:
         if details in (None, 'du'):
             info['quota storage'] = _format_quota_usage(info['ql_bytes'],
-                    info['uq_bytes'], True)
+                    info['uq_bytes'], True, info['ql_domaindefault'])
             info['quota messages'] = _format_quota_usage(info['ql_messages'],
-                    info['uq_messages'])
+                    info['uq_messages'], domaindefault=info['ql_domaindefault'])
             _print_info(ctx, info, _(u'Account'))
         else:
             info[0]['quota storage'] = _format_quota_usage(info[0]['ql_bytes'],
-                    info[0]['uq_bytes'], True)
-            info[0]['quota messages'] = _format_quota_usage(
-                    info[0]['ql_messages'], info[0]['uq_messages'])
+                    info[0]['uq_bytes'], True, info[0]['ql_domaindefault'])
+            info[0]['quota messages'] = \
+                _format_quota_usage(info[0]['ql_messages'],
+                                    info[0]['uq_messages'],
+                                    domaindefault=info[0]['ql_domaindefault'])
             _print_info(ctx, info[0], _(u'Account'))
             _print_list(info[1], _(u'alias addresses'))
 
@@ -830,7 +833,7 @@
     return order
 
 
-def _format_quota_usage(limit, used, human=False):
+def _format_quota_usage(limit, used, human=False, domaindefault=False):
     """Put quota's limit / usage / percentage in a formatted string."""
     if human:
         q_usage = {
@@ -846,7 +849,8 @@
         q_usage['percent'] = locale.format('%6.2f', 100. / limit * used, True)
     else:
         q_usage['percent'] = locale.format('%6.2f', 0, True)
-    return _(u'[%(percent)s%%] %(used)s/%(limit)s') % q_usage
+    fmt = format_domain_default if domaindefault else lambda s: s
+    return fmt(_(u'[%(percent)s%%] %(used)s/%(limit)s') % q_usage)
 
 
 def _print_info(ctx, info, title):
--- a/VirtualMailManager/common.py	Fri Apr 13 23:09:40 2012 +0200
+++ b/VirtualMailManager/common.py	Fri Apr 13 23:24:12 2012 +0200
@@ -186,4 +186,7 @@
     _version_cache[version] = version_string
     return version_string
 
+def format_domain_default(domaindata):
+    return _(u'%s [domain default]') % domaindata
+
 del _