From 266b9ffe425cf5f71d77e22bd26ee09b8b9cf6b6 Mon Sep 17 00:00:00 2001 From: Sarah Withee <2601974+geekygirlsarah@users.noreply.github.com> Date: Mon, 9 Oct 2023 19:04:32 -0400 Subject: [PATCH 1/3] Set environment variables for error reporting --- codethesaurus/settings.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/codethesaurus/settings.py b/codethesaurus/settings.py index ab498e543..ac79008c1 100644 --- a/codethesaurus/settings.py +++ b/codethesaurus/settings.py @@ -55,7 +55,8 @@ 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware' + 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'django.middleware.common.BrokenLinkEmailsMiddleware' ] ROOT_URLCONF = 'codethesaurus.urls' @@ -147,5 +148,15 @@ os.path.join(BASE_DIR, 'static'), ] +# For server error reporting +ADMINS = os.getenv('ADMINS', '') +EMAIL_HOST = os.getenv('EMAIL_HOST', '') +EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER', '') +EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD', '') +SERVER_EMAIL = os.getenv('SERVER_EMAIL', '') +EMAIL_USE_SSL = os.getenv('EMAIL_USE_SSL', '') +EMAIL_PORT = os.getenv('EMAIL_PORT', '') +EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS', '') + # Configure Django App for Heroku. django_on_heroku.settings(locals(), test_runner=False, databases=False, staticfiles=True, logging=True) From 85d6d72c9a68c41c0b634841a26a8c564b02e42e Mon Sep 17 00:00:00 2001 From: Sarah Withee <2601974+geekygirlsarah@users.noreply.github.com> Date: Mon, 9 Oct 2023 19:28:27 -0400 Subject: [PATCH 2/3] Add admin email handler --- codethesaurus/settings.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/codethesaurus/settings.py b/codethesaurus/settings.py index ac79008c1..8f51b5356 100644 --- a/codethesaurus/settings.py +++ b/codethesaurus/settings.py @@ -158,5 +158,17 @@ EMAIL_PORT = os.getenv('EMAIL_PORT', '') EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS', '') +LOGGING = { + "version": 1, # the dictConfig format version + "disable_existing_loggers": False, # retain the default loggers + "handlers": { + "mail_admins": { + "level": "ERROR", + "class": "django.utils.log.AdminEmailHandler", + "include_html": True, + }, + } +} + # Configure Django App for Heroku. django_on_heroku.settings(locals(), test_runner=False, databases=False, staticfiles=True, logging=True) From d716833d8be025cb45096b608d94b9daa6e66d43 Mon Sep 17 00:00:00 2001 From: Sarah Withee <2601974+geekygirlsarah@users.noreply.github.com> Date: Mon, 16 Oct 2023 19:18:22 -0400 Subject: [PATCH 3/3] Add parser for ADMINS env variable --- codethesaurus/settings.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/codethesaurus/settings.py b/codethesaurus/settings.py index 8f51b5356..2ce80463c 100644 --- a/codethesaurus/settings.py +++ b/codethesaurus/settings.py @@ -9,13 +9,11 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/3.1/ref/settings/ """ - from pathlib import Path import os import dj_database_url import django_on_heroku - # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -33,7 +31,7 @@ # redirect all http requests to https in non debugging envs SECURE_SSL_REDIRECT = not DEBUG -ALLOWED_HOSTS = [ "*" ] +ALLOWED_HOSTS = ["*"] # Application definition @@ -149,7 +147,16 @@ ] # For server error reporting -ADMINS = os.getenv('ADMINS', '') +# Admin emails are stored like Name1:email1@test.com,Name2:email2@test.com,... +# Parse that first then set the rest +ADMIN_STRING = os.getenv('ADMINS', '') +ADMIN_SPLIT = ADMIN_STRING.split(",") # gives us name/value list +ADMINS = [] +for item in ADMIN_SPLIT: + if item == "": + break + contact = item.split(":") + ADMINS.append((contact[0], contact[1])) EMAIL_HOST = os.getenv('EMAIL_HOST', '') EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER', '') EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD', '')