Edgewall Software

Changes between Initial Version and Version 1 of TracDev/Proposals/AdvancedNotification/INotificationFormatter


Ignore:
Timestamp:
Nov 17, 2012, 11:31:25 PM (11 years ago)
Author:
Peter Suter
Comment:

Document the proposed INotificationFormatter extension point.

Legend:

Unmodified
Added
Removed
Modified
  • TracDev/Proposals/AdvancedNotification/INotificationFormatter

    v1 v1  
     1== Extension Point : ''INotificationFormatter'' ==
     2
     3||'''Interface'''||''INotificationFormatter''||'''Since'''||[wiki:TracDev/ApiChanges/1.1.1#INotificationFormatter 1.1.1]||
     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 `styles()` method lists the supported MIME types for a given transport and event realm.
     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:INotificationDistributor INotificationDistributor])
     20* `style`: The style that should be used. One of those returned by `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 by SMS. (The `sms` library does not exist. Several commercial SMS services provide real APIs.)
     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 styles(self, transport, realm):
     40        if transport == 'sms' and realm == 'ticket':
     41            yield 'text/plain and short'
     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
     52Only `trac.ticket.notification.TicketFormatter` is part of core Trac.
     53
     54Various other formatters will be part of th:AnnouncerPlugin.
     55
     56== Additional Information and References ==
     57
     58 * [http://www.edgewall.org/docs/trac-trunk/epydoc/trac.notification.api.INotificationFormatter-class.html epydoc]
     59 * [http://www.edgewall.org/docs/trac-trunk/html/api/trac_notification.html#trac.notification.api.INotificationFormatter API Reference]
     60 * Related to the [wiki:INotificationDistributor INotificationDistributor]
     61 * This interface originated in th:AnnouncerPlugin as `IAnnouncementFormatter`.
     62   * DONE Dropped the `alternative_style_for()` method. (The distributor can select fallbacks without this.)
     63   * DONE Dropped the `realm` parameter to the `format()` method. (Use `event.realm` instead.)