VirtualMailManager/Account.py
branchv0.6.x
changeset 216 0c8c053b451c
parent 185 6e1ef32fbd82
child 225 a51809f7940b
equal deleted inserted replaced
215:33f727efa7c4 216:0c8c053b451c
     5 """Virtual Mail Manager's Account class to manage e-mail accounts."""
     5 """Virtual Mail Manager's Account class to manage e-mail accounts."""
     6 
     6 
     7 import VirtualMailManager.constants.ERROR as ERR
     7 import VirtualMailManager.constants.ERROR as ERR
     8 from VirtualMailManager.Domain import Domain
     8 from VirtualMailManager.Domain import Domain
     9 from VirtualMailManager.EmailAddress import EmailAddress
     9 from VirtualMailManager.EmailAddress import EmailAddress
    10 from VirtualMailManager.Exceptions import VMMAccountException as AccE
    10 from VirtualMailManager.errors import AccountError as AccE
    11 from VirtualMailManager.MailLocation import MailLocation
    11 from VirtualMailManager.MailLocation import MailLocation
    12 from VirtualMailManager.Transport import Transport
    12 from VirtualMailManager.Transport import Transport
    13 import VirtualMailManager as VMM
       
    14 
    13 
    15 class Account(object):
    14 class Account(object):
    16     """Class to manage e-mail accounts."""
    15     """Class to manage e-mail accounts."""
    17     __slots__ = ('_addr','_base','_gid','_mid','_passwd','_tid','_uid','_dbh')
    16     __slots__ = ('_addr','_base','_gid','_mid','_passwd','_tid','_uid','_dbh')
       
    17 
    18     def __init__(self, dbh, address, password=None):
    18     def __init__(self, dbh, address, password=None):
    19         self._dbh = dbh
    19         self._dbh = dbh
    20         self._base = None
    20         self._base = None
    21         if isinstance(address, EmailAddress):
    21         if isinstance(address, EmailAddress):
    22             self._addr = address
    22             self._addr = address
    27         self._mid = 0
    27         self._mid = 0
    28         self._tid = 0
    28         self._tid = 0
    29         self._passwd = password
    29         self._passwd = password
    30         self._setAddr()
    30         self._setAddr()
    31         self._exists()
    31         self._exists()
    32         if self._uid < 1 and VMM.VirtualMailManager.aliasExists(self._dbh,
    32         from VirtualMailManager.Handler import Handler
    33                 self._addr):
    33         if self._uid < 1 and Handler.aliasExists(self._dbh, self._addr):
    34             # TP: Hm, what quotation marks should be used?
    34             # TP: Hm, what quotation marks should be used?
    35             # If you are unsure have a look at:
    35             # If you are unsure have a look at:
    36             # http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
    36             # http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
    37             raise AccE(_(u"There is already an alias with the address “%s”.") %\
    37             raise AccE(_(u"There is already an alias with the address “%s”.") %
    38                     self._addr, ERR.ALIAS_EXISTS)
    38                        self._addr, ERR.ALIAS_EXISTS)
    39         if self._uid < 1 and VMM.VirtualMailManager.relocatedExists(self._dbh,
    39         if self._uid < 1 and Handler.relocatedExists(self._dbh, self._addr):
    40                 self._addr):
       
    41             raise AccE(
    40             raise AccE(
    42               _(u"There is already a relocated user with the address “%s”.") %\
    41               _(u"There is already a relocated user with the address “%s”.") %
    43                     self._addr, ERR.RELOCATED_EXISTS)
    42                        self._addr, ERR.RELOCATED_EXISTS)
    44 
    43 
    45     def _exists(self):
    44     def _exists(self):
    46         dbc = self._dbh.cursor()
    45         dbc = self._dbh.cursor()
    47         dbc.execute("SELECT uid, mid, tid FROM users \
    46         dbc.execute(
    48 WHERE gid=%s AND local_part=%s",
    47             "SELECT uid, mid, tid FROM users WHERE gid=%s AND local_part=%s",
    49                 self._gid, self._addr._localpart)
    48                     self._gid, self._addr._localpart)
    50         result = dbc.fetchone()
    49         result = dbc.fetchone()
    51         dbc.close()
    50         dbc.close()
    52         if result is not None:
    51         if result is not None:
    53             self._uid, self._mid, self._tid = result
    52             self._uid, self._mid, self._tid = result
    54             return True
    53             return True
    57 
    56 
    58     def _setAddr(self):
    57     def _setAddr(self):
    59         dom = Domain(self._dbh, self._addr._domainname)
    58         dom = Domain(self._dbh, self._addr._domainname)
    60         self._gid = dom.getID()
    59         self._gid = dom.getID()
    61         if self._gid == 0:
    60         if self._gid == 0:
    62             raise AccE(_(u"The domain “%s” doesn't exist.") %\
    61             raise AccE(_(u"The domain “%s” doesn't exist.") %
    63                     self._addr._domainname, ERR.NO_SUCH_DOMAIN)
    62                        self._addr._domainname, ERR.NO_SUCH_DOMAIN)
    64         self._base = dom.getDir()
    63         self._base = dom.getDir()
    65         self._tid = dom.getTransportID()
    64         self._tid = dom.getTransportID()
    66 
    65 
    67     def _setID(self):
    66     def _setID(self):
    68         dbc = self._dbh.cursor()
    67         dbc = self._dbh.cursor()