From e2ad6c538586ab1f61285ef7689540efcec9634f Mon Sep 17 00:00:00 2001 From: Erich Hasl Date: Tue, 30 May 2017 22:54:18 +0200 Subject: [PATCH] change settings to read from environment variables --- jdav_web/jdav_web/settings.py | 31 ++++++++++++++++++++----------- jdav_web/mailer/mailutils.py | 9 +++++---- jdav_web/mailer/models.py | 4 ++-- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/jdav_web/jdav_web/settings.py b/jdav_web/jdav_web/settings.py index 2638402..4f8e3bc 100644 --- a/jdav_web/jdav_web/settings.py +++ b/jdav_web/jdav_web/settings.py @@ -12,6 +12,8 @@ https://docs.djangoproject.com/en/1.10/ref/settings/ import os +deployed = '1' == os.environ.get('DJANGO_DEPLOY', '0') + # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -20,17 +22,22 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = '6_ew6l1r9_4(8=p8quv(e8b+z+k+*wm7&zxx%mcnnec99a!lpw' +SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', + '6_ew6l1r9_4(8=p8quv(e8b+z+k+*wm7&zxx%mcnnec99a!lpw') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = [os.environ.get('DJANGO_ALLOWED_HOST', ''),] # Define media paths e.g. for image storage MEDIA_ROOT = os.path.join((os.path.join(BASE_DIR, os.pardir)), "media") MEDIA_URL = '/media/' +# x forward + +USE_X_FORWARDED_HOST = True + # Application definition INSTALLED_APPS = [ @@ -84,10 +91,11 @@ WSGI_APPLICATION = 'jdav_web.wsgi.application' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', - 'NAME': 'jdav_db', - 'USER': 'jdav_user', - 'PASSWORD': 'jdav00jdav', - 'HOST': 'localhost' + 'NAME': os.environ.get('DJANGO_DATABASE_NAME', 'jdav_db'), + 'OPTIONS': { + 'read_default_file': os.environ.get('DJANGO_DATABASE_CONFIG', + os.path.join(BASE_DIR, 'my.cnf')) + }, } } @@ -135,7 +143,8 @@ STATICFILES_DIRS = [ # static root where all the static files are collected to # use python3 manage.py collectstatic to collect static files in the STATIC_ROOT # this is needed for deployment -STATIC_ROOT = os.path.join((os.path.join(BASE_DIR, os.pardir)), "static") +STATIC_ROOT = os.environ.get('DJANGO_STATIC_ROOT', + os.path.join((os.path.join(BASE_DIR, os.pardir)), "static")) # Locale files (translations) @@ -146,7 +155,7 @@ LOCALE_PATHS = (os.path.join(BASE_DIR, 'locale'),) # Email setup EMAIL_HOST = 'localhost' -EMAIL_PORT = 25 -EMAIL_HOST_USER = '' -EMAIL_HOST_PASSWORD = '' -EMAIL_USE_TLS = False +EMAIL_PORT = 587 if deployed else 25 +EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER', '') +EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD', '') +EMAIL_USE_TLS = True if deployed else False diff --git a/jdav_web/mailer/mailutils.py b/jdav_web/mailer/mailutils.py index fe9df14..4564492 100644 --- a/jdav_web/mailer/mailutils.py +++ b/jdav_web/mailer/mailutils.py @@ -1,8 +1,10 @@ from django.core import mail from django.core.mail import EmailMessage +import os NOT_SENT, SENT, PARTLY_SENT = 0, 1, 2 +HOST = os.environ.get('DJANGO_ALLOWED_HOST', 'localhost:8000') def send(subject, content, sender, recipients, reply_to=None, @@ -34,7 +36,7 @@ def send(subject, content, sender, recipients, reply_to=None, def get_content(content): # TODO: generate right url here - url = "localhost:8000/newsletter/unsubscribe" + url = "https://{}/newsletter/unsubscribe".format(HOST) text = "{}\n\nDiese Email wurde über die Webseite der JDAV Ludwigsburg"\ " verschickt. Wenn du in Zukunft keine Emails mehr erhalten möchtest,"\ " kannst du hier den Newsletter deabonnieren.\n\n{}"\ @@ -44,9 +46,8 @@ def get_content(content): def get_unsubscribe_link(member): key = member.generate_key() - print("generating key for", member, key) # TODO: generate right url here - return "localhost:8000/newsletter/unsubscribe?key={}".format(key) + return "https://{}/newsletter/unsubscribe?key={}".format(HOST, key) -mail_root = "christian@localhost" +mail_root = os.environ.get('EMAIL_SENDING_ADDRESS', 'christian@localhost') diff --git a/jdav_web/mailer/models.py b/jdav_web/mailer/models.py index b1d68ee..5af5916 100644 --- a/jdav_web/mailer/models.py +++ b/jdav_web/mailer/models.py @@ -3,12 +3,12 @@ from django.core.exceptions import ValidationError from django import forms from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext -from .mailutils import send, get_content, SENT, PARTLY_SENT +from .mailutils import send, get_content, SENT, PARTLY_SENT, mail_root import os # this is the mail address that is used to send mails -SENDING_ADDRESS = "jdav-lb@gmx.de" +SENDING_ADDRESS = mail_root class RestrictedFileField(models.FileField):