-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Custom PINAX Email Backend: allows html notifications
- Loading branch information
geosolutions
committed
May 21, 2019
1 parent
ef78c6b
commit dad94b2
Showing
2 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# -*- coding: utf-8 -*- | ||
######################################################################### | ||
# | ||
# Copyright (C) 2017 OSGeo | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
######################################################################### | ||
from django.conf import settings | ||
from django.core.mail import EmailMessage | ||
from django.template.loader import render_to_string | ||
from django.utils.translation import ugettext | ||
|
||
from pinax.notifications.backends.base import BaseBackend | ||
|
||
|
||
class EmailBackend(BaseBackend): | ||
spam_sensitivity = 2 | ||
|
||
def can_send(self, user, notice_type, scoping): | ||
can_send = super(EmailBackend, self).can_send(user, notice_type, scoping) | ||
if can_send and user.email: | ||
return True | ||
return False | ||
|
||
def deliver(self, recipient, sender, notice_type, extra_context): | ||
# TODO: require this to be passed in extra_context | ||
|
||
context = self.default_context() | ||
context.update({ | ||
"recipient": recipient, | ||
"sender": sender, | ||
"notice": ugettext(notice_type.display), | ||
}) | ||
context.update(extra_context) | ||
|
||
messages = self.get_formatted_messages(( | ||
"short.txt", | ||
"full.txt" | ||
), notice_type.label, context) | ||
|
||
context.update({ | ||
"message": messages["short.txt"], | ||
}) | ||
subject = "".join(render_to_string("pinax/notifications/email_subject.txt", context).splitlines()) | ||
|
||
context.update({ | ||
"message": messages["full.txt"] | ||
}) | ||
body = render_to_string("pinax/notifications/email_body.txt", context) | ||
print(body) | ||
print(settings.DEFAULT_FROM_EMAIL) | ||
email = EmailMessage( | ||
subject=subject, | ||
body=body, | ||
from_email=settings.DEFAULT_FROM_EMAIL, | ||
to=[recipient.email, ], | ||
reply_to=[settings.DEFAULT_FROM_EMAIL, ]) | ||
email.content_subtype = "html" | ||
email.send() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters