Trac is being migrated to new services! Issues can be found in our new YouTrack instance and WIKI pages can be found on our website.

LocalTracChanges: cached_restrict_users_list.diff

File cached_restrict_users_list.diff, 2.9 KB (added by datallah, 16 years ago)

Patch to cache list of users to whom a ticket can be assigned

  • api.py

    old new  
    2424from trac.util.text import shorten_line
    2525from trac.wiki import IWikiSyntaxProvider, Formatter
    2626
     27try:
     28    import threading
     29except ImportError:
     30    import dummy_threading as threading
    2731
    2832class ITicketChangeListener(Interface):
    2933    """Extension point interface for components that require notification when
     
    6872        [TracTickets#Assign-toasDrop-DownList Assign-to as Drop-Down List]
    6973        (''since 0.9'').""")
    7074
     75    cached_users = None
     76    cached_perm_id = None
     77    _cache_lock = threading.Lock()
     78
    7179    # Public API
    7280
    7381    def get_available_actions(self, ticket, perm_):
     
    99107        field = {'name': 'owner', 'label': 'Owner'}
    100108        if self.restrict_owner:
    101109            field['type'] = 'select'
    102             users = []
    103             perm = PermissionSystem(self.env)
    104             for username, name, email in self.env.get_known_users(db):
    105                 if perm.get_user_permissions(username).get('TICKET_MODIFY'):
    106                     users.append(username)
    107             field['options'] = users
    108             field['optional'] = True
     110
     111            perm_id = None
     112            try:
     113                db = self.env.get_db_cnx()
     114                cursor = db.cursor()
     115                cursor.execute("SELECT value FROM system WHERE name = 'perm_id'")
     116                perm_row = cursor.fetchone()
     117                cursor.close()
     118                if not perm_row:
     119                    raise TracError('perm_id KVP in system table missing',
     120                            'Invalid restricted owner caching data - is the trigger running?')
     121                perm_id = perm_row[0];
     122            except:
     123               pass
     124
     125            """We check once before locking and once after to prevent locking when we don't need to """
     126            if self.cached_users is None or (perm_id is not None and perm_id > self.cached_perm_id):
     127 
     128                self._cache_lock.acquire()
     129                try:
     130                    if self.cached_users is None or (perm_id is not None and perm_id > self.cached_perm_id):
     131                        users = []
     132                        perm = PermissionSystem(self.env)
     133                        for username, name, email in self.env.get_known_users(db):
     134                            if perm.get_user_permissions(username).get('TICKET_MODIFY'):
     135                                users.append(username)
     136                        self.cached_users = users;
     137                        if perm_id is not None:
     138                            self.cached_perm_id = perm_id
     139                finally:
     140                    self._cache_lock.release()
     141
     142            if self.cached_users is not None:
     143                field['options'] = self.cached_users
     144                field['optional'] = True
     145            else:
     146                field['type'] = 'text'
    109147        else:
    110148            field['type'] = 'text'
    111149        fields.append(field)
All information, including names and email addresses, entered onto this website or sent to mailing lists affiliated with this website will be public. Do not post confidential information, especially passwords!