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