VirtualMailManager/AliasDomain.py
changeset 571 a4aead244f75
parent 465 c0e1fb1b0145
parent 570 28230a8230bf
child 572 3238c58d01ae
equal deleted inserted replaced
465:c0e1fb1b0145 571:a4aead244f75
     1 # -*- coding: UTF-8 -*-
       
     2 # Copyright (c) 2008 - 2010, Pascal Volk
       
     3 # See COPYING for distribution information.
       
     4 
       
     5 """Virtual Mail Manager's AliasDomain class to manage alias domains."""
       
     6 
       
     7 from __main__ import ERR
       
     8 from Exceptions import VMMAliasDomainException as VADE
       
     9 import VirtualMailManager as VMM
       
    10 
       
    11 class AliasDomain(object):
       
    12     """Class to manage e-mail alias domains."""
       
    13     __slots__ = ('__gid', '__name', '_domain', '_dbh')
       
    14     def __init__(self, dbh, domainname, targetDomain=None):
       
    15         self._dbh = dbh
       
    16         self.__name = VMM.VirtualMailManager.chkDomainname(domainname)
       
    17         self.__gid = 0
       
    18         self._domain = targetDomain
       
    19         self._exists()
       
    20 
       
    21     def _exists(self):
       
    22         dbc = self._dbh.cursor()
       
    23         dbc.execute('SELECT gid, is_primary FROM domain_name WHERE domainname\
       
    24  = %s', self.__name)
       
    25         alias = dbc.fetchone()
       
    26         dbc.close()
       
    27         if alias is not None:
       
    28             self.__gid, primary = alias
       
    29             if primary:
       
    30                 raise VADE(_(u"The domain “%s” is a primary domain.") %
       
    31                         self.__name, ERR.ALIASDOMAIN_ISDOMAIN)
       
    32 
       
    33     def save(self):
       
    34         if self.__gid > 0:
       
    35             raise VADE(_(u'The alias domain “%s” already exists.') %self.__name,
       
    36                     ERR.ALIASDOMAIN_EXISTS)
       
    37         if self._domain is None:
       
    38             raise VADE(_(u'No destination domain specified for alias domain.'),
       
    39                     ERR.ALIASDOMAIN_NO_DOMDEST)
       
    40         if self._domain._id < 1:
       
    41             raise VADE (_(u"The target domain “%s” doesn't exist.") %
       
    42                     self._domain._name, ERR.NO_SUCH_DOMAIN)
       
    43         dbc = self._dbh.cursor()
       
    44         dbc.execute('INSERT INTO domain_name (domainname, gid, is_primary)\
       
    45  VALUES (%s, %s, FALSE)', self.__name, self._domain._id)
       
    46         self._dbh.commit()
       
    47         dbc.close()
       
    48 
       
    49     def info(self):
       
    50         if self.__gid > 0:
       
    51             dbc = self._dbh.cursor()
       
    52             dbc.execute('SELECT domainname FROM domain_name WHERE gid = %s\
       
    53  AND is_primary', self.__gid)
       
    54             domain = dbc.fetchone()
       
    55             dbc.close()
       
    56             if domain is not None:
       
    57                 return {'alias': self.__name, 'domain': domain[0]}
       
    58             else:# an almost unlikely case, isn't it?
       
    59                 raise VADE(
       
    60                     _(u'There is no primary domain for the alias domain “%s”.')\
       
    61                             % self.__name, ERR.NO_SUCH_DOMAIN)
       
    62         else:
       
    63             raise VADE(_(u"The alias domain “%s” doesn't exist.") %
       
    64                     self.__name, ERR.NO_SUCH_ALIASDOMAIN)
       
    65 
       
    66     def switch(self):
       
    67         if self._domain is None:
       
    68             raise VADE(_(u'No destination domain specified for alias domain.'),
       
    69                     ERR.ALIASDOMAIN_NO_DOMDEST)
       
    70         if self._domain._id < 1:
       
    71             raise VADE (_(u"The target domain “%s” doesn't exist.") %
       
    72                     self._domain._name, ERR.NO_SUCH_DOMAIN)
       
    73         if self.__gid < 1:
       
    74             raise VADE(_(u"The alias domain “%s” doesn't exist.") %
       
    75                     self.__name, ERR.NO_SUCH_ALIASDOMAIN)
       
    76         if self.__gid == self._domain._id:
       
    77             raise VADE(_(u"The alias domain “%(alias)s” is already assigned to\
       
    78  the domain “%(domain)s”.") %
       
    79                     {'alias': self.__name, 'domain': self._domain._name},
       
    80                     ERR.ALIASDOMAIN_EXISTS)
       
    81         dbc = self._dbh.cursor()
       
    82         dbc.execute('UPDATE domain_name SET gid = %s WHERE gid = %s\
       
    83  AND domainname = %s AND NOT is_primary',
       
    84                 self._domain._id, self.__gid, self.__name)
       
    85         self._dbh.commit()
       
    86         dbc.close()
       
    87 
       
    88     def delete(self):
       
    89         if self.__gid > 0:
       
    90             dbc = self._dbh.cursor()
       
    91             dbc.execute('DELETE FROM domain_name WHERE domainname = %s \
       
    92  AND NOT is_primary', self.__name)
       
    93             if dbc.rowcount > 0:
       
    94                 self._dbh.commit()
       
    95         else:
       
    96             raise VADE(
       
    97                   _(u"The alias domain “%s” doesn't exist.") % self.__name,
       
    98                   ERR.NO_SUCH_ALIASDOMAIN)
       
    99