VirtualMailManager/account.py
branchv0.6.x
changeset 528 4b8c3f51d7da
parent 527 e09139525580
child 531 cf3eb03c1c4f
--- a/VirtualMailManager/account.py	Fri Apr 13 23:24:12 2012 +0200
+++ b/VirtualMailManager/account.py	Sat Apr 14 15:11:27 2012 +0000
@@ -8,8 +8,7 @@
     Virtual Mail Manager's Account class to manage e-mail accounts.
 """
 
-from VirtualMailManager.common import version_str, \
-     format_domain_default
+from VirtualMailManager.common import version_str
 from VirtualMailManager.constants import \
      ACCOUNT_EXISTS, ACCOUNT_MISSING_PASSWORD, ALIAS_PRESENT, \
      INVALID_ARGUMENT, INVALID_MAIL_LOCATION, NO_SUCH_ACCOUNT, \
@@ -60,9 +59,9 @@
                        self._addr.domainname, NO_SUCH_DOMAIN)
         self._uid = 0
         self._mail = None
-        self._qlimit = None
-        self._services = None
-        self._transport = None
+        self._qlimit = self._domain.quotalimit
+        self._services = self._domain.serviceset
+        self._transport = self._domain.transport
         self._passwd = None
         self._new = True
         self._load()
@@ -82,19 +81,12 @@
         dbc.close()
         if result:
             self._uid, _mid, _qid, _ssid, _tid = result
-
-            def load_helper(ctor, own, field, dbresult):
-                cur = None if own is None else getattr(own, field)
-                if cur != dbresult:
-                    kwargs = { field : dbresult }
-                    return None if dbresult is None \
-                                else ctor(self._dbh, **kwargs)
-
-            self._qlimit = load_helper(QuotaLimit, self._qlimit, 'qid', _qid)
-            self._services = load_helper(ServiceSet, self._services, 'ssid',
-                                         _ssid)
-            self._transport = load_helper(Transport, self._transport, 'tid',
-                                          _tid)
+            if _qid != self._qlimit.qid:
+                self._qlimit = QuotaLimit(self._dbh, qid=_qid)
+            if _ssid != self._services.ssid:
+                self._services = ServiceSet(self._dbh, ssid=_ssid)
+            if _tid != self._transport.tid:
+                self._transport = Transport(self._dbh, tid=_tid)
             self._mail = MailLocation(self._dbh, mid=_mid)
             self._new = False
 
@@ -231,10 +223,8 @@
                     'qid, ssid, tid) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)',
                     (self._addr.localpart,
                      pwhash(self._passwd, user=self._addr), self._uid,
-                     self._domain.gid, self._mail.mid,
-                     self._qlimit.qid if self._qlimit else None,
-                     self._services.ssid if self._services else None,
-                     self._transport.tid if self._transport else None))
+                     self._domain.gid, self._mail.mid, self._qlimit.qid,
+                     self._services.ssid, self._transport.tid))
         self._dbh.commit()
         dbc.close()
         self._new = False
@@ -277,13 +267,11 @@
             raise VMMError(_(u'PostgreSQL-based dictionary quota requires '
                              u'Dovecot >= v1.1.2.'), VMM_ERROR)
         self._chk_state()
+        assert isinstance(quotalimit, QuotaLimit)
         if quotalimit == self._qlimit:
             return
+        self._update_tables('qid', quotalimit.qid)
         self._qlimit = quotalimit
-        if quotalimit is not None:
-            assert isinstance(quotalimit, QuotaLimit)
-            quotalimit = quotalimit.qid
-        self._update_tables('qid', quotalimit)
 
     def update_serviceset(self, serviceset):
         """Assign a different set of services to the Account.
@@ -294,13 +282,11 @@
           the new service set.
         """
         self._chk_state()
+        assert isinstance(serviceset, ServiceSet)
         if serviceset == self._services:
             return
+        self._update_tables('ssid', serviceset.ssid)
         self._services = serviceset
-        if serviceset is not None:
-            assert isinstance(serviceset, ServiceSet)
-            serviceset = serviceset.ssid
-        self._update_tables('ssid', serviceset)
 
     def update_transport(self, transport):
         """Sets a new transport for the Account.
@@ -311,47 +297,24 @@
           the new transport
         """
         self._chk_state()
+        assert isinstance(transport, Transport)
         if transport == self._transport:
             return
+        if transport.transport.lower() in ('virtual', 'virtual:') and \
+           not self._mail.postfix:
+            raise AErr(_(u"Invalid transport '%(transport)s' for mailbox "
+                         u"format '%(mbfmt)s'.") %
+                       {'transport': transport, 'mbfmt': self._mail.mbformat},
+                       INVALID_MAIL_LOCATION)
+        self._update_tables('tid', transport.tid)
         self._transport = transport
-        if transport is not None:
-            assert isinstance(transport, Transport)
-            if transport.transport.lower() in ('virtual', 'virtual:') and \
-                not self._mail.postfix:
-                raise AErr(_(u"Invalid transport '%(transport)s' for mailbox "
-                             u"format '%(mbfmt)s'.") %
-                           {'transport': transport, 'mbfmt': self._mail.mbformat},
-                           INVALID_MAIL_LOCATION)
-            transport = transport.tid
-        self._update_tables('tid', transport)
-
-    def _get_info_transport(self):
-        if self._transport:
-            return self._transport.transport
-        return format_domain_default(self._domain.transport.transport)
-
-    def _get_info_serviceset(self):
-        if self._services:
-            services = self._services.services
-            fmt = lambda s: s
-        else:
-            services = self._domain.serviceset.services
-            fmt = format_domain_default
-
-        ret = {}
-        for service, state in services.iteritems():
-            # TP: A service (e.g. pop3 or imap) may be enabled/usable or
-            # disabled/unusable for a user.
-            ret[service] = fmt((_('disabled'), _('enabled'))[state])
-        return ret
 
     def get_info(self):
         """Returns a dict with some information about the Account.
 
         The keys of the dict are: 'address', 'gid', 'home', 'imap'
         'mail_location', 'name', 'pop3', 'sieve', 'smtp', transport', 'uid',
-        'uq_bytes', 'uq_messages', 'ql_bytes', 'ql_messages', and
-        'ql_domaindefault'.
+        'uq_bytes', 'uq_messages', 'ql_bytes', and 'ql_messages'.
         """
         self._chk_state()
         dbc = self._dbh.cursor()
@@ -363,20 +326,17 @@
         dbc.close()
         if info:
             info = dict(zip(('name', 'uq_bytes', 'uq_messages'), info))
-            info.update(self._get_info_serviceset())
+            for service, state in self._services.services.iteritems():
+                # TP: A service (e.g. pop3 or imap) may be enabled/usable or
+                # disabled/unusable for a user.
+                info[service] = (_('disabled'), _('enabled'))[state]
             info['address'] = self._addr
             info['gid'] = self._domain.gid
             info['home'] = '%s/%s' % (self._domain.directory, self._uid)
             info['mail_location'] = self._mail.mail_location
-            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['ql_bytes'] = self._qlimit.bytes
+            info['ql_messages'] = self._qlimit.messages
+            info['transport'] = self._transport.transport
             info['uid'] = self._uid
             return info
         # nearly impossibleā€½