...
 
Commits (2)
  • guglielmo's avatar
    git ignores all .dcenv files · 9f69b5b4
    guglielmo authored
    docker image renamed and aligned between docker-compose.yml and .gitlab-ci.yml; uwsgi invocation corrected
    
    constance requirements adjusted
    
    taskmanager urls added
    9f69b5b4
  • guglielmo's avatar
    notification system improved: · 1a33d60e
    guglielmo authored
    - 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)
    1a33d60e
......@@ -5,7 +5,7 @@ __pycache__
# Env file
/config/.env
/config/.dcenv
/config/.dcenv*
# Unit test / coverage reports
.coverage
......
......@@ -105,7 +105,7 @@ deploy_staging:
- echo "$CA" > $DOCKER_CERT_PATH/ca.pem
- echo "$CLIENT_CERT" > $DOCKER_CERT_PATH/cert.pem
- echo "$CLIENT_KEY" > $DOCKER_CERT_PATH/key.pem
- docker build --compress -t openpolis/sources-verification:latest .
- docker build --compress -t openpolis/opsv:latest .
- docker-compose down
- docker-compose up -d --build
- docker exec opsv_web python manage.py collectstatic --noinput
......
......@@ -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
......
......@@ -12,7 +12,8 @@ urls = [
url(r'^favicon\.ico$', RedirectView.as_view(url='/static/images/favicon.ico')),
url(r'^admin/', admin.site.urls),
url(r'^diff/(?P<content_id>\d+)$', diff, name='diff'),
url(r'^signal/(?P<content_id>\d+)$', signal, name='signal')
url(r'^signal/(?P<content_id>\d+)$', signal, name='signal'),
url(r"^taskmanager/", include("taskmanager.urls")),
]
urlpatterns = urls
......
......@@ -5,7 +5,7 @@ services:
web:
restart: always
container_name: opsv_web
image: openpolis/sources-verification
image: openpolis/opsv
expose:
- "8000"
links:
......@@ -30,7 +30,7 @@ services:
volumes:
- public:/app/public
- weblogs:/var/log
command: /usr/local/bin/uwsgi --socket=:8000 --logto=/var/log/uwsgi.log --env DJANGO_SETTINGS_MODULE=config.settings --pythonpath=/app --module=wsgi --callable=application --processes=4
command: /usr/local/bin/uwsgi --socket=:8000 --master --logto=/var/log/uwsgi.log --env DJANGO_SETTINGS_MODULE=config.settings --pythonpath=/app --module=wsgi --callable=application --processes=4 --spooler=/var/lib/uwsgi --spooler-processes=2
nginx:
restart: always
......
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))
......
......@@ -12,7 +12,6 @@ django-object-actions
django-admin-row-actions
# live settings
django-constance==2.4.0
django-constance[database]
# slack notifications
......