VirtualMailManager/transport.py
branchv0.6.x
changeset 449 6826acb9b0a7
parent 379 7518d927d443
child 568 14abdd04ddf5
equal deleted inserted replaced
448:45c917e6a386 449:6826acb9b0a7
     7 
     7 
     8     Virtual Mail Manager's Transport class to manage the transport for
     8     Virtual Mail Manager's Transport class to manage the transport for
     9     domains and accounts.
     9     domains and accounts.
    10 """
    10 """
    11 
    11 
    12 from VirtualMailManager.constants import UNKNOWN_TRANSPORT_ID
       
    13 from VirtualMailManager.errors import TransportError
       
    14 from VirtualMailManager.pycompat import any
    12 from VirtualMailManager.pycompat import any
    15 
    13 
    16 _ = lambda msg: msg
    14 _ = lambda msg: msg
    17 
    15 
    18 
    16 
    31         tid -- the id of a transport (int/long)
    29         tid -- the id of a transport (int/long)
    32         transport -- the value of the transport (str)
    30         transport -- the value of the transport (str)
    33 
    31 
    34         """
    32         """
    35         self._dbh = dbh
    33         self._dbh = dbh
       
    34         self._tid = 0
    36         assert any((tid, transport))
    35         assert any((tid, transport))
    37         if tid:
    36         if tid:
    38             assert not isinstance(tid, bool) and isinstance(tid, (int, long))
    37             assert not isinstance(tid, bool) and isinstance(tid, (int, long))
    39             self._tid = tid
    38             self._load_by_id(tid)
    40             self._load_by_id()
       
    41         else:
    39         else:
    42             assert isinstance(transport, basestring)
    40             assert isinstance(transport, basestring)
    43             self._transport = transport
    41             self._transport = transport
    44             self._load_by_name()
    42             self._load_by_name()
    45 
    43 
    64         return NotImplemented
    62         return NotImplemented
    65 
    63 
    66     def __str__(self):
    64     def __str__(self):
    67         return self._transport
    65         return self._transport
    68 
    66 
    69     def _load_by_id(self):
    67     def _load_by_id(self, tid):
    70         """load a transport by its id from the database"""
    68         """load a transport by its id from the database"""
    71         dbc = self._dbh.cursor()
    69         dbc = self._dbh.cursor()
    72         dbc.execute('SELECT transport FROM transport WHERE tid = %s',
    70         dbc.execute('SELECT transport FROM transport WHERE tid = %s', (tid,))
    73                     (self._tid,))
       
    74         result = dbc.fetchone()
    71         result = dbc.fetchone()
    75         dbc.close()
    72         dbc.close()
    76         if result:
    73         if not result:
    77             self._transport = result[0]
    74             raise ValueError('Unknown transport id specified: %r' % tid)
    78         else:
    75         self._transport = result[0]
    79             raise TransportError(_(u'Unknown transport id specified.'),
    76         self._tid = tid
    80                                  UNKNOWN_TRANSPORT_ID)
       
    81 
    77 
    82     def _load_by_name(self):
    78     def _load_by_name(self):
    83         """Load a transport by its transport name from the database."""
    79         """Load a transport by its transport name from the database."""
    84         dbc = self._dbh.cursor()
    80         dbc = self._dbh.cursor()
    85         dbc.execute('SELECT tid FROM transport WHERE transport = %s',
    81         dbc.execute('SELECT tid FROM transport WHERE transport = %s',