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