VirtualMailManager/AliasDomain.py
branchv0.6.x
changeset 243 30aaf2bc079c
parent 236 084331dd1e4c
child 254 8aecc83a0d32
equal deleted inserted replaced
242:496099847480 243:30aaf2bc079c
     1 # -*- coding: UTF-8 -*-
     1 # -*- coding: UTF-8 -*-
     2 # Copyright (c) 2008 - 2010, Pascal Volk
     2 # Copyright (c) 2008 - 2010, Pascal Volk
     3 # See COPYING for distribution information.
     3 # See COPYING for distribution information.
     4 
     4 
     5 """Virtual Mail Manager's AliasDomain class to manage alias domains."""
     5 """
       
     6     VirtualMailManager.AliasDomain
     6 
     7 
     7 import VirtualMailManager.constants.ERROR as ERR
     8     Virtual Mail Manager's AliasDomain class to manage alias domains.
       
     9 """
       
    10 
     8 from VirtualMailManager import check_domainname
    11 from VirtualMailManager import check_domainname
     9 from VirtualMailManager.errors import AliasDomainError as ADE
    12 from VirtualMailManager.Domain import Domain
       
    13 from VirtualMailManager.constants.ERROR import \
       
    14      ALIASDOMAIN_EXISTS, ALIASDOMAIN_ISDOMAIN, ALIASDOMAIN_NO_DOMDEST, \
       
    15      NO_SUCH_ALIASDOMAIN, NO_SUCH_DOMAIN
       
    16 from VirtualMailManager.errors import AliasDomainError as ADErr
       
    17 
       
    18 
       
    19 _ = lambda msg: msg
       
    20 
    10 
    21 
    11 class AliasDomain(object):
    22 class AliasDomain(object):
    12     """Class to manage e-mail alias domains."""
    23     """Class to manage e-mail alias domains."""
    13     __slots__ = ('__gid', '__name', '_domain', '_dbh')
    24     __slots__ = ('_gid', '_name', '_domain', '_dbh')
    14     def __init__(self, dbh, domainname, targetDomain=None):
    25 
       
    26     def __init__(self, dbh, domainname):
       
    27         """Creates a new AliasDomain instance.
       
    28 
       
    29         Arguments:
       
    30 
       
    31         `dbh` : pyPgSQL.PgSQL.Connection
       
    32           a database connection for the database access
       
    33         `domainname` : basestring
       
    34           the name of the AliasDomain"""
    15         self._dbh = dbh
    35         self._dbh = dbh
    16         self.__name = check_domainname(domainname)
    36         self._name = check_domainname(domainname)
    17         self.__gid = 0
    37         self._gid = 0
    18         self._domain = targetDomain
    38         self._domain = None
    19         self._exists()
    39         self._load()
    20 
    40 
    21     def _exists(self):
    41     def _load(self):
       
    42         """Loads the AliasDomain's GID from the database and checks if the
       
    43         domain name is marked as primary."""
    22         dbc = self._dbh.cursor()
    44         dbc = self._dbh.cursor()
    23         dbc.execute('SELECT gid, is_primary FROM domain_name WHERE domainname\
    45         dbc.execute(
    24  = %s', self.__name)
    46             'SELECT gid, is_primary FROM domain_name WHERE domainname = %s',
    25         alias = dbc.fetchone()
    47                     self._name)
       
    48         result = dbc.fetchone()
    26         dbc.close()
    49         dbc.close()
    27         if alias is not None:
    50         if result:
    28             self.__gid, primary = alias
    51             if result[1]:
    29             if primary:
    52                 raise ADErr(_(u"The domain '%s' is a primary domain.") %
    30                 raise ADE(_(u"The domain “%s” is a primary domain.") %
    53                             self._name, ALIASDOMAIN_ISDOMAIN)
    31                         self.__name, ERR.ALIASDOMAIN_ISDOMAIN)
    54             self._gid = result[0]
       
    55 
       
    56     def set_destination(self, dest_domain):
       
    57         """Set the destination of a new AliasDomain or updates the
       
    58         destination of an existing AliasDomain.
       
    59 
       
    60         Argument:
       
    61 
       
    62         `dest_domain` : VirtualMailManager.Domain.Domain
       
    63           the AliasDomain's destination domain
       
    64         """
       
    65         assert isinstance(dest_domain, Domain)
       
    66         self._domain = dest_domain
    32 
    67 
    33     def save(self):
    68     def save(self):
    34         if self.__gid > 0:
    69         """Stores information about the new AliasDomain in the database."""
    35             raise ADE(_(u'The alias domain “%s” already exists.') %self.__name,
    70         if self._gid > 0:
    36                     ERR.ALIASDOMAIN_EXISTS)
    71             raise ADErr(_(u"The alias domain '%s' already exists.") %
    37         if self._domain is None:
    72                         self._name, ALIASDOMAIN_EXISTS)
    38             raise ADE(_(u'No destination domain specified for alias domain.'),
    73         if not self._domain:
    39                     ERR.ALIASDOMAIN_NO_DOMDEST)
    74             raise ADErr(_(u'No destination domain set for the alias domain.'),
       
    75                         ALIASDOMAIN_NO_DOMDEST)
    40         if self._domain.gid < 1:
    76         if self._domain.gid < 1:
    41             raise ADE (_(u"The target domain “%s” doesn't exist.") %
    77             raise ADErr(_(u"The target domain '%s' doesn't exist.") %
    42                     self._domain.name, ERR.NO_SUCH_DOMAIN)
    78                         self._domain.name, NO_SUCH_DOMAIN)
    43         dbc = self._dbh.cursor()
    79         dbc = self._dbh.cursor()
    44         dbc.execute('INSERT INTO domain_name (domainname, gid, is_primary)\
    80         dbc.execute('INSERT INTO domain_name VALUES (%s, %s, FALSE)',
    45  VALUES (%s, %s, FALSE)', self.__name, self._domain.gid)
    81                     self._name, self._domain.gid)
    46         self._dbh.commit()
    82         self._dbh.commit()
    47         dbc.close()
    83         dbc.close()
    48 
    84 
    49     def info(self):
    85     def info(self):
    50         if self.__gid > 0:
    86         """Returns a dict (keys: "alias" and "domain") with the names of the
    51             dbc = self._dbh.cursor()
    87         AliasDomain and its primary domain."""
    52             dbc.execute('SELECT domainname FROM domain_name WHERE gid = %s\
    88         if self._gid < 1:
    53  AND is_primary', self.__gid)
    89             raise ADErr(_(u"The alias domain '%s' doesn't exist.") %
    54             domain = dbc.fetchone()
    90                         self._name, NO_SUCH_ALIASDOMAIN)
    55             dbc.close()
    91         dbc = self._dbh.cursor()
    56             if domain is not None:
    92         dbc.execute(
    57                 return {'alias': self.__name, 'domain': domain[0]}
    93             'SELECT domainname FROM domain_name WHERE gid = %s AND is_primary',
    58             else:# an almost unlikely case, isn't it?
    94                     self._gid)
    59                 raise ADE(
    95         domain = dbc.fetchone()
    60                     _(u'There is no primary domain for the alias domain “%s”.')\
    96         dbc.close()
    61                             % self.__name, ERR.NO_SUCH_DOMAIN)
    97         if domain:
    62         else:
    98             return {'alias': self._name, 'domain': domain[0]}
    63             raise ADE(_(u"The alias domain “%s” doesn't exist.") % self.__name,
    99         else:  # an almost unlikely case, isn't it?
    64                         ERR.NO_SUCH_ALIASDOMAIN)
   100             raise ADErr(
       
   101                    _(u"There is no primary domain for the alias domain '%s'.")\
       
   102                         % self._name, NO_SUCH_DOMAIN)
    65 
   103 
    66     def switch(self):
   104     def switch(self):
    67         if self._domain is None:
   105         """Switch the destination of the AliasDomain to the new destination,
    68             raise ADE(_(u'No destination domain specified for alias domain.'),
   106         set with the method `set_destination()`.
    69                     ERR.ALIASDOMAIN_NO_DOMDEST)
   107         """
       
   108         if not self._domain:
       
   109             raise ADErr(_(u'No destination domain set for the alias domain.'),
       
   110                         ALIASDOMAIN_NO_DOMDEST)
    70         if self._domain.gid < 1:
   111         if self._domain.gid < 1:
    71             raise ADE (_(u"The target domain “%s” doesn't exist.") %
   112             raise ADErr(_(u"The target domain '%s' doesn't exist.") %
    72                     self._domain.name, ERR.NO_SUCH_DOMAIN)
   113                         self._domain.name, NO_SUCH_DOMAIN)
    73         if self.__gid < 1:
   114         if self._gid < 1:
    74             raise ADE(_(u"The alias domain “%s” doesn't exist.") % self.__name,
   115             raise ADErr(_(u"The alias domain '%s' doesn't exist.") %
    75                         ERR.NO_SUCH_ALIASDOMAIN)
   116                         self._name, NO_SUCH_ALIASDOMAIN)
    76         if self.__gid == self._domain.gid:
   117         if self._gid == self._domain.gid:
    77             raise ADE(_(u"The alias domain “%(alias)s” is already assigned to\
   118             raise ADErr(_(u"The alias domain '%(alias)s' is already assigned\
    78  the domain “%(domain)s”.") %
   119  to the domain '%(domain)s'.") %
    79                     {'alias': self.__name, 'domain': self._domain.name},
   120                         {'alias': self._name, 'domain': self._domain.name},
    80                     ERR.ALIASDOMAIN_EXISTS)
   121                         ALIASDOMAIN_EXISTS)
    81         dbc = self._dbh.cursor()
   122         dbc = self._dbh.cursor()
    82         dbc.execute('UPDATE domain_name SET gid = %s WHERE gid = %s\
   123         dbc.execute('UPDATE domain_name SET gid = %s WHERE gid = %s\
    83  AND domainname = %s AND NOT is_primary',
   124  AND domainname = %s AND NOT is_primary',
    84                 self._domain.gid, self.__gid, self.__name)
   125                     self._domain.gid, self._gid, self._name)
    85         self._dbh.commit()
   126         self._dbh.commit()
    86         dbc.close()
   127         dbc.close()
    87 
   128 
    88     def delete(self):
   129     def delete(self):
    89         if self.__gid > 0:
   130         """Delete the AliasDomain's record form the database.
    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 ADE(_(u"The alias domain “%s” doesn't exist.") % self.__name,
       
    97                         ERR.NO_SUCH_ALIASDOMAIN)
       
    98 
   131 
       
   132         Raises an AliasDomainError if the AliasDomain doesn't exist.
       
   133         """
       
   134         if self._gid < 1:
       
   135             raise ADErr(_(u"The alias domain '%s' doesn't exist.") %
       
   136                         self._name, NO_SUCH_ALIASDOMAIN)
       
   137         dbc = self._dbh.cursor()
       
   138         dbc.execute(
       
   139             'DELETE FROM domain_name WHERE domainname = %s AND NOT is_primary',
       
   140                     self._name)
       
   141         if dbc.rowcount > 0:
       
   142             self._dbh.commit()
       
   143 
       
   144 
       
   145 del _