From d5d4efd2dffa1629b65a335e7b545b4493d0aa4c Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Wed, 19 Aug 2020 12:13:44 +0200 Subject: [PATCH] Fix email sending --- .../commands/check_pending_comments.py | 12 +++++++---- articles/views/html.py | 3 +-- blog/settings.py | 20 +++++++++++-------- requirements.txt | 1 + 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/articles/management/commands/check_pending_comments.py b/articles/management/commands/check_pending_comments.py index 1431be8..2090c39 100644 --- a/articles/management/commands/check_pending_comments.py +++ b/articles/management/commands/check_pending_comments.py @@ -1,5 +1,7 @@ +from django.conf import settings from django.core.mail import mail_admins from django.core.management import BaseCommand +from django.urls import reverse from articles.models import Comment @@ -8,9 +10,11 @@ class Command(BaseCommand): help = "Check for pending comments and send an email to the admin." def handle(self, *args, **options): - count = Comment.objects.filter(status=Comment.PENDING) - # url = reverse("admin:articles_comment_list") - url = "" + count = Comment.objects.filter(status=Comment.PENDING).count() if count: - message = f"There are {count} comments pending[0].\n[0]: {url}" + url = reverse("admin:articles_comment_changelist") + url = (settings.BLOG["base_url"] + url).replace( + "//", "/" + ) + "?status__exact=pending" + message = f"There are {count} comments pending review.\n{url}" mail_admins("Comments pending", message) diff --git a/articles/views/html.py b/articles/views/html.py index afe6af1..07d06c2 100644 --- a/articles/views/html.py +++ b/articles/views/html.py @@ -85,8 +85,7 @@ class ArticleDetailView(FormMixin, generic.DetailView): comment.article = self.object comment.save() messages.success( - self.request, - f'Comment successfully saved, you can check it below.', + self.request, "Comment successfully saved, pending review.", ) return super().form_valid(form) diff --git a/blog/settings.py b/blog/settings.py index 9313774..08e0cb2 100644 --- a/blog/settings.py +++ b/blog/settings.py @@ -28,16 +28,19 @@ admins = os.getenv("ADMINS", "") if admins: ADMINS = list(map(lambda x: tuple(x.split(",")), admins.split(";"))) -DEFAULT_FROM_EMAIL = "blog@gabnotes.org" -SERVER_EMAIL = "blog@gabnotes.org" -EMAIL_HOST = os.getenv("EMAIL_HOST", "localhost") -EMAIL_HOST_PASSWORD = os.getenv("EMAIL_HOST_PASSWORD", "") -EMAIL_HOST_USER = os.getenv("EMAIL_HOST_USER", "") -EMAIL_PORT = 465 +DEFAULT_FROM_EMAIL = "Gab's Notes " +SERVER_EMAIL = "Gab's Notes " EMAIL_SUBJECT_PREFIX = "[Blog] " -EMAIL_USE_TLS = True EMAIL_TIMEOUT = 30 +ANYMAIL = { + "MAILGUN_API_KEY": os.getenv("MAILGUN_API_KEY", ""), + "MAILGUN_SENDER_DOMAIN": os.getenv("MAILGUN_SENDER_DOMAIN", ""), + "MAILGUN_API_URL": "https://api.eu.mailgun.net/v3", +} +EMAIL_BACKEND = "anymail.backends.mailgun.EmailBackend" + + # SECURITY WARNING: don't run with debug turned on in production! DEBUG = os.getenv("DEBUG", "true").lower() == "true" TESTING = os.getenv("TESTING", "false").lower() == "true" @@ -61,6 +64,7 @@ INSTALLED_APPS = [ "django.contrib.messages", "django.contrib.staticfiles", "articles", + "anymail", ] MIDDLEWARE = [ @@ -159,5 +163,5 @@ AUTH_USER_MODEL = "articles.User" BLOG = { "description": "My take on tech-related subjects (but not only)", - "base_url": "https://gabnotes.org/", + "base_url": os.getenv("BLOG_BASE_URL", "https://gabnotes.org/"), } diff --git a/requirements.txt b/requirements.txt index a4ff4a9..52b64f8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ django==3.1 markdown==3.2.2 gunicorn==20.0.4 Pygments==2.6.1 +django-anymail[mailgun]==7.2.1