VMM/domain: Integrated class ServiceSet in class Domain. v0.6.x
authorPascal Volk <neverseen@users.sourceforge.net>
Tue, 01 Nov 2011 02:13:57 +0000
branchv0.6.x
changeset 441 1af5fe0683ce
parent 440 1a08fe35b496
child 442 abcd73f5e980
VMM/domain: Integrated class ServiceSet in class Domain. VMM/handler: Extended Handler.domain_add() and added Handler.domain_services(). Removed unused domain.get_gid import.
VirtualMailManager/domain.py
VirtualMailManager/handler.py
--- a/VirtualMailManager/domain.py	Tue Nov 01 01:01:33 2011 +0000
+++ b/VirtualMailManager/domain.py	Tue Nov 01 02:13:57 2011 +0000
@@ -18,6 +18,7 @@
 from VirtualMailManager.errors import VMMError, DomainError as DomErr
 from VirtualMailManager.pycompat import all, any
 from VirtualMailManager.quotalimit import QuotaLimit
+from VirtualMailManager.serviceset import ServiceSet
 from VirtualMailManager.transport import Transport
 
 
@@ -29,8 +30,8 @@
 
 class Domain(object):
     """Class to manage e-mail domains."""
-    __slots__ = ('_directory', '_gid', '_name', '_qlimit', '_transport',
-                 '_dbh', '_new')
+    __slots__ = ('_directory', '_gid', '_name', '_qlimit', '_services',
+                 '_transport', '_dbh', '_new')
 
     def __init__(self, dbh, domainname):
         """Creates a new Domain instance.
@@ -53,6 +54,7 @@
         self._dbh = dbh
         self._gid = 0
         self._qlimit = None
+        self._services = None
         self._transport = None
         self._directory = None
         self._new = True
@@ -66,18 +68,19 @@
         domain.
         """
         dbc = self._dbh.cursor()
-        dbc.execute('SELECT dd.gid, qid, tid, domaindir, is_primary FROM '
-                    'domain_data dd, domain_name dn WHERE domainname = %s AND '
-                    'dn.gid = dd.gid', (self._name,))
+        dbc.execute('SELECT dd.gid, qid, ssid, tid, domaindir, is_primary '
+                    'FROM domain_data dd, domain_name dn WHERE domainname = '
+                    '%s AND dn.gid = dd.gid', (self._name,))
         result = dbc.fetchone()
         dbc.close()
         if result:
-            if not result[4]:
+            if not result[5]:
                 raise DomErr(_(u"The domain '%s' is an alias domain.") %
                              self._name, DOMAIN_ALIAS_EXISTS)
-            self._gid, self._directory = result[0], result[3]
+            self._gid, self._directory = result[0], result[4]
             self._qlimit = QuotaLimit(self._dbh, qid=result[1])
-            self._transport = Transport(self._dbh, tid=result[2])
+            self._services = ServiceSet(self._dbh, ssid=result[2])
+            self._transport = Transport(self._dbh, tid=result[3])
             self._new = False
 
     def _set_gid(self):
@@ -122,13 +125,13 @@
         Arguments:
 
         `column` : basestring
-          Name of the table column. Currently: qid and tid
+          Name of the table column. Currently: qid, ssid and tid
         `value` : long
           The referenced key
         `force` : bool
           enforce the new setting also for existing users. Default: `False`
         """
-        if column not in ('qid', 'tid'):
+        if column not in ('qid', 'ssid', 'tid'):
             raise ValueError('Unknown column: %r' % column)
         dbc = self._dbh.cursor()
         dbc.execute('UPDATE domain_data SET %s = %%s WHERE gid = %%s' % column,
@@ -163,6 +166,11 @@
         return self._qlimit
 
     @property
+    def serviceset(self):
+        """The Domain's serviceset."""
+        return self._services
+
+    @property
     def transport(self):
         """The Domain's transport."""
         return self._transport
@@ -197,6 +205,20 @@
         assert isinstance(quotalimit, QuotaLimit)
         self._qlimit = quotalimit
 
+    def set_serviceset(self, serviceset):
+        """Set the services for the new Domain.
+
+        Argument:
+
+       `serviceset` : VirtualMailManager.serviceset.ServiceSet
+         The service set for the new Domain.
+        """
+        if not self._new:
+            raise DomErr(_(u"The domain '%s' already exists.") % self._name,
+                         DOMAIN_EXISTS)
+        assert isinstance(serviceset, ServiceSet)
+        self._services = serviceset
+
     def set_transport(self, transport):
         """Set the transport for the new Domain.
 
@@ -216,11 +238,13 @@
         if not self._new:
             raise DomErr(_(u"The domain '%s' already exists.") % self._name,
                          DOMAIN_EXISTS)
-        assert all((self._directory, self._qlimit, self._transport))
+        assert all((self._directory, self._qlimit, self._services,
+                    self._transport))
         dbc = self._dbh.cursor()
-        dbc.execute('INSERT INTO domain_data (gid, qid, tid, domaindir) '
-                    'VALUES (%s, %s, %s, %s)', (self._gid, self._qlimit.qid,
-                    self._transport.tid, self._directory))
+        dbc.execute('INSERT INTO domain_data (gid, qid, ssid, tid, domaindir) '
+                    'VALUES (%s, %s, %s, %s, %s)', (self._gid,
+                    self._qlimit.qid, self._services.ssid, self._transport.tid,
+                    self._directory))
         dbc.execute('INSERT INTO domain_name (domainname, gid, is_primary) '
                     'VALUES (%s, %s, TRUE)', (self._name, self._gid))
         self._dbh.commit()
@@ -251,6 +275,7 @@
         dbc.close()
         self._gid = 0
         self._directory = self._qlimit = self._transport = None
+        self._services = None
         self._new = True
 
     def update_quotalimit(self, quotalimit, force=False):
@@ -277,6 +302,26 @@
         self._update_tables('qid', quotalimit.qid, force)
         self._qlimit = quotalimit
 
+    def update_serviceset(self, serviceset, force=False):
+        """Assign a different set of services to the Domain,
+
+        If *force* is `True` the *serviceset* will be also assigned to
+        all existing accounts of the Domain.  Otherwise the *serviceset*
+        will be only the 'default' for accounts created from now on.
+
+        Arguments:
+        `serviceset` : VirtualMailManager.serviceset.ServiceSet
+          the new set of services
+        `force`
+          enforce the serviceset for all accounts, default `False`
+        """
+        self._chk_state()
+        assert isinstance(serviceset, ServiceSet)
+        if not force and serviceset == self._services:
+            return
+        self._update_tables('ssid', serviceset.ssid, force)
+        self._services = serviceset
+
     def update_transport(self, transport, force=False):
         """Sets a new transport for the Domain.
 
--- a/VirtualMailManager/handler.py	Tue Nov 01 01:01:33 2011 +0000
+++ b/VirtualMailManager/handler.py	Tue Nov 01 02:13:57 2011 +0000
@@ -28,8 +28,9 @@
      DATABASE_ERROR, DOMAINDIR_GROUP_MISMATCH, DOMAIN_INVALID, \
      FOUND_DOTS_IN_PATH, INVALID_ARGUMENT, MAILDIR_PERM_MISMATCH, \
      NOT_EXECUTABLE, NO_SUCH_ACCOUNT, NO_SUCH_ALIAS, NO_SUCH_BINARY, \
-     NO_SUCH_DIRECTORY, NO_SUCH_RELOCATED, RELOCATED_EXISTS, VMM_ERROR
-from VirtualMailManager.domain import Domain, get_gid
+     NO_SUCH_DIRECTORY, NO_SUCH_RELOCATED, RELOCATED_EXISTS, UNKNOWN_SERVICE, \
+     VMM_ERROR
+from VirtualMailManager.domain import Domain
 from VirtualMailManager.emailaddress import DestinationEmailAddress, \
      EmailAddress
 from VirtualMailManager.errors import \
@@ -38,6 +39,7 @@
 from VirtualMailManager.pycompat import all, any
 from VirtualMailManager.quotalimit import QuotaLimit
 from VirtualMailManager.relocated import Relocated
+from VirtualMailManager.serviceset import ServiceSet, SERVICES
 from VirtualMailManager.transport import Transport
 
 
@@ -433,6 +435,11 @@
         dom.set_quotalimit(QuotaLimit(self._dbh,
                            bytes=long(self._cfg.dget('misc.quota_bytes')),
                            messages=self._cfg.dget('misc.quota_messages')))
+        dom.set_serviceset(ServiceSet(self._dbh,
+                                      imap=self._cfg.dget('account.imap'),
+                                      pop3=self._cfg.dget('account.pop3'),
+                                      sieve=self._cfg.dget('account.sieve'),
+                                      smtp=self._cfg.dget('account.smtp')))
         dom.set_directory(self._cfg.dget('misc.base_directory'))
         dom.save()
         self._make_domain_dir(dom)
@@ -452,6 +459,22 @@
         else:
             dom.update_quotalimit(quotalimit, force=True)
 
+    def domain_services(self, domainname, force=None, *services):
+        """Wrapper around Domain.update_serviceset()."""
+        kwargs = dict.fromkeys(SERVICES, False)
+        if force is not None and force != 'force':
+            raise DomainError(_(u"Invalid argument: '%s'") % force,
+                              INVALID_ARGUMENT)
+        for service in set(services):
+            if service not in SERVICES:
+                raise DomainError(_(u"Unknown service: '%s'") % service,
+                                  UNKNOWN_SERVICE)
+            kwargs[service] = True
+
+        dom = self._get_domain(domainname)
+        serviceset = ServiceSet(self._dbh, **kwargs)
+        dom.update_serviceset(serviceset, (True, False)[not force])
+
     def domain_transport(self, domainname, transport, force=None):
         """Wrapper around Domain.update_transport()"""
         if force is not None and force != 'force':