VirtualMailManager/DomainAlias.py
changeset 53 5b50eb306d37
parent 48 0d5f58f8b8f5
equal deleted inserted replaced
52:c152d7714802 53:5b50eb306d37
    13 __revision__ = 'rev '+'$Rev$'.split()[1]
    13 __revision__ = 'rev '+'$Rev$'.split()[1]
    14 __date__ = '$Date$'.split()[1]
    14 __date__ = '$Date$'.split()[1]
    15 
    15 
    16 from Exceptions import VMMDomainAliasException as VDAE
    16 from Exceptions import VMMDomainAliasException as VDAE
    17 import constants.ERROR as ERR
    17 import constants.ERROR as ERR
       
    18 import VirtualMailManager as VMM
    18 
    19 
    19 class DomainAlias:
    20 class DomainAlias:
    20     """Class to manage e-mail alias domains."""
    21     """Class to manage e-mail alias domains."""
    21     def __init__(self, dbh, domainname, targetDomain):
    22     def __init__(self, dbh, domainname, targetDomain=None):
    22         self._dbh = dbh
    23         self._dbh = dbh
       
    24         self.__name = VMM.VirtualMailManager.chkDomainname(domainname)
       
    25         self.__gid = 0
       
    26         self._domain = targetDomain
       
    27         self._exists()
    23 
    28 
    24     def _exists(self):
    29     def _exists(self):
    25         pass
    30         dbc = self._dbh.cursor()
       
    31         dbc.execute('SELECT gid, is_primary FROM domain_name WHERE domainname\
       
    32  = %s', self.__name)
       
    33         alias = dbc.fetchone()
       
    34         dbc.close()
       
    35         if alias is not None:
       
    36             self.__gid, primary = alias
       
    37             if primary:
       
    38                 raise VDAE(_(u"The domain »%s« is a primary domain.") %
       
    39                         self.__name, ERR.DOMAIN_ALIAS_ISDOMAIN)
    26 
    40 
    27     def save(self):
    41     def save(self):
    28         pass
    42         if self.__gid > 0:
       
    43             raise VDAE(_(u'The domain alias »%s« already exists.') %self.__name,
       
    44                 ERR.DOMAIN_ALIAS_EXISTS)
       
    45         if self._domain is None:
       
    46             raise VDAE(_(u'No destination domain for alias domain denoted.'),
       
    47                     ERR.DOMAIN_ALIAS_NO_DOMDEST)
       
    48         if self._domain._id < 1:
       
    49             raise VDAE (_(u"The target domain »%s« doesn't exist yet.") %
       
    50                     self._domain._name, ERR.NO_SUCH_DOMAIN)
       
    51         dbc = self._dbh.cursor()
       
    52         dbc.execute('INSERT INTO domain_name (domainname, gid, is_primary)\
       
    53  VALUES (%s, %s, FALSE)', self.__name, self._domain._id)
       
    54         self._dbh.commit()
       
    55         dbc.close()
       
    56 
    29 
    57 
    30     def info(self):
    58     def info(self):
    31         pass
    59         if self.__gid > 0:
       
    60             dbc = self._dbh.cursor()
       
    61             dbc.execute('SELECT domainname FROM domain_name WHERE gid = %s\
       
    62  AND is_primary', self.__gid)
       
    63             domain = dbc.fetchone()
       
    64             dbc.close()
       
    65             if domain is not None:
       
    66                 return _(u"The domain alias »%(alias)s« belongs to »%(dom)s«.")\
       
    67                         % {'alias': self.__name, 'dom': domain[0]}
       
    68             else:# an almost unlikely case, isn't it?
       
    69                 raise VDAE(
       
    70                     _(u'There is no primary domain for the domain alias »%s«.')\
       
    71                             % self.__name, ERR.NO_SUCH_DOMAIN)
       
    72         else:
       
    73             raise VDAE(
       
    74                   _(u"The domain alias »%s« doesn't exist yet.") % self.__name,
       
    75                   ERR.NO_SUCH_DOMAIN_ALIAS)
    32     
    76     
    33     def delete(self):
    77     def delete(self):
    34         pass
    78         if self.__gid > 0:
       
    79             dbc = self._dbh.cursor()
       
    80             dbc.execute('DELETE FROM domain_name WHERE domainname = %s \
       
    81  AND NOT is_primary', self.__name)
       
    82             if dbc.rowcount > 0:
       
    83                 self._dbh.commit()
       
    84         else:
       
    85             raise VDAE(
       
    86                   _(u"The domain alias »%s« doesn't exist yet.") % self.__name,
       
    87                   ERR.NO_SUCH_DOMAIN_ALIAS)
       
    88