VirtualMailManager/handler.py
branchv0.6.x
changeset 417 8209da83e256
parent 407 3162ff959375
child 422 33a45e4c80c4
equal deleted inserted replaced
416:f32b323fd347 417:8209da83e256
   151         global _db_mod
   151         global _db_mod
   152         if self._cfg.dget('database.module').lower() == 'psycopg2':
   152         if self._cfg.dget('database.module').lower() == 'psycopg2':
   153             try:
   153             try:
   154                 _db_mod = __import__('psycopg2')
   154                 _db_mod = __import__('psycopg2')
   155             except ImportError:
   155             except ImportError:
   156                 raise VMMError(_(u"Unable to import database module '%s'") %
   156                 raise VMMError(_(u"Unable to import database module '%s'.") %
   157                                'psycopg2', VMM_ERROR)
   157                                'psycopg2', VMM_ERROR)
   158             self._db_connect = self._psycopg2_connect
   158             self._db_connect = self._psycopg2_connect
   159         else:
   159         else:
   160             try:
   160             try:
   161                 tmp = __import__('pyPgSQL', globals(), locals(), ['PgSQL'])
   161                 tmp = __import__('pyPgSQL', globals(), locals(), ['PgSQL'])
   162             except ImportError:
   162             except ImportError:
   163                 raise VMMError(_(u"Unable to import database module '%s'") %
   163                 raise VMMError(_(u"Unable to import database module '%s'.") %
   164                                'pyPgSQL', VMM_ERROR)
   164                                'pyPgSQL', VMM_ERROR)
   165             _db_mod = tmp.PgSQL
   165             _db_mod = tmp.PgSQL
   166             self._db_connect = self._pypgsql_connect
   166             self._db_connect = self._pypgsql_connect
   167 
   167 
   168     def _pypgsql_connect(self):
   168     def _pypgsql_connect(self):
   592             self._warnings.extend(('  * %s' % w for w in warnings))
   592             self._warnings.extend(('  * %s' % w for w in warnings))
   593         for destination in destinations:
   593         for destination in destinations:
   594             if destination.gid and \
   594             if destination.gid and \
   595                not self._chk_other_address_types(destination, TYPE_RELOCATED):
   595                not self._chk_other_address_types(destination, TYPE_RELOCATED):
   596                 self._warnings.append(_(u"The destination account/alias '%s' "
   596                 self._warnings.append(_(u"The destination account/alias '%s' "
   597                                         u"doesn't exist.") % destination)
   597                                         u"does not exist.") % destination)
   598 
   598 
   599     def user_delete(self, emailaddress, force=False):
   599     def user_delete(self, emailaddress, force=False):
   600         """Wrapper around Account.delete(...)"""
   600         """Wrapper around Account.delete(...)"""
   601         if not isinstance(force, bool):
   601         if not isinstance(force, bool):
   602             raise TypeError('force must be a bool')
   602             raise TypeError('force must be a bool')
   603         acc = self._get_account(emailaddress)
   603         acc = self._get_account(emailaddress)
   604         if not acc:
   604         if not acc:
   605             raise VMMError(_(u"The account '%s' doesn't exist.") %
   605             raise VMMError(_(u"The account '%s' does not exist.") %
   606                            acc.address, NO_SUCH_ACCOUNT)
   606                            acc.address, NO_SUCH_ACCOUNT)
   607         uid = acc.uid
   607         uid = acc.uid
   608         gid = acc.gid
   608         gid = acc.gid
   609         dom_dir = acc.domain.directory
   609         dom_dir = acc.domain.directory
   610         acc_dir = acc.home
   610         acc_dir = acc.home
   629         instances) for the `Alias` with the given *aliasaddress*."""
   629         instances) for the `Alias` with the given *aliasaddress*."""
   630         alias = self._get_alias(aliasaddress)
   630         alias = self._get_alias(aliasaddress)
   631         if alias:
   631         if alias:
   632             return alias.get_destinations()
   632             return alias.get_destinations()
   633         if not self._is_other_address(alias.address, TYPE_ALIAS):
   633         if not self._is_other_address(alias.address, TYPE_ALIAS):
   634             raise VMMError(_(u"The alias '%s' doesn't exist.") %
   634             raise VMMError(_(u"The alias '%s' does not exist.") %
   635                            alias.address, NO_SUCH_ALIAS)
   635                            alias.address, NO_SUCH_ALIAS)
   636 
   636 
   637     def alias_delete(self, aliasaddress, targetaddress=None):
   637     def alias_delete(self, aliasaddress, targetaddress=None):
   638         """Deletes the `Alias` *aliasaddress* with all its destinations from
   638         """Deletes the `Alias` *aliasaddress* with all its destinations from
   639         the database. If *targetaddress* is not ``None``, only this
   639         the database. If *targetaddress* is not ``None``, only this
   650             raise VMMError(_(u"Invalid argument: '%s'") % details,
   650             raise VMMError(_(u"Invalid argument: '%s'") % details,
   651                            INVALID_ARGUMENT)
   651                            INVALID_ARGUMENT)
   652         acc = self._get_account(emailaddress)
   652         acc = self._get_account(emailaddress)
   653         if not acc:
   653         if not acc:
   654             if not self._is_other_address(acc.address, TYPE_ACCOUNT):
   654             if not self._is_other_address(acc.address, TYPE_ACCOUNT):
   655                 raise VMMError(_(u"The account '%s' doesn't exist.") %
   655                 raise VMMError(_(u"The account '%s' does not exist.") %
   656                                acc.address, NO_SUCH_ACCOUNT)
   656                                acc.address, NO_SUCH_ACCOUNT)
   657         info = acc.get_info()
   657         info = acc.get_info()
   658         if self._cfg.dget('account.disk_usage') or details in ('du', 'full'):
   658         if self._cfg.dget('account.disk_usage') or details in ('du', 'full'):
   659             path = os.path.join(acc.home, acc.mail_location.directory)
   659             path = os.path.join(acc.home, acc.mail_location.directory)
   660             info['disk usage'] = self._get_disk_usage(path)
   660             info['disk usage'] = self._get_disk_usage(path)
   676         if not isinstance(password, basestring) or not password:
   676         if not isinstance(password, basestring) or not password:
   677             raise VMMError(_(u"Could not accept password: '%s'") % password,
   677             raise VMMError(_(u"Could not accept password: '%s'") % password,
   678                            INVALID_ARGUMENT)
   678                            INVALID_ARGUMENT)
   679         acc = self._get_account(emailaddress)
   679         acc = self._get_account(emailaddress)
   680         if not acc:
   680         if not acc:
   681             raise VMMError(_(u"The account '%s' doesn't exist.") %
   681             raise VMMError(_(u"The account '%s' does not exist.") %
   682                            acc.address, NO_SUCH_ACCOUNT)
   682                            acc.address, NO_SUCH_ACCOUNT)
   683         acc.modify('password', password)
   683         acc.modify('password', password)
   684 
   684 
   685     def user_name(self, emailaddress, name):
   685     def user_name(self, emailaddress, name):
   686         """Wrapper for Account.modify('name', ...)."""
   686         """Wrapper for Account.modify('name', ...)."""
   687         if not isinstance(name, basestring) or not name:
   687         if not isinstance(name, basestring) or not name:
   688             raise VMMError(_(u"Could not accept name: '%s'") % name,
   688             raise VMMError(_(u"Could not accept name: '%s'") % name,
   689                            INVALID_ARGUMENT)
   689                            INVALID_ARGUMENT)
   690         acc = self._get_account(emailaddress)
   690         acc = self._get_account(emailaddress)
   691         if not acc:
   691         if not acc:
   692             raise VMMError(_(u"The account '%s' doesn't exist.") %
   692             raise VMMError(_(u"The account '%s' does not exist.") %
   693                            acc.address, NO_SUCH_ACCOUNT)
   693                            acc.address, NO_SUCH_ACCOUNT)
   694         acc.modify('name', name)
   694         acc.modify('name', name)
   695 
   695 
   696     def user_quotalimit(self, emailaddress, bytes_, messages=0):
   696     def user_quotalimit(self, emailaddress, bytes_, messages=0):
   697         """Wrapper for Account.update_quotalimit(QuotaLimit)."""
   697         """Wrapper for Account.update_quotalimit(QuotaLimit)."""
   698         if not all(isinstance(i, (int, long)) for i in (bytes_, messages)):
   698         if not all(isinstance(i, (int, long)) for i in (bytes_, messages)):
   699             raise TypeError("'bytes_' and 'messages' have to be "
   699             raise TypeError("'bytes_' and 'messages' have to be "
   700                             "integers or longs.")
   700                             "integers or longs.")
   701         acc = self._get_account(emailaddress)
   701         acc = self._get_account(emailaddress)
   702         if not acc:
   702         if not acc:
   703             raise VMMError(_(u"The account '%s' doesn't exist.") %
   703             raise VMMError(_(u"The account '%s' does not exist.") %
   704                            acc.address, NO_SUCH_ACCOUNT)
   704                            acc.address, NO_SUCH_ACCOUNT)
   705         acc.update_quotalimit(QuotaLimit(self._dbh, bytes=bytes_,
   705         acc.update_quotalimit(QuotaLimit(self._dbh, bytes=bytes_,
   706                                          messages=messages))
   706                                          messages=messages))
   707 
   707 
   708     def user_transport(self, emailaddress, transport):
   708     def user_transport(self, emailaddress, transport):
   710         if not isinstance(transport, basestring) or not transport:
   710         if not isinstance(transport, basestring) or not transport:
   711             raise VMMError(_(u"Could not accept transport: '%s'") % transport,
   711             raise VMMError(_(u"Could not accept transport: '%s'") % transport,
   712                            INVALID_ARGUMENT)
   712                            INVALID_ARGUMENT)
   713         acc = self._get_account(emailaddress)
   713         acc = self._get_account(emailaddress)
   714         if not acc:
   714         if not acc:
   715             raise VMMError(_(u"The account '%s' doesn't exist.") %
   715             raise VMMError(_(u"The account '%s' does not exist.") %
   716                            acc.address, NO_SUCH_ACCOUNT)
   716                            acc.address, NO_SUCH_ACCOUNT)
   717         acc.update_transport(Transport(self._dbh, transport=transport))
   717         acc.update_transport(Transport(self._dbh, transport=transport))
   718 
   718 
   719     def user_disable(self, emailaddress, services=None):
   719     def user_disable(self, emailaddress, services=None):
   720         """Wrapper for Account.disable(*services)"""
   720         """Wrapper for Account.disable(*services)"""
   722             services = []
   722             services = []
   723         else:
   723         else:
   724             assert isinstance(services, list)
   724             assert isinstance(services, list)
   725         acc = self._get_account(emailaddress)
   725         acc = self._get_account(emailaddress)
   726         if not acc:
   726         if not acc:
   727             raise VMMError(_(u"The account '%s' doesn't exist.") % acc.address,
   727             raise VMMError(_(u"The account '%s' does not exist.") %
   728                            NO_SUCH_ACCOUNT)
   728                            acc.address, NO_SUCH_ACCOUNT)
   729         acc.disable(*services)
   729         acc.disable(*services)
   730 
   730 
   731     def user_enable(self, emailaddress, services=None):
   731     def user_enable(self, emailaddress, services=None):
   732         """Wrapper for Account.enable(*services)"""
   732         """Wrapper for Account.enable(*services)"""
   733         if services is None:
   733         if services is None:
   734             services = []
   734             services = []
   735         else:
   735         else:
   736             assert isinstance(services, list)
   736             assert isinstance(services, list)
   737         acc = self._get_account(emailaddress)
   737         acc = self._get_account(emailaddress)
   738         if not acc:
   738         if not acc:
   739             raise VMMError(_(u"The account '%s' doesn't exist.") % acc.address,
   739             raise VMMError(_(u"The account '%s' does not exist.") %
   740                            NO_SUCH_ACCOUNT)
   740                            acc.address, NO_SUCH_ACCOUNT)
   741         acc.enable(*services)
   741         acc.enable(*services)
   742 
   742 
   743     def relocated_add(self, emailaddress, targetaddress):
   743     def relocated_add(self, emailaddress, targetaddress):
   744         """Creates a new `Relocated` entry in the database. If there is
   744         """Creates a new `Relocated` entry in the database. If there is
   745         already a relocated user with the given *emailaddress*, only the
   745         already a relocated user with the given *emailaddress*, only the
   750         destination = DestinationEmailAddress(targetaddress, self._dbh)
   750         destination = DestinationEmailAddress(targetaddress, self._dbh)
   751         relocated.set_destination(destination)
   751         relocated.set_destination(destination)
   752         if destination.gid and \
   752         if destination.gid and \
   753            not self._chk_other_address_types(destination, TYPE_RELOCATED):
   753            not self._chk_other_address_types(destination, TYPE_RELOCATED):
   754             self._warnings.append(_(u"The destination account/alias '%s' "
   754             self._warnings.append(_(u"The destination account/alias '%s' "
   755                                     u"doesn't exist.") % destination)
   755                                     u"does not exist.") % destination)
   756 
   756 
   757     def relocated_info(self, emailaddress):
   757     def relocated_info(self, emailaddress):
   758         """Returns the target address of the relocated user with the given
   758         """Returns the target address of the relocated user with the given
   759         *emailaddress*."""
   759         *emailaddress*."""
   760         relocated = self._get_relocated(emailaddress)
   760         relocated = self._get_relocated(emailaddress)
   761         if relocated:
   761         if relocated:
   762             return relocated.get_info()
   762             return relocated.get_info()
   763         if not self._is_other_address(relocated.address, TYPE_RELOCATED):
   763         if not self._is_other_address(relocated.address, TYPE_RELOCATED):
   764             raise VMMError(_(u"The relocated user '%s' doesn't exist.") %
   764             raise VMMError(_(u"The relocated user '%s' does not exist.") %
   765                            relocated.address, NO_SUCH_RELOCATED)
   765                            relocated.address, NO_SUCH_RELOCATED)
   766 
   766 
   767     def relocated_delete(self, emailaddress):
   767     def relocated_delete(self, emailaddress):
   768         """Deletes the relocated user with the given *emailaddress* from
   768         """Deletes the relocated user with the given *emailaddress* from
   769         the database."""
   769         the database."""