Skip to content

Commit

Permalink
- Custom PINAX Email Backend: allows html notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
geosolutions committed May 21, 2019
1 parent ef78c6b commit dad94b2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
71 changes: 71 additions & 0 deletions geonode/notifications_backend.py
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()
2 changes: 1 addition & 1 deletion geonode/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@
#PINAX_NOTIFICATIONS_LANGUAGE_MODEL = "people.Profile"

# notifications backends
_EMAIL_BACKEND = "pinax.notifications.backends.email.EmailBackend"
_EMAIL_BACKEND = "geonode.notifications_backend.EmailBackend"
PINAX_NOTIFICATIONS_BACKENDS = [
("email", _EMAIL_BACKEND),
]
Expand Down

0 comments on commit dad94b2

Please sign in to comment.