VirtualMailManager/AliasDomain.py
branchv0.6.x
changeset 216 0c8c053b451c
parent 199 0684790fff7c
child 236 084331dd1e4c
equal deleted inserted replaced
215:33f727efa7c4 216:0c8c053b451c
     4 
     4 
     5 """Virtual Mail Manager's AliasDomain class to manage alias domains."""
     5 """Virtual Mail Manager's AliasDomain class to manage alias domains."""
     6 
     6 
     7 import VirtualMailManager.constants.ERROR as ERR
     7 import VirtualMailManager.constants.ERROR as ERR
     8 from VirtualMailManager import check_domainname
     8 from VirtualMailManager import check_domainname
     9 from VirtualMailManager.Exceptions import VMMAliasDomainException as VADE
     9 from VirtualMailManager.errors import AliasDomainError as ADE
    10 
    10 
    11 class AliasDomain(object):
    11 class AliasDomain(object):
    12     """Class to manage e-mail alias domains."""
    12     """Class to manage e-mail alias domains."""
    13     __slots__ = ('__gid', '__name', '_domain', '_dbh')
    13     __slots__ = ('__gid', '__name', '_domain', '_dbh')
    14     def __init__(self, dbh, domainname, targetDomain=None):
    14     def __init__(self, dbh, domainname, targetDomain=None):
    25         alias = dbc.fetchone()
    25         alias = dbc.fetchone()
    26         dbc.close()
    26         dbc.close()
    27         if alias is not None:
    27         if alias is not None:
    28             self.__gid, primary = alias
    28             self.__gid, primary = alias
    29             if primary:
    29             if primary:
    30                 raise VADE(_(u"The domain “%s” is a primary domain.") %
    30                 raise ADE(_(u"The domain “%s” is a primary domain.") %
    31                         self.__name, ERR.ALIASDOMAIN_ISDOMAIN)
    31                         self.__name, ERR.ALIASDOMAIN_ISDOMAIN)
    32 
    32 
    33     def save(self):
    33     def save(self):
    34         if self.__gid > 0:
    34         if self.__gid > 0:
    35             raise VADE(_(u'The alias domain “%s” already exists.') %self.__name,
    35             raise ADE(_(u'The alias domain “%s” already exists.') %self.__name,
    36                     ERR.ALIASDOMAIN_EXISTS)
    36                     ERR.ALIASDOMAIN_EXISTS)
    37         if self._domain is None:
    37         if self._domain is None:
    38             raise VADE(_(u'No destination domain specified for alias domain.'),
    38             raise ADE(_(u'No destination domain specified for alias domain.'),
    39                     ERR.ALIASDOMAIN_NO_DOMDEST)
    39                     ERR.ALIASDOMAIN_NO_DOMDEST)
    40         if self._domain._id < 1:
    40         if self._domain._id < 1:
    41             raise VADE (_(u"The target domain “%s” doesn't exist.") %
    41             raise ADE (_(u"The target domain “%s” doesn't exist.") %
    42                     self._domain._name, ERR.NO_SUCH_DOMAIN)
    42                     self._domain._name, ERR.NO_SUCH_DOMAIN)
    43         dbc = self._dbh.cursor()
    43         dbc = self._dbh.cursor()
    44         dbc.execute('INSERT INTO domain_name (domainname, gid, is_primary)\
    44         dbc.execute('INSERT INTO domain_name (domainname, gid, is_primary)\
    45  VALUES (%s, %s, FALSE)', self.__name, self._domain._id)
    45  VALUES (%s, %s, FALSE)', self.__name, self._domain._id)
    46         self._dbh.commit()
    46         self._dbh.commit()
    54             domain = dbc.fetchone()
    54             domain = dbc.fetchone()
    55             dbc.close()
    55             dbc.close()
    56             if domain is not None:
    56             if domain is not None:
    57                 return {'alias': self.__name, 'domain': domain[0]}
    57                 return {'alias': self.__name, 'domain': domain[0]}
    58             else:# an almost unlikely case, isn't it?
    58             else:# an almost unlikely case, isn't it?
    59                 raise VADE(
    59                 raise ADE(
    60                     _(u'There is no primary domain for the alias domain “%s”.')\
    60                     _(u'There is no primary domain for the alias domain “%s”.')\
    61                             % self.__name, ERR.NO_SUCH_DOMAIN)
    61                             % self.__name, ERR.NO_SUCH_DOMAIN)
    62         else:
    62         else:
    63             raise VADE(_(u"The alias domain “%s” doesn't exist.") %
    63             raise ADE(_(u"The alias domain “%s” doesn't exist.") % self.__name,
    64                     self.__name, ERR.NO_SUCH_ALIASDOMAIN)
    64                         ERR.NO_SUCH_ALIASDOMAIN)
    65 
    65 
    66     def switch(self):
    66     def switch(self):
    67         if self._domain is None:
    67         if self._domain is None:
    68             raise VADE(_(u'No destination domain specified for alias domain.'),
    68             raise ADE(_(u'No destination domain specified for alias domain.'),
    69                     ERR.ALIASDOMAIN_NO_DOMDEST)
    69                     ERR.ALIASDOMAIN_NO_DOMDEST)
    70         if self._domain._id < 1:
    70         if self._domain._id < 1:
    71             raise VADE (_(u"The target domain “%s” doesn't exist.") %
    71             raise ADE (_(u"The target domain “%s” doesn't exist.") %
    72                     self._domain._name, ERR.NO_SUCH_DOMAIN)
    72                     self._domain._name, ERR.NO_SUCH_DOMAIN)
    73         if self.__gid < 1:
    73         if self.__gid < 1:
    74             raise VADE(_(u"The alias domain “%s” doesn't exist.") %
    74             raise ADE(_(u"The alias domain “%s” doesn't exist.") % self.__name,
    75                     self.__name, ERR.NO_SUCH_ALIASDOMAIN)
    75                         ERR.NO_SUCH_ALIASDOMAIN)
    76         if self.__gid == self._domain._id:
    76         if self.__gid == self._domain._id:
    77             raise VADE(_(u"The alias domain “%(alias)s” is already assigned to\
    77             raise ADE(_(u"The alias domain “%(alias)s” is already assigned to\
    78  the domain “%(domain)s”.") %
    78  the domain “%(domain)s”.") %
    79                     {'alias': self.__name, 'domain': self._domain._name},
    79                     {'alias': self.__name, 'domain': self._domain._name},
    80                     ERR.ALIASDOMAIN_EXISTS)
    80                     ERR.ALIASDOMAIN_EXISTS)
    81         dbc = self._dbh.cursor()
    81         dbc = self._dbh.cursor()
    82         dbc.execute('UPDATE domain_name SET gid = %s WHERE gid = %s\
    82         dbc.execute('UPDATE domain_name SET gid = %s WHERE gid = %s\
    91             dbc.execute('DELETE FROM domain_name WHERE domainname = %s \
    91             dbc.execute('DELETE FROM domain_name WHERE domainname = %s \
    92  AND NOT is_primary', self.__name)
    92  AND NOT is_primary', self.__name)
    93             if dbc.rowcount > 0:
    93             if dbc.rowcount > 0:
    94                 self._dbh.commit()
    94                 self._dbh.commit()
    95         else:
    95         else:
    96             raise VADE(
    96             raise ADE(_(u"The alias domain “%s” doesn't exist.") % self.__name,
    97                   _(u"The alias domain “%s” doesn't exist.") % self.__name,
    97                         ERR.NO_SUCH_ALIASDOMAIN)
    98                   ERR.NO_SUCH_ALIASDOMAIN)
       
    99 
    98