VirtualMailManager/serviceset.py
branchv0.7.x
changeset 691 932cd76bf879
parent 676 2bc11dada296
child 694 b1bfd4d1d9c0
equal deleted inserted replaced
690:06c0acf6c45d 691:932cd76bf879
    30     `sieve` : bool
    30     `sieve` : bool
    31       Boolean flag for service sieve
    31       Boolean flag for service sieve
    32     `services` : dict
    32     `services` : dict
    33       The four services above with boolean values
    33       The four services above with boolean values
    34     """
    34     """
    35     __slots__ = ('_ssid', '_services', '_sieve_col', '_dbh')
    35     __slots__ = ('_ssid', '_services', '_dbh')
    36     _kwargs = (('ssid',) + SERVICES)
    36     _kwargs = (('ssid',) + SERVICES)
    37 
    37 
    38     def __init__(self, dbh, **kwargs):
    38     def __init__(self, dbh, **kwargs):
    39         """Creates a new ServiceSet instance.
    39         """Creates a new ServiceSet instance.
    40 
    40 
    58           Boolean flag for service sieve - default `True`
    58           Boolean flag for service sieve - default `True`
    59         """
    59         """
    60         self._dbh = dbh
    60         self._dbh = dbh
    61         self._ssid = 0
    61         self._ssid = 0
    62         self._services = dict.fromkeys(SERVICES, True)
    62         self._services = dict.fromkeys(SERVICES, True)
    63         if cfg_dget('misc.dovecot_version') < 0x10200b02:
       
    64             self._sieve_col = 'managesieve'
       
    65         else:
       
    66             self._sieve_col = 'sieve'
       
    67 
    63 
    68         for key in kwargs.keys():
    64         for key in kwargs.keys():
    69             if key not in self.__class__._kwargs:
    65             if key not in self.__class__._kwargs:
    70                 raise ValueError('unrecognized keyword: %r' % key)
    66                 raise ValueError('unrecognized keyword: %r' % key)
    71             if key == 'ssid':
    67             if key == 'ssid':
   106     def _load_by_services(self):
   102     def _load_by_services(self):
   107         """Try to load the service_set by it's service combination."""
   103         """Try to load the service_set by it's service combination."""
   108         sql = ('SELECT ssid FROM service_set WHERE %s' %
   104         sql = ('SELECT ssid FROM service_set WHERE %s' %
   109                ' AND '.join('%s = %s' %
   105                ' AND '.join('%s = %s' %
   110                (k, str(v).upper()) for k, v in self._services.items()))
   106                (k, str(v).upper()) for k, v in self._services.items()))
   111         if self._sieve_col == 'managesieve':
       
   112             sql = sql.replace('sieve', self._sieve_col)
       
   113         dbc = self._dbh.cursor()
   107         dbc = self._dbh.cursor()
   114         dbc.execute(sql)
   108         dbc.execute(sql)
   115         result = dbc.fetchone()
   109         result = dbc.fetchone()
   116         dbc.close()
   110         dbc.close()
   117         if result:
   111         if result:
   120             self._save()
   114             self._save()
   121 
   115 
   122     def _load_by_ssid(self, ssid):
   116     def _load_by_ssid(self, ssid):
   123         """Try to load the service_set by it's primary key."""
   117         """Try to load the service_set by it's primary key."""
   124         dbc = self._dbh.cursor()
   118         dbc = self._dbh.cursor()
   125         dbc.execute('SELECT ssid, smtp, pop3, imap, %s' % (self._sieve_col,) +
   119         dbc.execute('SELECT ssid, smtp, pop3, imap, sieve '
   126                     ' FROM service_set WHERE ssid = %s', (ssid,))
   120                     'FROM service_set WHERE ssid = %s', (ssid,))
   127         result = dbc.fetchone()
   121         result = dbc.fetchone()
   128         dbc.close()
   122         dbc.close()
   129         if not result:
   123         if not result:
   130             raise ValueError('Unknown service_set id specified: %r' % ssid)
   124             raise ValueError('Unknown service_set id specified: %r' % ssid)
   131         self._ssid = result[0]
   125         self._ssid = result[0]
   133         for key, value in zip(SERVICES, result[1:]):  # pyPgSQL compatible
   127         for key, value in zip(SERVICES, result[1:]):  # pyPgSQL compatible
   134             self._services[key] = True if value else False
   128             self._services[key] = True if value else False
   135 
   129 
   136     def _save(self):
   130     def _save(self):
   137         """Store a new service_set in the database."""
   131         """Store a new service_set in the database."""
   138         sql = ('INSERT INTO service_set (ssid, smtp, pop3, imap, %s) ' %
   132         sql = ('INSERT INTO service_set (ssid, smtp, pop3, imap, sieve) '
   139                (self._sieve_col,) +
       
   140                'VALUES (%(ssid)s, %(smtp)s, %(pop3)s, %(imap)s, %(sieve)s)')
   133                'VALUES (%(ssid)s, %(smtp)s, %(pop3)s, %(imap)s, %(sieve)s)')
   141         self._set_ssid()
   134         self._set_ssid()
   142         values = {'ssid': self._ssid}
   135         values = {'ssid': self._ssid}
   143         values.update(self._services)
   136         values.update(self._services)
   144         dbc = self._dbh.cursor()
   137         dbc = self._dbh.cursor()