Edgewall Software

Ticket #2561: attachment-search-2.patch

File attachment-search-2.patch, 3.0 KB (added by Remy Blank <remy.blank@…>, 4 months ago)

Made search in attachments consistent with timeline handling

  • trac/attachment.py

    diff --git a/trac/attachment.py b/trac/attachment.py
    a b  
    3232from trac.mimeview import * 
    3333from trac.perm import PermissionError, PermissionSystem, IPermissionPolicy 
    3434from trac.resource import * 
     35from trac.search import search_to_sql, shorten_result 
    3536from trac.util import get_reporter_id, create_unique_file, content_disposition 
    3637from trac.util.datefmt import to_timestamp, utc 
    3738from trac.util.text import unicode_quote, unicode_unquote, pretty_size 
     
    476477            return format_to_oneliner(self.env, context(attachment.parent), 
    477478                                      descr) 
    478479    
     480    def get_search_results(self, req, resource_realm, terms): 
     481        """Return a search result generator suitable for ISearchSource. 
     482         
     483        Search results are attachments on resources of the given  
     484        `resource_realm.realm` whose filename, description or author match  
     485        the given terms. 
     486        """ 
     487        db = self.env.get_db_cnx() 
     488        sql_query, args = search_to_sql(db, ['filename', 'description',  
     489                                        'author'], terms) 
     490        cursor = db.cursor() 
     491        cursor.execute("SELECT id,time,filename,description,author " 
     492                       "FROM attachment " 
     493                       "WHERE type = %s " 
     494                       "AND " + sql_query, (resource_realm.realm, ) + args) 
     495         
     496        for id, time, filename, desc, author in cursor: 
     497            attachment = resource_realm(id=id).child('attachment', filename) 
     498            if 'ATTACHMENT_VIEW' in req.perm(attachment): 
     499                yield (get_resource_url(self.env, attachment, req.href), 
     500                       "%s: %s" % (get_resource_shortname(self.env,  
     501                                                          attachment.parent), 
     502                                   filename), 
     503                       datetime.fromtimestamp(time, utc), author, 
     504                       shorten_result(desc, terms)) 
     505     
    479506    # IResourceManager methods 
    480507     
    481508    def get_resource_realms(self): 
  • trac/ticket/web_ui.py

    diff --git a/trac/ticket/web_ui.py b/trac/ticket/web_ui.py
    a b  
    197197                                                       resolution, type)), 
    198198                       datetime.fromtimestamp(ts, utc), author, 
    199199                       shorten_result(desc, terms)) 
     200         
     201        # Attachments 
     202        for result in AttachmentModule(self.env).get_search_results( 
     203            req, ticket_realm, terms): 
     204            yield result         
    200205 
    201206    # ITimelineEventProvider methods 
    202207 
  • trac/wiki/web_ui.py

    diff --git a/trac/wiki/web_ui.py b/trac/wiki/web_ui.py
    a b  
    617617                       '%s: %s' % (name, shorten_line(text)), 
    618618                       datetime.fromtimestamp(ts, utc), author, 
    619619                       shorten_result(text, terms)) 
     620         
     621        # Attachments 
     622        for result in AttachmentModule(self.env).get_search_results( 
     623            req, wiki_realm, terms): 
     624            yield result