Fix email sending

This commit is contained in:
Gabriel Augendre 2020-08-19 12:13:44 +02:00
parent 6f4ec418ee
commit d5d4efd2df
4 changed files with 22 additions and 14 deletions

View file

@ -1,5 +1,7 @@
from django.conf import settings
from django.core.mail import mail_admins from django.core.mail import mail_admins
from django.core.management import BaseCommand from django.core.management import BaseCommand
from django.urls import reverse
from articles.models import Comment from articles.models import Comment
@ -8,9 +10,11 @@ class Command(BaseCommand):
help = "Check for pending comments and send an email to the admin." help = "Check for pending comments and send an email to the admin."
def handle(self, *args, **options): def handle(self, *args, **options):
count = Comment.objects.filter(status=Comment.PENDING) count = Comment.objects.filter(status=Comment.PENDING).count()
# url = reverse("admin:articles_comment_list")
url = ""
if 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) mail_admins("Comments pending", message)

View file

@ -85,8 +85,7 @@ class ArticleDetailView(FormMixin, generic.DetailView):
comment.article = self.object comment.article = self.object
comment.save() comment.save()
messages.success( messages.success(
self.request, self.request, "Comment successfully saved, pending review.",
f'Comment successfully saved, you can check it <a href="#{comment.id}">below</a>.',
) )
return super().form_valid(form) return super().form_valid(form)

View file

@ -28,16 +28,19 @@ admins = os.getenv("ADMINS", "")
if admins: if admins:
ADMINS = list(map(lambda x: tuple(x.split(",")), admins.split(";"))) ADMINS = list(map(lambda x: tuple(x.split(",")), admins.split(";")))
DEFAULT_FROM_EMAIL = "blog@gabnotes.org" DEFAULT_FROM_EMAIL = "Gab's Notes <blog@mg.gabnotes.org>"
SERVER_EMAIL = "blog@gabnotes.org" SERVER_EMAIL = "Gab's Notes <blog@mg.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
EMAIL_SUBJECT_PREFIX = "[Blog] " EMAIL_SUBJECT_PREFIX = "[Blog] "
EMAIL_USE_TLS = True
EMAIL_TIMEOUT = 30 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! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.getenv("DEBUG", "true").lower() == "true" DEBUG = os.getenv("DEBUG", "true").lower() == "true"
TESTING = os.getenv("TESTING", "false").lower() == "true" TESTING = os.getenv("TESTING", "false").lower() == "true"
@ -61,6 +64,7 @@ INSTALLED_APPS = [
"django.contrib.messages", "django.contrib.messages",
"django.contrib.staticfiles", "django.contrib.staticfiles",
"articles", "articles",
"anymail",
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@ -159,5 +163,5 @@ AUTH_USER_MODEL = "articles.User"
BLOG = { BLOG = {
"description": "My take on tech-related subjects (but not only)", "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/"),
} }

View file

@ -2,3 +2,4 @@ django==3.1
markdown==3.2.2 markdown==3.2.2
gunicorn==20.0.4 gunicorn==20.0.4
Pygments==2.6.1 Pygments==2.6.1
django-anymail[mailgun]==7.2.1