ctdo-trac/TracRendezVous/tracrendezvous/rendezvous/notification.py

136 lines
5.2 KiB
Python

# -*- coding: utf-8 -*-
from trac import __version__
from trac.core import *
from trac.config import *
from trac.notification import NotifyEmail
from trac.util import md5
from trac.util.datefmt import to_timestamp
from trac.util.text import CRLF, wrap, to_unicode, obfuscate_email_address
from genshi.template.text import TextTemplate
from tracrendezvous.model import RendezVous, RendezVousLocation
class RendezVousNotificationSystem(Component):
always_notify_workflow_change = BoolOption('rendezvous', 'always_notify_workflow_changes',
'true',
"""Always send notifications to all authors, voters in the rendezvous on workflow changes""")
ticket_subject_template = Option('rendezvous', 'ticket_subject_template',
'$prefix #$rendezvous.rendezvous_id: $summary',
"""A Genshi text template snippet used to get the notification subject.
(since 0.3)""")
class RendezVousSchedulingNotifyEmail(NotifyEmail):
"""Notification for a scheduled rendezvous.
"""
template_name = "rendezvous_notify_email.txt"
rendezvous = None
modtime = 0
from_email = 'trac+rendezvous@localhost'
COLS = 75
ical_fmt = "%Y%m%dT%H%M00Z"
def __init__(self, env):
NotifyEmail.__init__(self, env)
def notify(self, editor, rendezvous, is_new=False, modtime=None):
self.editor = editor
self.rendezvous = rendezvous
self.modtime = modtime
self.is_new = is_new
changes_body = ''
changes_descr = ''
self.rendezvous.link = self.env.abs_href.rendezvous(rendezvous.rendezvous_id)
self.rendezvous_description = wrap(
self.rendezvous.description, self.COLS,
initial_indent=' ', subsequent_indent=' ', linesep=CRLF)
subject = self.format_subj("RendezVous evolved to %s")
if not is_new:
subject = 'Re: ' + subject
self.data.update({
'rendezvous_props': self.format_props(),
'rendezvous_body_hdr': self.format_hdr(),
'subject': subject,
'rendezvous': rendezvous,
'changes_body': changes_body,
'changes_descr': changes_descr
})
NotifyEmail.notify(self, rendezvous.rendezvous_id, subject)
def format_props(self):
rendezvous = self.rendezvous
text = ""
text+= "%s: %s\n" % ("Name".center(12), rendezvous.name)
text+= "%s: %s\n" % ("Author".center(12), rendezvous.author)
text+= "%s: %s\n" % ("Created on".center(12), rendezvous.time_created)
location = RendezVousLocation.fetch_one(self.env, rendezvous.location_id)
text+= "%s: %s\n" % ("Location".center(12), location and location.name or "")
text+= "%s: %s\n" % ("Coordinates".center(12), location and location.coordinate_str() or "")
return text
def get_recipients(self, rendezvous_id):
def parse_email(txt):
return filter(lambda x: '@' in x, txt.replace(',', ' ').split())
r = RendezVous.fetch_one(self.env, rendezvous_id, fetch_dates=True)
recipients = []
if r:
tmp = r.email
if tmp:
emails = parse_email(tmp)
recipients.extend(emails)
tmp = r.dates[r.elected].email
if tmp:
emails = parse_email(tmp)
recipients.extend(emails)
return recipients, []
def format_hdr(self):
return '#%s: %s' % (self.rendezvous.rendezvous_id, wrap(self.rendezvous.description,
self.COLS, linesep=CRLF))
def format_subj(self, summary):
template = self.config.get('rendezvous','rendezvous_subject_template')
template = TextTemplate(template.encode('utf8'))
prefix = self.config.get('notification', 'smtp_subject_prefix')
if prefix == '__default__':
prefix = '[%s]' % self.config.get('project', 'name')
data = {
'prefix': prefix,
'summary': summary,
'rendezvous': self.rendezvous,
'env': self.env,
}
return template.generate(**data).render('text', encoding=None).strip()
def get_message_id(self, rcpt, modtime=None):
"""Generate a predictable, but sufficiently unique message ID."""
s = '%s.%08d.%d.%s' % (self.config.get('project', 'url'),
int(self.rendezvous.rendezvous_id), to_timestamp(modtime),
rcpt.encode('ascii', 'ignore'))
dig = md5(s).hexdigest()
host = self.from_email[self.from_email.find('@') + 1:]
msgid = '<%03d.%s@%s>' % (len(s), dig, host)
return msgid
def send(self, torcpts, ccrcpts):
dest = self.editor or 'anonymous'
hdrs = {}
hdrs['Message-ID'] = self.get_message_id(dest, self.modtime)
hdrs['X-Trac-Ticket-ID'] = str(self.rendezvous.rendezvous_id)
hdrs['X-Trac-Ticket-URL'] = self.rendezvous.link
if not self.is_new:
msgid = self.get_message_id(dest)
hdrs['In-Reply-To'] = msgid
hdrs['References'] = msgid
NotifyEmail.send(self, torcpts, ccrcpts, hdrs)