Edgewall Software

Ticket #2561: attachment-search.patch

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

Patch against 0.11-stable [7346] adding searching in attachment metadata

  • 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 ISearchSource, 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 
     
    297298 
    298299    implements(IEnvironmentSetupParticipant, IRequestHandler, 
    299300               INavigationContributor, IWikiSyntaxProvider, 
    300                IResourceManager) 
     301               IResourceManager, ISearchSource) 
    301302 
    302303    change_listeners = ExtensionPoint(IAttachmentChangeListener) 
    303304    manipulators = ExtensionPoint(IAttachmentManipulator) 
     
    515516            return _("Attachments of %(parent)s", 
    516517                     parent=get_resource_name(self.env, resource.parent)) 
    517518 
     519    # ISearchSource methods 
     520 
     521    def get_search_filters(self, req): 
     522        yield ('attachment', _('Attachments')) 
     523 
     524    def get_search_results(self, req, terms, filters): 
     525        if not 'attachment' in filters: 
     526            return 
     527        db = self.env.get_db_cnx() 
     528        sql_query, args = search_to_sql(db, ['filename', 'description',  
     529                                        'author'], terms) 
     530        cursor = db.cursor() 
     531        cursor.execute("SELECT type,id,time,filename,description,author " 
     532                       "FROM attachment " 
     533                       "WHERE " + sql_query, args) 
     534         
     535        for type, id, time, filename, desc, author in cursor: 
     536            attachment = Resource(type, id).child('attachment', filename) 
     537            if 'ATTACHMENT_VIEW' in req.perm(attachment): 
     538                yield (get_resource_url(self.env, attachment, req.href), 
     539                       "%s: %s" % (get_resource_shortname(self.env,  
     540                                                          attachment.parent), 
     541                                   filename), 
     542                       datetime.fromtimestamp(time, utc), author, 
     543                       shorten_result(desc, terms)) 
     544     
    518545    # Internal methods 
    519546 
    520547    def _do_save(self, req, attachment):