VirtualMailManager/DomainAlias.py
changeset 55 15c873f94ba6
parent 54 1fc1f82c662f
child 56 9ae1b1b2ee5c
equal deleted inserted replaced
54:1fc1f82c662f 55:15c873f94ba6
     1 #!/usr/bin/env python
       
     2 # -*- coding: UTF-8 -*-
       
     3 # Copyright 2008 VEB IT
       
     4 # See COPYING for distribution information.
       
     5 # $Id$
       
     6 
       
     7 """Virtual Mail Manager's DomainAlias class to manage alias domains."""
       
     8 
       
     9 from constants.VERSION import VERSION
       
    10 
       
    11 __author__ = 'Pascal Volk <p.volk@veb-it.de>'
       
    12 __version__ = VERSION
       
    13 __revision__ = 'rev '+'$Rev$'.split()[1]
       
    14 __date__ = '$Date$'.split()[1]
       
    15 
       
    16 from Exceptions import VMMDomainAliasException as VDAE
       
    17 import constants.ERROR as ERR
       
    18 import VirtualMailManager as VMM
       
    19 
       
    20 class DomainAlias:
       
    21     """Class to manage e-mail alias domains."""
       
    22     def __init__(self, dbh, domainname, targetDomain=None):
       
    23         self._dbh = dbh
       
    24         self.__name = VMM.VirtualMailManager.chkDomainname(domainname)
       
    25         self.__gid = 0
       
    26         self._domain = targetDomain
       
    27         self._exists()
       
    28 
       
    29     def _exists(self):
       
    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)
       
    40 
       
    41     def save(self):
       
    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 
       
    57 
       
    58     def info(self):
       
    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)
       
    76     
       
    77     def delete(self):
       
    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