VirtualMailManager/domain.py
branchv0.6.x
changeset 453 f2934105181b
parent 447 c31d604e26d3
child 492 e5c2b3647971
equal deleted inserted replaced
452:63ae77f05088 453:f2934105181b
   108             raise DomErr(_(u'There are %(account_count)u accounts, '
   108             raise DomErr(_(u'There are %(account_count)u accounts, '
   109                            u'%(alias_count)u aliases and %(relocated_count)u '
   109                            u'%(alias_count)u aliases and %(relocated_count)u '
   110                            u'relocated users.') % dict(zip(keys, result)),
   110                            u'relocated users.') % dict(zip(keys, result)),
   111                          ACCOUNT_AND_ALIAS_PRESENT)
   111                          ACCOUNT_AND_ALIAS_PRESENT)
   112 
   112 
   113     def _chk_state(self):
   113     def _chk_state(self, must_exist=True):
   114         """Throws a DomainError if the Domain is new - not saved in the
   114         """Checks the state of the Domain instance and will raise a
   115         database."""
   115         VirtualMailManager.errors.DomainError:
   116         if self._new:
   116           - if *must_exist* is `True` and the domain doesn't exist
       
   117           - or *must_exist* is `False` and the domain exists
       
   118         """
       
   119         if must_exist and self._new:
   117             raise DomErr(_(u"The domain '%s' does not exist.") % self._name,
   120             raise DomErr(_(u"The domain '%s' does not exist.") % self._name,
   118                          NO_SUCH_DOMAIN)
   121                          NO_SUCH_DOMAIN)
       
   122         elif not must_exist and not self._new:
       
   123             raise DomErr(_(u"The domain '%s' already exists.") % self._name,
       
   124                          DOMAIN_EXISTS)
   119 
   125 
   120     def _update_tables(self, column, value, force=False):
   126     def _update_tables(self, column, value, force=False):
   121         """Update various columns in the domain_data table. When *force* is
   127         """Update various columns in the domain_data table. When *force* is
   122         `True` also the corresponding column in the users table will be
   128         `True` also the corresponding column in the users table will be
   123         updated.
   129         updated.
   181         Argument:
   187         Argument:
   182 
   188 
   183         `basedir` : basestring
   189         `basedir` : basestring
   184           The base directory of all domains
   190           The base directory of all domains
   185         """
   191         """
   186         if not self._new:
   192         self._chk_state(False)
   187             raise DomErr(_(u"The domain '%s' already exists.") % self._name,
       
   188                          DOMAIN_EXISTS)
       
   189         assert self._directory is None
   193         assert self._directory is None
   190         self._set_gid()
   194         self._set_gid()
   191         self._directory = os.path.join(basedir, choice(MAILDIR_CHARS),
   195         self._directory = os.path.join(basedir, choice(MAILDIR_CHARS),
   192                                        str(self._gid))
   196                                        str(self._gid))
   193 
   197 
   197         Argument:
   201         Argument:
   198 
   202 
   199         `quotalimit` : VirtualMailManager.quotalimit.QuotaLimit
   203         `quotalimit` : VirtualMailManager.quotalimit.QuotaLimit
   200           The quota limit of the new Domain.
   204           The quota limit of the new Domain.
   201         """
   205         """
   202         if not self._new:
   206         self._chk_state(False)
   203             raise DomErr(_(u"The domain '%s' already exists.") % self._name,
       
   204                          DOMAIN_EXISTS)
       
   205         assert isinstance(quotalimit, QuotaLimit)
   207         assert isinstance(quotalimit, QuotaLimit)
   206         self._qlimit = quotalimit
   208         self._qlimit = quotalimit
   207 
   209 
   208     def set_serviceset(self, serviceset):
   210     def set_serviceset(self, serviceset):
   209         """Set the services for the new Domain.
   211         """Set the services for the new Domain.
   211         Argument:
   213         Argument:
   212 
   214 
   213        `serviceset` : VirtualMailManager.serviceset.ServiceSet
   215        `serviceset` : VirtualMailManager.serviceset.ServiceSet
   214          The service set for the new Domain.
   216          The service set for the new Domain.
   215         """
   217         """
   216         if not self._new:
   218         self._chk_state(False)
   217             raise DomErr(_(u"The domain '%s' already exists.") % self._name,
       
   218                          DOMAIN_EXISTS)
       
   219         assert isinstance(serviceset, ServiceSet)
   219         assert isinstance(serviceset, ServiceSet)
   220         self._services = serviceset
   220         self._services = serviceset
   221 
   221 
   222     def set_transport(self, transport):
   222     def set_transport(self, transport):
   223         """Set the transport for the new Domain.
   223         """Set the transport for the new Domain.
   225         Argument:
   225         Argument:
   226 
   226 
   227         `transport` : VirtualMailManager.Transport
   227         `transport` : VirtualMailManager.Transport
   228           The transport of the new Domain
   228           The transport of the new Domain
   229         """
   229         """
   230         if not self._new:
   230         self._chk_state(False)
   231             raise DomErr(_(u"The domain '%s' already exists.") % self._name,
       
   232                          DOMAIN_EXISTS)
       
   233         assert isinstance(transport, Transport)
   231         assert isinstance(transport, Transport)
   234         self._transport = transport
   232         self._transport = transport
   235 
   233 
   236     def save(self):
   234     def save(self):
   237         """Stores the new domain in the database."""
   235         """Stores the new domain in the database."""
   238         if not self._new:
   236         self._chk_state(False)
   239             raise DomErr(_(u"The domain '%s' already exists.") % self._name,
       
   240                          DOMAIN_EXISTS)
       
   241         assert all((self._directory, self._qlimit, self._services,
   237         assert all((self._directory, self._qlimit, self._services,
   242                     self._transport))
   238                     self._transport))
   243         dbc = self._dbh.cursor()
   239         dbc = self._dbh.cursor()
   244         dbc.execute('INSERT INTO domain_data (gid, qid, ssid, tid, domaindir) '
   240         dbc.execute('INSERT INTO domain_data (gid, qid, ssid, tid, domaindir) '
   245                     'VALUES (%s, %s, %s, %s, %s)', (self._gid,
   241                     'VALUES (%s, %s, %s, %s, %s)', (self._gid,