Commit 1a33d60e authored by guglielmo's avatar guglielmo

notification system improved:

- both email and slack can be used at the same time;
- all SMTP configurations are tunable with constance
- slack configurations are tunable with constance
- recipients are in bcc
- from address is configurable in constance
live settings available for notification systems (email and slack)
parent 9f69b5b4
......@@ -282,9 +282,15 @@ TEST_RUNNER = "django.test.runner.DiscoverRunner"
# CONSTANCE (LIVE SETTINGS) CONFIGURATION
# -----------------------------------------------------------------------------
CONSTANCE_CONFIG = {
'USE_SLACK': (False, "Enable notifications to slack channel. If False, then email is used", bool),
'SLACK_TOKEN': ("", "The token for slack integration, as read from slack"),
'SLACK_CHANNEL': ("", "The channel used for automatic notifications"),
'SLACK_TOKEN': (env("SLACK_TOKEN", default=""), "The token for slack integration, as read from slack"),
'SLACK_CHANNEL': (env("SLACK_CHANNEL", default=""), "The channel used for automatic notifications"),
'SMTP_HOST': (env("EMAIL_HOST", default=""), "Host of the SMTP server"),
'SMTP_PORT': (env.int("EMAIL_PORT", default=587), "Port of the SMTP server", int),
'SMTP_USE_TLS': (env.bool("EMAIL_USE_TLS", default=True), "If the SMTP server uses TLS", bool),
'SMTP_USERNAME': (env("EMAIL_HOST_USER", default=""), "Username for the SMTP host"),
'SMTP_PASSWORD': (env("EMAIL_HOST_PASS", default=""), "Password for the SMTP host"),
'EMAIL_SUBJECT_PREFIX': (env("EMAIL_SUBJECT_PREFIX", default="[servizio verificafonti] "), "Email subject prefix"),
'EMAIL_FROM': (env("DEFAULT_FROM_EMAIL", default="noreply@openpolis.it"), "From email address for notifications")
}
CONSTANCE_IGNORE_ADMIN_VERSION_CHECK = True
......
import slack
from django.contrib.sites.models import Site
from django.core.mail import EmailMultiAlternatives
from django.conf import settings
from constance import config
from django.core.mail.backends.smtp import EmailBackend
from taskmanager.logging_utils import LoggingBaseCommand
from project.webapp.models import Content, Recipient
......@@ -20,10 +20,10 @@ class Command(LoggingBaseCommand):
help='Execute a dry run: no db is written.'
)
parser.add_argument(
'--notification-method',
dest='notification_method',
"--notification-method",
dest="notification_method",
default="slack",
help='How notifications are sent.'
help="How notifications are sent. [slack|email|both]"
)
def handle(self, *args, **options):
......@@ -51,7 +51,10 @@ class Command(LoggingBaseCommand):
if options['notification_method'] == 'slack':
self.handle_slack(context=context, summary_txt=summary_txt, **options)
elif options['notification_method'] == 'email':
self.handle_email(context=context, summary_txt=summary_txt, **options)
else:
self.handle_slack(context=context, summary_txt=summary_txt, **options)
self.handle_email(context=context, summary_txt=summary_txt, **options)
def handle_slack(self, **kwargs):
......@@ -104,32 +107,6 @@ class Command(LoggingBaseCommand):
"""
context = kwargs.get('context')
# if modified_contents.count():
# msg_txt += "Cambiamenti:\n"
# msg_html += "Questo l'elenco dei siti cambiati: <br/><ul " \
# "style=\"list-style-type:none\">"
# for content in modified_contents:
# msg_txt += " - {0.title}\n".format(content)
# msg_html += """
# <li>
# <a href="http://{1}/sitescheck/content/{0.id}">
# {0.title}
# </a>
# - <a href="http://{1}/diff/{0.id}">diff</a>
# </li>
# """.format(content, domain)
# msg_html += "</ul>"
#
# if failed_contents.count():
# msg_txt += "Errori:\n"
# msg_html += "Questo l'elenco dei siti con errori: <br/><ul style=\"list-style-type:none\">"
# for content in failed_contents:
# msg_txt += " - {0.title}\n".format(content)
# msg_html += """
# <li><a href="{0.url}">{0.title}</a></li>
# """.format(content)
# msg_html += "</ul>"
#
if context['n_modified_contents'] + context['n_failed_contents'] > 0:
msg_txt = kwargs.get('summary_txt')
msg_html = \
......@@ -146,10 +123,24 @@ class Command(LoggingBaseCommand):
if kwargs.get('dryrun', False) is False:
try:
subject = '[Openpolis] Cambiamento nei siti sotto controllo!'
msg = EmailMultiAlternatives(subject, msg_txt, settings.DEFAULT_FROM_EMAIL, recipients)
msg.attach_alternative(msg_html, "text/html")
msg.send()
smtp_backend = EmailBackend(
host=config.SMTP_HOST,
port=config.SMTP_PORT,
use_tls=config.SMTP_USE_TLS,
username=config.SMTP_USERNAME,
password=config.SMTP_PASSWORD,
)
# smtp_connection = mail.get_connection(backend=smtp_backend)
subject = config.EMAIL_SUBJECT_PREFIX
with smtp_backend as smtp_connection:
msg = EmailMultiAlternatives(
subject, msg_txt,
from_email=config.EMAIL_FROM,
bcc=recipients,
connection=smtp_connection
)
msg.attach_alternative(msg_html, "text/html")
msg.send()
self.logger.info("Emails sent.")
except Exception as e:
self.logger.error("error sending email: {0}".format(e))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment