From cc3e0f1fc0f699e2674b28fb796ba5c5032b0b9d Mon Sep 17 00:00:00 2001 From: Guillermo Guerrero Date: Tue, 17 Jan 2017 12:19:42 +0100 Subject: [PATCH] Added Postgresql config in production. Added Postgresql config if is present ENV => DATABASE_URL. Added migrations. Fixed path sqlite. Added MAILTRAP. --- Procfile | 1 + bin/post_compile | 10 ++++++++++ requirements.txt | 3 +++ superlists/settings.py | 28 ++++++++++++++++++++++------ superlists/wsgi.py | 3 ++- 5 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 Procfile create mode 100755 bin/post_compile diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..ae0c72c --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: gunicorn superlists.wsgi --log-file - diff --git a/bin/post_compile b/bin/post_compile new file mode 100755 index 0000000..ed62ed3 --- /dev/null +++ b/bin/post_compile @@ -0,0 +1,10 @@ +# !/usr/bin/env bash +# File path should be ./bin/post_compile +# (.sh extension added in Gist just to enable shell syntax highlighting. +# https://discussion.heroku.com/t/django-automaticlly-run-syncdb-and-migrations-after-heroku-deploy-with-a-buildpack-or-otherwise/466/7 + +echo "=> Performing database migrations..." +python manage.py migrate + +echo "=> Generating static files..." +python manage.py collectstatic --noinput diff --git a/requirements.txt b/requirements.txt index 4a7e093..13b6af2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,5 @@ django==1.10.4 gunicorn==19.6.0 +dj-database-url==0.4.1 +psycopg2>=2.6.1 +whitenoise==3.2.1 diff --git a/superlists/settings.py b/superlists/settings.py index 4107456..7a7d194 100644 --- a/superlists/settings.py +++ b/superlists/settings.py @@ -11,6 +11,7 @@ """ import os +import dj_database_url # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -25,7 +26,7 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = ['localhost', 'testing-example.herokuapp.com'] # Application definition @@ -84,11 +85,22 @@ DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, '../database/db.sqlite3'), + 'NAME': os.path.join(BASE_DIR, './database/db.sqlite3'), } } +if os.getenv('DATABASE_URL', '') != '': + # Update database configuration with $DATABASE_URL. + db_from_env = dj_database_url.config() + DATABASES['default'].update(db_from_env) + +# Simplified static file serving. +# https://warehouse.python.org/project/whitenoise/ + +STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' + + # Password validation # https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators @@ -145,8 +157,12 @@ 'root': {'level': 'INFO'}, } -EMAIL_HOST = 'smtp.gmail.com' -EMAIL_HOST_USER = 'obeythetestinggoat@gmail.com' -EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASSWORD') -EMAIL_USE_TLS = True +if os.getenv('MAILTRAP_ENABLED', '') != '': + EMAIL_HOST = 'mailtrap.io' + EMAIL_HOST_USER = os.getenv('MAILTRAP_USER', '') + EMAIL_HOST_PASSWORD = os.getenv('MAILTRAP_PASSWORD', '') + EMAIL_PORT = '2525' +else: + # It writes e-mails to standard out instead of sending them + EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' diff --git a/superlists/wsgi.py b/superlists/wsgi.py index 6a65856..3198c51 100644 --- a/superlists/wsgi.py +++ b/superlists/wsgi.py @@ -10,7 +10,8 @@ import os from django.core.wsgi import get_wsgi_application +from whitenoise.django import DjangoWhiteNoise os.environ.setdefault("DJANGO_SETTINGS_MODULE", "superlists.settings") -application = get_wsgi_application() +application = DjangoWhiteNoise(get_wsgi_application())