diff --git a/issue/notifications.py b/issue/notifications.py new file mode 100644 index 0000000..4cd92c0 --- /dev/null +++ b/issue/notifications.py @@ -0,0 +1,89 @@ +from django.core.mail import send_mass_mail +from django.template.loader import render_to_string +from django.core.urlresolvers import reverse +from django.conf import settings + +from issue.models import * + + +def notify_new_issue(issue): + + project = issue.project + + dests = project.subscribers.all().distinct() + + if hasattr(settings, 'FROM_ADDR'): + from_addr = settings.FROM_ADDR + else: + return + + subject = "[PonyTracker] New issue: %s (%s)" %(issue.title, project.name) + + data = [] + + for dest in dests: + + if dest == issue.author: + continue + + dest_addr = dest.email + if not dest_addr: + continue + + c = { + 'dest': dest.username, + 'author': issue.author.username, + 'title': issue.title, + 'description:': issue.description, + 'uri': settings.BASE_URL \ + + reverse('show-issue', args=[project.name, issue.id]), + } + + message = render_to_string('emails/new_issue.html', c) + + data += [(subject, message, from_addr, [dest_addr])] + + send_mass_mail(tuple(data)) + + +def notify_new_comment(event): + + issue = event.issue + project = issue.project + + dests = issue.subscribers.all() + dests |= project.subscribers.all() + dests = dests.distinct() + + if hasattr(settings, 'FROM_ADDR'): + from_addr = settings.FROM_ADDR + else: + return + + subject = "[PonyTracker] New comment - %s (%s)" %(issue.title, project.name) + + data = [] + + for dest in dests: + + if dest == event.author: + continue + + dest_addr = dest.email + if not dest_addr: + continue + + c = { + 'dest': dest.username, + 'author': event.author.username, + 'title': issue.title, + 'comment': event.additionnal_section, + 'uri': settings.BASE_URL \ + + reverse('show-issue', args=[project.name, issue.id]), + } + + message = render_to_string('emails/new_comment.html', c) + + data += [(subject, message, from_addr, [dest_addr])] + + send_mass_mail(tuple(data)) diff --git a/issue/templates/emails/new_comment.html b/issue/templates/emails/new_comment.html new file mode 100644 index 0000000..cf1a870 --- /dev/null +++ b/issue/templates/emails/new_comment.html @@ -0,0 +1,12 @@ +Dear {{ dest }}, + +{{ author }} commented the issue '{{ title }}': + + {{ comment }} + +You can respond by following this link: + + {{ uri }} + +Sincerly, +PonyTracker diff --git a/issue/templates/emails/new_issue.html b/issue/templates/emails/new_issue.html new file mode 100644 index 0000000..53c6551 --- /dev/null +++ b/issue/templates/emails/new_issue.html @@ -0,0 +1,12 @@ +Dear {{ dest }}, + +{{ author }} added a new issue: '{{ title }}' +{% if description %} + {{ description }} +{% endif %} +You can coment it by following this link: + + {{ uri }} + +Sincerly, +PonyTracker diff --git a/issue/views.py b/issue/views.py index 47f140b..8617fd6 100644 --- a/issue/views.py +++ b/issue/views.py @@ -7,6 +7,7 @@ from django.views.decorators.http import require_http_methods from issue.models import * from issue.forms import * +from issue.notifications import * from issue.decorators import project_perm_required import shlex @@ -405,6 +406,7 @@ def issue_edit(request, project, issue=None): issue.save() issue.subscribers.add(author) issue.description = description + notify_new_issue(issue) messages.success(request, 'Issue created successfully.') return redirect('show-issue', project.name, issue.id) @@ -480,6 +482,7 @@ def issue_edit_comment(request, project, issue, comment=None): code=Event.COMMENT, additionnal_section=comment) event.save() issue.subscribers.add(author) + notify_new_comment(event) messages.success(request, 'Comment added successfully.') return redirect('show-issue', project.name, issue.id) diff --git a/ponytracker/settings.py b/ponytracker/settings.py index 60720dd..bb23d4c 100644 --- a/ponytracker/settings.py +++ b/ponytracker/settings.py @@ -123,3 +123,9 @@ AUTHENTICATION_BACKENDS = ( ) SITE_ID = 1 + +EMAIL_HOST = 'smtp' + +#FROM_ADDR = 'ponytracker@example.com' + +BASE_URL = 'http://localhost:8000'