VirtualMailManager/Alias.py
changeset 48 0d5f58f8b8f5
parent 47 191d5a5adc4a
child 52 c152d7714802
equal deleted inserted replaced
47:191d5a5adc4a 48:0d5f58f8b8f5
    19 
    19 
    20 class Alias:
    20 class Alias:
    21     """Class to manage e-mail accounts."""
    21     """Class to manage e-mail accounts."""
    22     def __init__(self, dbh, address, destination=None):
    22     def __init__(self, dbh, address, destination=None):
    23         if address == destination:
    23         if address == destination:
    24             raise VMMAliasException((
    24             raise VMMAliasException(
    25                 _('Address and destination are identical.'),
    25                 _(u'Address and destination are identical.'),
    26                 ERR.ALIAS_ADDR_DEST_IDENTICAL))
    26                 ERR.ALIAS_ADDR_DEST_IDENTICAL)
    27         self._dbh = dbh
    27         self._dbh = dbh
    28         self._addr = address
    28         self._addr = address
    29         self._dest = destination
    29         self._dest = destination
    30         self._localpart = None
    30         self._localpart = None
    31         self._gid = 0
    31         self._gid = 0
    33         self._setAddr()
    33         self._setAddr()
    34         if not self._dest is None:
    34         if not self._dest is None:
    35             self._exists()
    35             self._exists()
    36         if self._isAccount():
    36         if self._isAccount():
    37             raise VMMAliasException(
    37             raise VMMAliasException(
    38             (_(u"There is already an account with address '%s'") % self._addr,
    38             _(u"There is already an account with address '%s'") % self._addr,
    39                 ERR.ACCOUNT_EXISTS))
    39                 ERR.ACCOUNT_EXISTS)
    40 
    40 
    41     def _exists(self):
    41     def _exists(self):
    42         dbc = self._dbh.cursor()
    42         dbc = self._dbh.cursor()
    43         dbc.execute("SELECT gid FROM alias WHERE gid=%s AND address=%s\
    43         dbc.execute("SELECT gid FROM alias WHERE gid=%s AND address=%s\
    44  AND destination=%s", self._gid, self._localpart, self._dest)
    44  AND destination=%s", self._gid, self._localpart, self._dest)
    63     def _setAddr(self):
    63     def _setAddr(self):
    64         self._localpart, d = self._addr.split('@')
    64         self._localpart, d = self._addr.split('@')
    65         dom = Domain(self._dbh, d)
    65         dom = Domain(self._dbh, d)
    66         self._gid = dom.getID()
    66         self._gid = dom.getID()
    67         if self._gid == 0:
    67         if self._gid == 0:
    68             raise VMMAliasException((_(u"Domain '%s' doesn't exist.") % d,
    68             raise VMMAliasException(_(u"Domain '%s' doesn't exist.") % d,
    69                 ERR.NO_SUCH_DOMAIN))
    69                 ERR.NO_SUCH_DOMAIN)
    70 
    70 
    71     def save(self):
    71     def save(self):
    72         if self._dest is None:
    72         if self._dest is None:
    73            raise VMMAliasException((
    73            raise VMMAliasException(
    74                _('No destination address for alias denoted.'),
    74                _('No destination address for alias denoted.'),
    75                ERR.ALIAS_MISSING_DEST))
    75                ERR.ALIAS_MISSING_DEST)
    76         if self._isNew:
    76         if self._isNew:
    77             dbc = self._dbh.cursor()
    77             dbc = self._dbh.cursor()
    78             dbc.execute("INSERT INTO alias (gid, address, destination) VALUES\
    78             dbc.execute("INSERT INTO alias (gid, address, destination) VALUES\
    79  (%s, %s, %s)", self._gid, self._localpart, self._dest)
    79  (%s, %s, %s)", self._gid, self._localpart, self._dest)
    80             self._dbh.commit()
    80             self._dbh.commit()
    81             dbc.close()
    81             dbc.close()
    82         else:
    82         else:
    83             raise VMMAliasException((_("Alias already exists."),
    83             raise VMMAliasException(_("Alias already exists."),
    84                 ERR.ALIAS_EXISTS))
    84                 ERR.ALIAS_EXISTS)
    85 
    85 
    86     def getInfo(self):
    86     def getInfo(self):
    87         dbc = self._dbh.cursor()
    87         dbc = self._dbh.cursor()
    88         dbc.execute('SELECT destination FROM alias WHERE gid=%s AND address=%s',
    88         dbc.execute('SELECT destination FROM alias WHERE gid=%s AND address=%s',
    89                 self._gid, self._localpart)
    89                 self._gid, self._localpart)
    93             targets = []
    93             targets = []
    94             for destination in destinations:
    94             for destination in destinations:
    95                 targets.append(destination[0])
    95                 targets.append(destination[0])
    96             return targets
    96             return targets
    97         else:
    97         else:
    98             raise VMMAliasException((_("Alias doesn't exists"),
    98             raise VMMAliasException(_("Alias doesn't exists"),
    99                 ERR.NO_SUCH_ALIAS))
    99                 ERR.NO_SUCH_ALIAS)
   100 
   100 
   101     def delete(self):
   101     def delete(self):
   102         dbc = self._dbh.cursor()
   102         dbc = self._dbh.cursor()
   103         if self._dest is None:
   103         if self._dest is None:
   104             dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s",
   104             dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s",
   109         rowcount = dbc.rowcount
   109         rowcount = dbc.rowcount
   110         dbc.close()
   110         dbc.close()
   111         if rowcount > 0:
   111         if rowcount > 0:
   112             self._dbh.commit()
   112             self._dbh.commit()
   113         else:
   113         else:
   114             raise VMMAliasException((_("Alias doesn't exists"),
   114             raise VMMAliasException(_("Alias doesn't exists"),
   115                 ERR.NO_SUCH_ALIAS))
   115                 ERR.NO_SUCH_ALIAS)
   116 
   116