VirtualMailManager/Domain.py
branchv0.6.x
changeset 216 0c8c053b451c
parent 199 0684790fff7c
child 222 d0c16e70a9fb
equal deleted inserted replaced
215:33f727efa7c4 216:0c8c053b451c
     8 
     8 
     9 from VirtualMailManager import check_domainname
     9 from VirtualMailManager import check_domainname
    10 from VirtualMailManager.constants.ERROR import \
    10 from VirtualMailManager.constants.ERROR import \
    11      ACCOUNT_AND_ALIAS_PRESENT, ACCOUNT_PRESENT, ALIAS_PRESENT, \
    11      ACCOUNT_AND_ALIAS_PRESENT, ACCOUNT_PRESENT, ALIAS_PRESENT, \
    12      DOMAIN_ALIAS_EXISTS, DOMAIN_EXISTS, NO_SUCH_DOMAIN
    12      DOMAIN_ALIAS_EXISTS, DOMAIN_EXISTS, NO_SUCH_DOMAIN
    13 from VirtualMailManager.Exceptions import VMMDomainException as VMMDE
    13 from VirtualMailManager.errors import DomainError as DomErr
    14 from VirtualMailManager.Transport import Transport
    14 from VirtualMailManager.Transport import Transport
    15 
    15 
    16 
    16 
    17 MAILDIR_CHARS = '0123456789abcdefghijklmnopqrstuvwxyz'
    17 MAILDIR_CHARS = '0123456789abcdefghijklmnopqrstuvwxyz'
    18 
    18 
    36         else:
    36         else:
    37             self._transport = transport
    37             self._transport = transport
    38         self._id = 0
    38         self._id = 0
    39         self._domaindir = None
    39         self._domaindir = None
    40         if not self._exists() and self._isAlias():
    40         if not self._exists() and self._isAlias():
    41             raise VMMDE(_(u"The domain “%s” is an alias domain.") % self._name,
    41             raise DomErr(_(u"The domain “%s” is an alias domain.") %
    42                         DOMAIN_ALIAS_EXISTS)
    42                          self._name, DOMAIN_ALIAS_EXISTS)
    43 
    43 
    44     def _exists(self):
    44     def _exists(self):
    45         """Checks if the domain already exists.
    45         """Checks if the domain already exists.
    46 
    46 
    47         If the domain exists _id will be set and returns True, otherwise False
    47         If the domain exists _id will be set and returns True, otherwise False
   121         if not delAlias:
   121         if not delAlias:
   122             hasAlias = self._has('alias')
   122             hasAlias = self._has('alias')
   123         else:
   123         else:
   124             hasAlias = False
   124             hasAlias = False
   125         if hasUser and hasAlias:
   125         if hasUser and hasAlias:
   126             raise VMMDE(_(u'There are accounts and aliases.'),
   126             raise DomErr(_(u'There are accounts and aliases.'),
   127                 ACCOUNT_AND_ALIAS_PRESENT)
   127                          ACCOUNT_AND_ALIAS_PRESENT)
   128         elif hasUser:
   128         elif hasUser:
   129             raise VMMDE(_(u'There are accounts.'), ACCOUNT_PRESENT)
   129             raise DomErr(_(u'There are accounts.'), ACCOUNT_PRESENT)
   130         elif hasAlias:
   130         elif hasAlias:
   131             raise VMMDE(_(u'There are aliases.'), ALIAS_PRESENT)
   131             raise DomErr(_(u'There are aliases.'), ALIAS_PRESENT)
   132 
   132 
   133     def save(self):
   133     def save(self):
   134         """Stores the new domain in the database."""
   134         """Stores the new domain in the database."""
   135         if self._id < 1:
   135         if self._id < 1:
   136             self._prepare()
   136             self._prepare()
   140             dbc.execute("INSERT INTO domain_name (domainname, gid, is_primary)\
   140             dbc.execute("INSERT INTO domain_name (domainname, gid, is_primary)\
   141  VALUES (%s, %s, %s)", self._name, self._id, True)
   141  VALUES (%s, %s, %s)", self._name, self._id, True)
   142             self._dbh.commit()
   142             self._dbh.commit()
   143             dbc.close()
   143             dbc.close()
   144         else:
   144         else:
   145             raise VMMDE(_(u'The domain “%s” already exists.') % self._name,
   145             raise DomErr(_(u'The domain “%s” already exists.') % self._name,
   146                         DOMAIN_EXISTS)
   146                          DOMAIN_EXISTS)
   147 
   147 
   148     def delete(self, delUser=False, delAlias=False):
   148     def delete(self, delUser=False, delAlias=False):
   149         """Deletes the domain.
   149         """Deletes the domain.
   150 
   150 
   151         Keyword arguments:
   151         Keyword arguments:
   158             for t in ('alias','users','relocated','domain_name','domain_data'):
   158             for t in ('alias','users','relocated','domain_name','domain_data'):
   159                 dbc.execute("DELETE FROM %s WHERE gid = %d" % (t, self._id))
   159                 dbc.execute("DELETE FROM %s WHERE gid = %d" % (t, self._id))
   160             self._dbh.commit()
   160             self._dbh.commit()
   161             dbc.close()
   161             dbc.close()
   162         else:
   162         else:
   163             raise VMMDE(_(u"The domain “%s” doesn't exist.") % self._name,
   163             raise DomErr(_(u"The domain “%s” doesn't exist.") % self._name,
   164                         NO_SUCH_DOMAIN)
   164                          NO_SUCH_DOMAIN)
   165 
   165 
   166     def updateTransport(self, transport, force=False):
   166     def updateTransport(self, transport, force=False):
   167         """Sets a new transport for the domain.
   167         """Sets a new transport for the domain.
   168 
   168 
   169         Keyword arguments:
   169         Keyword arguments:
   184                         trsp.getID(), self._id)
   184                         trsp.getID(), self._id)
   185                 if dbc.rowcount > 0:
   185                 if dbc.rowcount > 0:
   186                     self._dbh.commit()
   186                     self._dbh.commit()
   187             dbc.close()
   187             dbc.close()
   188         else:
   188         else:
   189             raise VMMDE(_(u"The domain “%s” doesn't exist.") % self._name,
   189             raise DomErr(_(u"The domain “%s” doesn't exist.") % self._name,
   190                         NO_SUCH_DOMAIN)
   190                          NO_SUCH_DOMAIN)
   191 
   191 
   192     def getID(self):
   192     def getID(self):
   193         """Returns the ID of the domain."""
   193         """Returns the ID of the domain."""
   194         return self._id
   194         return self._id
   195 
   195 
   215         dbc = self._dbh.cursor()
   215         dbc = self._dbh.cursor()
   216         dbc.execute(sql)
   216         dbc.execute(sql)
   217         info = dbc.fetchone()
   217         info = dbc.fetchone()
   218         dbc.close()
   218         dbc.close()
   219         if info is None:
   219         if info is None:
   220             raise VMMDE(_(u"The domain “%s” doesn't exist.") % self._name,
   220             raise DomErr(_(u"The domain “%s” doesn't exist.") % self._name,
   221                         NO_SUCH_DOMAIN)
   221                          NO_SUCH_DOMAIN)
   222         else:
   222         else:
   223             keys = ['gid', 'domainname', 'transport', 'domaindir',
   223             keys = ['gid', 'domainname', 'transport', 'domaindir',
   224                     'aliasdomains', 'accounts', 'aliases', 'relocated']
   224                     'aliasdomains', 'accounts', 'aliases', 'relocated']
   225             return dict(zip(keys, info))
   225             return dict(zip(keys, info))
   226 
   226 
   313     return order, domdict
   313     return order, domdict
   314 
   314 
   315 def get_gid(dbh, domainname):
   315 def get_gid(dbh, domainname):
   316     """Returns the *GID* of the domain *domainname*.
   316     """Returns the *GID* of the domain *domainname*.
   317 
   317 
   318     Raises an `VMMDomainException` if the domain does not exist.
   318     Raises an `DomainError` if the domain does not exist.
   319     """
   319     """
   320     domainname = check_domainname(domainname)
   320     domainname = check_domainname(domainname)
   321     dbc = dbh.cursor()
   321     dbc = dbh.cursor()
   322     dbc.execute('SELECT gid FROM domain_name WHERE domainname=%s', domainname)
   322     dbc.execute('SELECT gid FROM domain_name WHERE domainname=%s', domainname)
   323     gid = dbc.fetchone()
   323     gid = dbc.fetchone()
   324     dbc.close()
   324     dbc.close()
   325     if gid:
   325     if gid:
   326         return gid[0]
   326         return gid[0]
   327     else:
   327     else:
   328         raise VMMDE(_(u"The domain “%s” doesn't exist.") % domainname,
   328         raise DomErr(_(u"The domain “%s” doesn't exist.") % domainname,
   329                     NO_SUCH_DOMAIN)
   329                      NO_SUCH_DOMAIN)