Edgewall Software

Changes between Initial Version and Version 1 of TracDev/PluginDevelopment/ExtensionPoints/trac.attachment.IAttachmentChangeListener


Ignore:
Timestamp:
Jul 17, 2014, 12:06:51 AM (10 years ago)
Author:
Peter Suter
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • TracDev/PluginDevelopment/ExtensionPoints/trac.attachment.IAttachmentChangeListener

    v1 v1  
     1== Extension Point : ''IAttachmentChangeListener'' ==
     2
     3||'''Interface'''||''IAttachmentChangeListener''||'''Since'''||0.10||
     4||'''Module'''||''trac.attachment''||'''Source'''||[source:trunk/trac/attachment.py#/IAttachmentChangeListener attachment.py]||
     5
     6The ''IAttachmentChangeListener'' allows components to listen for and to react to attachment events.
     7
     8== Purpose ==
     9
     10Trac allows users to attach arbitrary files to e.g. tickets or wiki pages. Plugins can hook into the attachment module to trigger their own actions when attachments are added, deleted or reparented (i.e. moved to another parent resource or with a renamed parent resource).
     11
     12The main purpose for this interface is to allow plugins to stay informed about the existing attachments and trigger appropriate actions elsewhere (e.g. sending notifications, starting indexing services, updating supplementary data structures etc.)
     13
     14(For attachment validation and manipulation use [../trac.attachment.IAttachmentManipulator IAttachmentManipulator] instead.)
     15
     16== Usage ==
     17
     18Implementing the interface follows the standard guidelines found in [wiki:TracDev/ComponentArchitecture] and of course [wiki:TracDev/PluginDevelopment].
     19
     20When a user adds a new attachment the `attachment_added` method is called. Similarly `attachment_delete` is called when a user deletes an attachment or its parent resource, and `attachment_reparented` is called when a user moves an attachment to a new parent resource or renames the parent resource.
     21
     22== Examples ==
     23
     24A common use case for attachments is to attach images to wiki pages. For convenience you might want to automatically append an [wiki:WikiMacros#Image-macro Image macro] to the wiki page.
     25The following minimal example IAttachmentChangeListener implementation could implement such a feature:
     26{{{#!python
     27from trac.core import Component, implements
     28from trac.attachment import IAttachmentChangeListener
     29from trac.mimeview.api import get_mimetype
     30from trac.wiki.model import WikiPage
     31
     32class ImageAutoAppender(Component):
     33    implements(IAttachmentChangeListener)
     34
     35    def attachment_added(self, attachment):
     36        if attachment.parent_realm != 'wiki':
     37            return
     38        mimetype = get_mimetype(attachment.filename)
     39        if not mimetype.startswith('image/'):
     40            return
     41        page = WikiPage(self.env, attachment.parent_id)
     42        page.text += '[[Image(%s)]]' % (attachment.filename,)
     43        page.save(attachment.author, 'Auto-append image', attachment.ipnr)
     44
     45    def attachment_deleted(self, attachment):
     46        pass
     47
     48    def attachment_reparented(self, attachment, old_parent_realm, old_parent_id):
     49        pass
     50}}}
     51
     52== Available Implementations ==
     53
     54* th:SearchAttachmentsPlugin: Search attachments
     55* th:ImageTracPlugin: Attach a ticket image
     56* th:TracCiaPlugin: CIA integration
     57
     58== Additional Information and References ==
     59
     60 * [http://www.edgewall.org/docs/trac-trunk/epydoc/trac.attachment.IAttachmentChangeListener-class.html epydoc]
     61 * [http://www.edgewall.org/docs/trac-trunk/html/api/trac_attachment.html#trac.attachment.IAttachmentChangeListener API Reference]
     62 * See [../trac.attachment.IAttachmentManipulator trac.attachment.IAttachmentManipulator]
     63 * See [../trac.ticket.api.IMilestoneChangeListener trac.ticket.api.IMilestoneChangeListener], [../trac.versioncontrol.api.IRepositoryChangeListener trac.versioncontrol.api.IRepositoryChangeListener],  [../trac.ticket.api.ITicketChangeListener trac.ticket.api.ITicketChangeListener], [../trac.wiki.api.IWikiChangeListener trac.wiki.api.IWikiChangeListener]
     64 * Introduced in changeset:3399
     65 * [TracDev/DatabaseSchema/Attachments Attachments database schema]
     66 * Related tickets:
     67  * [query:status!=closed&component=attachment attachment component]
     68  * Generic change listener replacement: #8834, #11148
     69  * #1278 Listen for attachments to record wiki page change
     70  * #2259 Notification when adding attachment to ticket