Edgewall Software

Ticket #7608: query.patch

File query.patch, 3.1 KB (added by trac-dev@…, 3 years ago)

Simple implementation

  • trac/ticket/query.py

    === trac/ticket/query.py
    ==================================================================
     
    226226        return cols 
    227227 
    228228    def count(self, req, db=None, cached_ids=None): 
    229         sql, args = self.get_sql(req, cached_ids) 
    230         return self._count(sql, args) 
     229        self.execute(req, db, cached_ids) 
     230        return self.num_items 
    231231 
    232     def _count(self, sql, args, db=None): 
    233         if not db: 
    234             db = self.env.get_db_cnx() 
    235         cursor = db.cursor() 
    236  
    237         count_sql = 'SELECT COUNT(*) FROM (' + sql + ') AS foo' 
    238         # self.env.log.debug("Count results in Query SQL: " + count_sql %  
    239         #                    tuple([repr(a) for a in args])) 
    240  
    241         cnt = 0 
    242         try: 
    243             cursor.execute(count_sql, args); 
    244         except: 
    245             db.rollback() 
    246             raise 
    247         for cnt, in cursor: 
    248             break 
    249         self.env.log.debug("Count results in Query: %d" % cnt) 
    250         return cnt 
    251  
    252232    def execute(self, req, db=None, cached_ids=None): 
    253233        if not db: 
    254234            db = self.env.get_db_cnx() 
    255235        cursor = db.cursor() 
    256236 
    257237        sql, args = self.get_sql(req, cached_ids) 
    258         self.num_items = self._count(sql, args, db) 
    259238 
    260         if self.num_items <= self.max: 
    261             self.has_more_pages = False 
    262  
    263         if self.has_more_pages: 
    264             max = self.max 
    265             if self.group: 
    266                 max += 1 
    267             sql = sql + " LIMIT %d OFFSET %d" % (max, self.offset) 
    268             if (self.page > int(ceil(float(self.num_items) / self.max)) and 
    269                 self.num_items != 0): 
    270                 raise TracError(_('Page %(page)s is beyond the number of ' 
    271                                   'pages in the query', page=self.page)) 
    272  
    273239        self.env.log.debug("Query SQL: " + sql % tuple([repr(a) for a in args]))      
    274240        try: 
    275241            cursor.execute(sql, args) 
     
    304270                    except (TypeError, ValueError): 
    305271                        val = False 
    306272                result[name] = val 
    307             results.append(result) 
     273            if 'TICKET_VIEW' in req.perm('ticket', result['id']): 
     274                results.append(result) 
    308275        cursor.close() 
     276 
     277        self.num_items = len(results) 
     278 
     279        if self.num_items <= self.max: 
     280            self.has_more_pages = False 
     281 
     282        if self.has_more_pages: 
     283            max = self.max 
     284            if self.group: 
     285                max += 1 
     286            if (self.page > int(ceil(float(self.num_items) / self.max)) and 
     287                self.num_items != 0): 
     288                raise TracError(_('Page %(page)s is beyond the number of ' 
     289                                  'pages in the query', page=self.page)) 
    309290        return results 
    310291 
    311292    def get_href(self, href, id=None, order=None, desc=None, format=None, 
     
    667648 
    668649        results = Paginator(tickets, 
    669650                            self.page - 1, 
    670                             self.max, 
    671                             self.num_items) 
     651                            self.max) 
    672652         
    673653        if req: 
    674654            if results.has_next_page: