VirtualMailManager/Relocated.py
changeset 76 14c0a092d7d2
child 82 6c85915f3815
equal deleted inserted replaced
75:af813ede1e19 76:14c0a092d7d2
       
     1 # -*- coding: UTF-8 -*-
       
     2 # Copyright 2008 VEB IT
       
     3 # See COPYING for distribution information.
       
     4 # $Id$
       
     5 
       
     6 """Virtual Mail Manager's Relocated class to manage relocated users."""
       
     7 
       
     8 from constants.VERSION import VERSION
       
     9 
       
    10 __author__ = 'Pascal Volk <p.volk@veb-it.de>'
       
    11 __version__ = VERSION
       
    12 __revision__ = 'rev '+'$Rev$'.split()[1]
       
    13 __date__ = '$Date$'.split()[1]
       
    14 
       
    15 from Exceptions import VMMRelocatedException as VMMRE
       
    16 from Domain import Domain
       
    17 from EmailAddress import EmailAddress
       
    18 import constants.ERROR as ERR
       
    19 import VirtualMailManager as VMM
       
    20 
       
    21 class Relocated:
       
    22     """Class to manage e-mail addresses of relocated users."""
       
    23     def __init__(self, dbh, address, destination=None):
       
    24         if isinstance(address, EmailAddress):
       
    25             self._addr = address
       
    26         else:
       
    27             raise TypeError("Argument 'address' is not an EmailAddress")
       
    28         if destination is None:
       
    29             self._dest = None
       
    30         elif isinstance(destination, EmailAddress):
       
    31             self._dest = destination
       
    32         else:
       
    33             raise TypeError("Argument 'destination' is not an EmailAddress")
       
    34         if address == destination:
       
    35             raise VMMRE(_(u"Address and destination are identical."),
       
    36                 ERR.RELOCATED_ADDR_DEST_IDENTICAL)
       
    37         self._dbh = dbh
       
    38         self._gid = 0
       
    39         self._isNew = False
       
    40         self._setAddr()
       
    41         self._exists()
       
    42         if VMM.VirtualMailManager.accountExists(self._dbh, self._addr):
       
    43             raise VMMRE(_(u"There is already an account with address »%s«.") %\
       
    44                     self._addr, ERR.ACCOUNT_EXISTS)
       
    45         if VMM.VirtualMailManager.aliasExists(self._dbh, self._addr):
       
    46             raise VMMRE(
       
    47                     _(u"There is already an alias with the address »%s«.") %\
       
    48                     self._addr, ERR.ALIAS_EXISTS)
       
    49 
       
    50     def _exists(self):
       
    51         dbc = self._dbh.cursor()
       
    52         dbc.execute("SELECT gid FROM relocated WHERE gid = %s AND address = %s",
       
    53                 self._gid, self._addr._localpart)
       
    54         gid = dbc.fetchone()
       
    55         dbc.close()
       
    56         if gid is None:
       
    57             self._isNew = True
       
    58 
       
    59     def _setAddr(self):
       
    60         dom = Domain(self._dbh, self._addr._domainname)
       
    61         self._gid = dom.getID()
       
    62         if self._gid == 0:
       
    63             raise VMMRE(_(u"The domain »%s« doesn't exist yet.") %\
       
    64                     self._addr._domainname, ERR.NO_SUCH_DOMAIN)
       
    65 
       
    66     def save(self):
       
    67         if self._dest is None:
       
    68            raise VMMRE(_(u"No destination address for relocated user denoted."),
       
    69                ERR.RELOCATED_MISSING_DEST)
       
    70         if self._isNew:
       
    71             dbc = self._dbh.cursor()
       
    72             dbc.execute("INSERT INTO relocated VALUES (%s, %s, %s)",
       
    73                     self._gid, self._addr._localpart, str(self._dest))
       
    74             self._dbh.commit()
       
    75             dbc.close()
       
    76         else:
       
    77             raise VMMRE(
       
    78                     _(u"The relocated user »%s« already exists.") % self._addr,
       
    79                     ERR.RELOCATED_EXISTS)
       
    80 
       
    81     def getInfo(self):
       
    82         dbc = self._dbh.cursor()
       
    83         dbc.execute('SELECT destination FROM relocated WHERE gid=%s\
       
    84  AND address=%s',
       
    85                 self._gid, self._addr._localpart)
       
    86         destination = dbc.fetchone()
       
    87         dbc.close()
       
    88         if destination is not None:
       
    89             return destination[0]
       
    90         else:
       
    91             raise VMMRE(
       
    92                     _(u"The relocated user »%s« doesn't exists.") % self._addr,
       
    93                     ERR.NO_SUCH_RELOCATED)
       
    94 
       
    95     def delete(self):
       
    96         dbc = self._dbh.cursor()
       
    97         dbc.execute("DELETE FROM relocated WHERE gid = %s AND address = %s",
       
    98                 self._gid, self._addr._localpart)
       
    99         rowcount = dbc.rowcount
       
   100         dbc.close()
       
   101         if rowcount > 0:
       
   102             self._dbh.commit()
       
   103         else:
       
   104             raise VMMRE(
       
   105                     _(u"The relocated user »%s« doesn't exists.") % self._addr,
       
   106                     ERR.NO_SUCH_RELOCATED)
       
   107