From 1781eb28758610b826714dbb6bf34bac2295257e Mon Sep 17 00:00:00 2001 From: Sonny Bakker Date: Fri, 23 Apr 2021 22:03:30 +0200 Subject: [PATCH 1/4] Add email settings --- src/newsreader/conf/base.py | 3 ++- src/newsreader/conf/production.py | 9 +++++++++ src/newsreader/news/collection/tasks.py | 2 ++ src/newsreader/news/collection/twitter.py | 2 ++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/newsreader/conf/base.py b/src/newsreader/conf/base.py index d41f352..d6bef7f 100644 --- a/src/newsreader/conf/base.py +++ b/src/newsreader/conf/base.py @@ -193,7 +193,8 @@ STATICFILES_FINDERS = [ "django.contrib.staticfiles.finders.AppDirectoriesFinder", ] -DEFAULT_FROM_EMAIL = "newsreader@rss.fudiggity.nl" +# Email +EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" # Project settings VERSION = get_current_version() diff --git a/src/newsreader/conf/production.py b/src/newsreader/conf/production.py index f481885..bbda117 100644 --- a/src/newsreader/conf/production.py +++ b/src/newsreader/conf/production.py @@ -45,6 +45,15 @@ TEMPLATES = [ } ] +# Email +EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend" +EMAIL_HOST = os.environ.get("EMAIL_HOST") +EMAIL_PORT = os.environ.get("EMAIL_PORT") +EMAIL_HOST_USER = os.environ.get("EMAIL_HOST_USER") +EMAIL_HOST_PASSWORD = os.environ.get("EMAIL_HOST_PASSWORD") +EMAIL_USE_TLS = os.environ.get("EMAIL_USE_TLS") +EMAIL_USE_SSL = os.environ.get("EMAIL_USE_SSL") + # Reddit integration REDDIT_CLIENT_ID = os.environ.get("REDDIT_CLIENT_ID", "") REDDIT_CLIENT_SECRET = os.environ.get("REDDIT_CLIENT_SECRET", "") diff --git a/src/newsreader/news/collection/tasks.py b/src/newsreader/news/collection/tasks.py index b82bf66..9ca65f9 100644 --- a/src/newsreader/news/collection/tasks.py +++ b/src/newsreader/news/collection/tasks.py @@ -106,6 +106,8 @@ class RedditTokenTask(app.Task): user.reddit_refresh_token = None user.save() + + # TODO add notification mail return response_data = response.json() diff --git a/src/newsreader/news/collection/twitter.py b/src/newsreader/news/collection/twitter.py index e56ec13..debe31e 100644 --- a/src/newsreader/news/collection/twitter.py +++ b/src/newsreader/news/collection/twitter.py @@ -240,6 +240,8 @@ class TwitterClient(PostClient): stream.rule.user.twitter_oauth_token_secret = None stream.rule.user.save() + # TODO add notification mail + self.set_rule_error(stream.rule, e) break From 804a7ec8bb4fe82123b0a236bfa983523255ce49 Mon Sep 17 00:00:00 2001 From: Sonny Bakker Date: Fri, 23 Apr 2021 22:12:50 +0200 Subject: [PATCH 2/4] Send notification mails when re-authentication is needed --- src/newsreader/news/collection/tasks.py | 12 +++++++++++- src/newsreader/news/collection/twitter.py | 15 ++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/newsreader/news/collection/tasks.py b/src/newsreader/news/collection/tasks.py index 9ca65f9..799a101 100644 --- a/src/newsreader/news/collection/tasks.py +++ b/src/newsreader/news/collection/tasks.py @@ -1,5 +1,7 @@ from django.conf import settings from django.core.exceptions import ObjectDoesNotExist +from django.core.mail import send_mail +from django.utils.translation import ugettext as _ import requests @@ -107,7 +109,15 @@ class RedditTokenTask(app.Task): user.reddit_refresh_token = None user.save() - # TODO add notification mail + message = _( + "Your Reddit account credentials have expired. Re-authenticate in" + " the settings page to keep retrieving Reddit specific information" + " from your account." + ) + + send_mail( + "Reddit account needs re-authentication", message, None, [user.email] + ) return response_data = response.json() diff --git a/src/newsreader/news/collection/twitter.py b/src/newsreader/news/collection/twitter.py index debe31e..c854e49 100644 --- a/src/newsreader/news/collection/twitter.py +++ b/src/newsreader/news/collection/twitter.py @@ -5,8 +5,10 @@ from datetime import datetime from json import JSONDecodeError from django.conf import settings +from django.core.mail import send_mail from django.utils import timezone from django.utils.html import format_html, urlize +from django.utils.translation import ugettext as _ import pytz @@ -240,7 +242,18 @@ class TwitterClient(PostClient): stream.rule.user.twitter_oauth_token_secret = None stream.rule.user.save() - # TODO add notification mail + message = _( + "Your Twitter account credentials have expired. Re-authenticate in" + " the settings page to keep retrieving Twitter specific information" + " from your account." + ) + + send_mail( + "Twitter account needs re-authentication", + message, + None, + [stream.rule.user.email], + ) self.set_rule_error(stream.rule, e) From c1214d86cf18d141c2a635ff600b268b20eb408c Mon Sep 17 00:00:00 2001 From: Sonny Bakker Date: Fri, 23 Apr 2021 22:15:53 +0200 Subject: [PATCH 3/4] Use django email defaults --- src/newsreader/conf/production.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/newsreader/conf/production.py b/src/newsreader/conf/production.py index bbda117..ecd6c8d 100644 --- a/src/newsreader/conf/production.py +++ b/src/newsreader/conf/production.py @@ -47,12 +47,12 @@ TEMPLATES = [ # Email EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend" -EMAIL_HOST = os.environ.get("EMAIL_HOST") -EMAIL_PORT = os.environ.get("EMAIL_PORT") -EMAIL_HOST_USER = os.environ.get("EMAIL_HOST_USER") -EMAIL_HOST_PASSWORD = os.environ.get("EMAIL_HOST_PASSWORD") -EMAIL_USE_TLS = os.environ.get("EMAIL_USE_TLS") -EMAIL_USE_SSL = os.environ.get("EMAIL_USE_SSL") +EMAIL_HOST = os.environ.get("EMAIL_HOST", "localhost") +EMAIL_PORT = os.environ.get("EMAIL_PORT", 25) +EMAIL_HOST_USER = os.environ.get("EMAIL_HOST_USER", "") +EMAIL_HOST_PASSWORD = os.environ.get("EMAIL_HOST_PASSWORD", "") +EMAIL_USE_TLS = os.environ.get("EMAIL_USE_TLS", False) +EMAIL_USE_SSL = os.environ.get("EMAIL_USE_SSL", False) # Reddit integration REDDIT_CLIENT_ID = os.environ.get("REDDIT_CLIENT_ID", "") From 25e9e1d60a3fb0a28dc857a98ab705433b6eacfa Mon Sep 17 00:00:00 2001 From: Sonny Bakker Date: Fri, 23 Apr 2021 22:22:16 +0200 Subject: [PATCH 4/4] Set DEFAULT_FROM_EMAIL --- src/newsreader/conf/production.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/newsreader/conf/production.py b/src/newsreader/conf/production.py index ecd6c8d..66b6591 100644 --- a/src/newsreader/conf/production.py +++ b/src/newsreader/conf/production.py @@ -47,10 +47,14 @@ TEMPLATES = [ # Email EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend" +DEFAULT_FROM_EMAIL = os.environ.get("EMAIL_DEFAULT_FROM", "webmaster@localhost") + EMAIL_HOST = os.environ.get("EMAIL_HOST", "localhost") EMAIL_PORT = os.environ.get("EMAIL_PORT", 25) + EMAIL_HOST_USER = os.environ.get("EMAIL_HOST_USER", "") EMAIL_HOST_PASSWORD = os.environ.get("EMAIL_HOST_PASSWORD", "") + EMAIL_USE_TLS = os.environ.get("EMAIL_USE_TLS", False) EMAIL_USE_SSL = os.environ.get("EMAIL_USE_SSL", False)