Edgewall Software

Changes between Initial Version and Version 1 of TracDev/PluginDevelopment/ExtensionPoints/trac.notification.api.INotificationFormatter


Ignore:
Timestamp:
Dec 6, 2014, 12:51:19 PM (9 years ago)
Author:
Peter Suter
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • TracDev/PluginDevelopment/ExtensionPoints/trac.notification.api.INotificationFormatter

    v1 v1  
     1== Extension Point : ''INotificationFormatter'' ==
     2
     3||'''Interface'''||''INotificationFormatter''||'''Since'''||[wiki:TracDev/ApiChanges/1.1.3#INotificationFormatter 1.1.3]||
     4||'''Module'''||''trac.notification''||'''Source'''||[source:trunk/trac/notification/api.py#/INotificationFormatter api.py]||
     5
     6The ''INotificationFormatter'' formats [TracNotification notification] events to messages in various formats ready to be distributed.
     7
     8== Purpose ==
     9
     10Trac provides an extendible and flexible notification system, that historically has sent plain text emails for ticket changes. Notifications of different event realms (e.g. wiki notifications), transports (e.g. SMS) and message formats (e.g. HTML messages) might all require different formatting logic though.
     11
     12== Usage ==
     13
     14Implementing the interface follows the standard guidelines found in [wiki:TracDev/ComponentArchitecture] and of course [wiki:TracDev/PluginDevelopment].
     15
     16A simple `get_supported_styles()` method lists the supported MIME types and event realms for a given transport.
     17
     18The `format()` method formats a notification event. It receives the following parameters:
     19* `transport`: The name of the transports that should be used. (See [wiki:trac.notification.api.INotificationDistributor INotificationDistributor])
     20* `style`: The style that should be used. One of those returned by `get_supported_styles()`.
     21* `event`: A `trac.notification.api.NotificationEvent` instance describing the event about which the recipients should be notified.
     22
     23It should return the formatted message. The exact return type depends on the transport.
     24
     25== Examples ==
     26
     27The following example formats notifications to a short string for distribution via SMS.
     28
     29{{{#!python
     30from trac.core import *
     31from trac.notification.api import INotificationFormatter
     32
     33class ShortTicketNotificationFormatter(Component):
     34
     35    implements(INotificationFormatter)
     36
     37    # INotificationFormatter methods
     38   
     39    def get_supported_styles(self, transport):
     40        if transport == 'sms':
     41            yield ('text/plain', 'ticket')
     42
     43    def format(self, transport, style, event):
     44        if transport == 'sms' and event.realm == 'ticket':
     45            return "#{0} {1} by {2}" % (event.target,
     46                                        event.category,
     47                                        event.author)
     48}}}
     49
     50== Available Implementations ==
     51
     52* [source:trunk/trac/ticket/notification.py#/TicketFormatter trac.ticket.notification.TicketFormatter]
     53
     54== Additional Information and References ==
     55 * [apiref:trac.notification.api.INotificationFormatter-class epydoc]
     56 * [apidoc:api/trac_notification#trac.notification.api.INotificationFormatter API Reference]
     57 * Related to the [wiki:trac.notification.api.INotificationDistributor INotificationDistributor]
     58
     59=== History
     60 * This interface originated in th:AnnouncerPlugin as `IAnnouncementFormatter`.
     61* [wiki:TracDev/ApiChanges/1.1.3#INotificationDistributor 1.1.3]: Integrated `INotificationFormatter` in Trac as part of [wiki:TracDev/Proposals/AdvancedNotification this proposal] (#3517)
     62   * Dropped the `alternative_style_for()` method. (The distributor can select fallbacks without this.)
     63   * Dropped the `realm` parameter to the `format()` method. (Use `event.realm` instead.)
     64   * Switched from `styles(transport, realm)` to `get_supported_styles(transport)`