Fix transport_maps function for non-existent domains
The postfix_transport_maps function had a bug causing
2012-04-15 17:40:22 CEST LOG: statement: SELECT transport FROM postfix_transport_map('logcheck', 'domine.madduck.net');
2012-04-15 17:40:22 CEST ERROR: query returned no rows
when the domain was not in the database. This would make did be NULL and make
the query fail.
This patch moves the tid query until after a check for did. If the latter is
NULL, the function RETURNs (rather than fails).
# -*- coding: UTF-8 -*-# Copyright (c) 2008 - 2011, Pascal Volk# See COPYING for distribution information.""" VirtualMailManager.transport ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Virtual Mail Manager's Transport class to manage the transport for domains and accounts."""fromVirtualMailManager.pycompatimportany_=lambdamsg:msgclassTransport(object):"""A wrapper class that provides access to the transport table"""__slots__=('_tid','_transport','_dbh')def__init__(self,dbh,tid=None,transport=None):"""Creates a new Transport instance. Either tid or transport must be specified. When both arguments are given, tid will be used. Keyword arguments: dbh -- a pyPgSQL.PgSQL.connection tid -- the id of a transport (int/long) transport -- the value of the transport (str) """self._dbh=dbhself._tid=0assertany((tid,transport))iftid:assertnotisinstance(tid,bool)andisinstance(tid,(int,long))self._load_by_id(tid)else:assertisinstance(transport,basestring)self._transport=transportself._load_by_name()@propertydeftid(self):"""The transport's unique ID."""returnself._tid@propertydeftransport(self):"""The transport's value, ex: 'dovecot:'"""returnself._transportdef__eq__(self,other):ifisinstance(other,self.__class__):returnself._tid==other._tidreturnNotImplementeddef__ne__(self,other):ifisinstance(other,self.__class__):returnself._tid!=other._tidreturnNotImplementeddef__str__(self):returnself._transportdef_load_by_id(self,tid):"""load a transport by its id from the database"""dbc=self._dbh.cursor()dbc.execute('SELECT transport FROM transport WHERE tid = %s',(tid,))result=dbc.fetchone()dbc.close()ifnotresult:raiseValueError('Unknown transport id specified: %r'%tid)self._transport=result[0]self._tid=tiddef_load_by_name(self):"""Load a transport by its transport name from the database."""dbc=self._dbh.cursor()dbc.execute('SELECT tid FROM transport WHERE transport = %s',(self._transport,))result=dbc.fetchone()dbc.close()ifresult:self._tid=result[0]else:self._save()def_save(self):"""Save the new transport in the database."""dbc=self._dbh.cursor()dbc.execute("SELECT nextval('transport_id')")self._tid=dbc.fetchone()[0]dbc.execute('INSERT INTO transport (tid, transport) VALUES (%s, %s)',(self._tid,self._transport))self._dbh.commit()dbc.close()del_