VirtualMailManager/AliasDomain.py
changeset 55 15c873f94ba6
parent 53 5b50eb306d37
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 AliasDomain 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 VMMAliasDomainException as VADE
       
    17 import constants.ERROR as ERR
       
    18 import VirtualMailManager as VMM
       
    19 
       
    20 class AliasDomain:
       
    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 VADE(_(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 VADE(_(u'The alias domain »%s« already exists.') %self.__name,
       
    44                 ERR.DOMAIN_ALIAS_EXISTS)
       
    45         if self._domain is None:
       
    46             raise VADE(_(u'No destination domain for alias domain denoted.'),
       
    47                     ERR.DOMAIN_ALIAS_NO_DOMDEST)
       
    48         if self._domain._id < 1:
       
    49             raise VADE (_(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 {'alias': self.__name, 'domain': domain[0]}
       
    67             else:# an almost unlikely case, isn't it?
       
    68                 raise VADE(
       
    69                     _(u'There is no primary domain for the alias domain »%s«.')\
       
    70                             % self.__name, ERR.NO_SUCH_DOMAIN)
       
    71         else:
       
    72             raise VADE(
       
    73                   _(u"The alias domain »%s« doesn't exist yet.") % self.__name,
       
    74                   ERR.NO_SUCH_DOMAIN_ALIAS)
       
    75     
       
    76     def delete(self):
       
    77         if self.__gid > 0:
       
    78             dbc = self._dbh.cursor()
       
    79             dbc.execute('DELETE FROM domain_name WHERE domainname = %s \
       
    80  AND NOT is_primary', self.__name)
       
    81             if dbc.rowcount > 0:
       
    82                 self._dbh.commit()
       
    83         else:
       
    84             raise VADE(
       
    85                   _(u"The alias domain »%s« doesn't exist yet.") % self.__name,
       
    86                   ERR.NO_SUCH_DOMAIN_ALIAS)
       
    87