== Extension Point : ''INotificationFormatter'' == ||'''Interface'''||''INotificationFormatter''||'''Since'''||[wiki:TracDev/ApiChanges/1.1#INotificationFormatter 1.1.3]|| ||'''Module'''||''trac.notification''||'''Source'''||[source:trunk/trac/notification/api.py#/INotificationFormatter api.py]|| The ''INotificationFormatter'' formats [TracNotification notification] events to messages in various formats ready to be distributed. == Purpose == Trac 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. == Usage == Implementing the interface follows the standard guidelines found in [wiki:TracDev/ComponentArchitecture] and of course [wiki:TracDev/PluginDevelopment]. A simple `get_supported_styles()` method lists the supported MIME types and event realms for a given transport. The `format()` method formats a notification event. It receives the following parameters: * `transport`: The name of the transports that should be used. (See [wiki:trac.notification.api.INotificationDistributor INotificationDistributor]) * `style`: The style that should be used. One of those returned by `get_supported_styles()`. * `event`: A `trac.notification.api.NotificationEvent` instance describing the event about which the recipients should be notified. It should return the formatted message. The exact return type depends on the transport. == Examples == The following example formats notifications to a short string for distribution via SMS. {{{#!python from trac.core import * from trac.notification.api import INotificationFormatter class ShortTicketNotificationFormatter(Component): implements(INotificationFormatter) # INotificationFormatter methods def get_supported_styles(self, transport): if transport == 'sms': yield ('text/plain', 'ticket') def format(self, transport, style, event): if transport == 'sms' and event.realm == 'ticket': return "#{0} {1} by {2}" % (event.target, event.category, event.author) }}} == Available Implementations == * [source:trunk/trac/ticket/notification.py#/TicketFormatter trac.ticket.notification.TicketFormatter] == Additional Information and References == * [apiref:trac.notification.api.INotificationFormatter-class epydoc] * [apidoc:api/trac_notification#trac.notification.api.INotificationFormatter API Reference] * Related to the [wiki:trac.notification.api.INotificationDistributor INotificationDistributor] === History * This interface originated in th:AnnouncerPlugin as `IAnnouncementFormatter`. * [wiki:TracDev/ApiChanges/1.1#INotificationFormatter 1.1.3]: Integrated `INotificationFormatter` in Trac as part of [wiki:TracDev/Proposals/AdvancedNotification this proposal] (#3517) * Dropped the `alternative_style_for()` method. (The distributor can select fallbacks without this.) * Dropped the `realm` parameter to the `format()` method. (Use `event.realm` instead.) * Switched from `styles(transport, realm)` to `get_supported_styles(transport)`