Changeset 2751

Show
Ignore:
Timestamp:
11/07/07 15:49:15 (1 year ago)
Author:
gt4329b
Message:

fixed to work with Postgres: the _ensure_user_data method was using an INSERT IGNORE statement, which is not support SQL in Postgres. Modified the method to grab a list of known users from session_attribute and only do an insert for any that aren't already present. Really this is an optimization *and* a bugfix, as the 99.9% of the time situation the initial SELECT is all that will be performed per ticket page load, which will be a heck of a lot faster than a whole slew of INSERT IGNOREs (or the equivalent) that just fall through.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • flexibleassigntoplugin/0.11/trunk/flexibleassignto/flexibleassignto.py

    r2665 r2751  
    1313## python imports 
    1414from pprint import pprint, pformat 
     15import psycopg2 #import IntegrityError 
    1516 
    1617## trac imports 
     
    208209        return _tag 
    209210 
     211    def _get_allusers_session_info(self, cnx): 
     212        """ 
     213        """ 
     214        cursor = cnx.cursor() 
     215        cursor.execute(''' 
     216            SELECT DISTINCT n.sid AS sid, n.value AS name, e.value AS email 
     217            FROM session_attribute AS n  
     218             LEFT JOIN session_attribute AS e ON (e.sid=n.sid 
     219                AND e.authenticated=1 AND e.name = 'email') 
     220            WHERE n.authenticated=1  
     221                AND n.name = 'name' 
     222            ORDER BY n.sid''') 
     223        for username,name,email in cursor: 
     224            yield username, name, email         
     225 
    210226    def _ensure_user_data(self, users): 
    211227        """ 
     
    214230        values for 'name' or 'email'. 
    215231        """ 
    216         EMAIL_SQL = '''INSERT OR IGNORE INTO session_attribute  
     232        EMAIL_SQL = '''INSERT INTO session_attribute  
    217233            (sid, authenticated, name, value)  
    218             VALUES ("%s", 1, "email" , "%s")''' 
    219         NAME_SQL = '''INSERT OR IGNORE INTO session_attribute  
     234            VALUES ('%s', 1, 'email' , '%s')''' 
     235        NAME_SQL = '''INSERT INTO session_attribute  
    220236            (sid, authenticated, name, value)  
    221             VALUES ("%s", 1, "name" , "%s")''' 
     237            VALUES ('%s', 1, 'name' , '%s')''' 
    222238        db = self.env.get_db_cnx() 
    223239        cursor = db.cursor() 
     240        known_users = self._get_allusers_session_info(db) 
     241        known_usernames = [u[0] for u in known_users] 
    224242        for u in users: 
    225243            _username = str(u.getUsername()).strip() 
    226244            _email = str(u.email).strip() 
    227245            _fullname = str(u.fullname).strip() 
    228             if u.getUsername() and _username != '': 
     246            if u.getUsername() and _username != '' and \ 
     247                                            _username not in known_usernames: 
    229248                if u.email and _email != '': 
    230                     cursor.execute(EMAIL_SQL % (_username, _email)) 
     249                    try: 
     250                        cursor.execute(EMAIL_SQL % (_username, _email)) 
     251                    except psycopg2.IntegrityError: 
     252                        pass 
    231253                if u.fullname and _fullname != '': 
    232                     cursor.execute(NAME_SQL % (_username, _fullname)) 
    233         db.commit() 
     254                    try: 
     255                        cursor.execute(NAME_SQL % (_username, _fullname)) 
     256                    except psycopg2.IntegrityError: 
     257                        pass                     
     258        #db.commit() 
    234259        cursor.close() 
    235260