From 7220a90873d208453e3ece3949c5aae6f64ee379 Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Wed, 5 Jul 2023 11:56:28 +0200 Subject: [PATCH] configure for postgres --- CTOAsYouGo/CTOAsYouGo/settings.py | 140 ++++++++++++++++++++++++++---- requirements.txt | 1 + 2 files changed, 126 insertions(+), 15 deletions(-) diff --git a/CTOAsYouGo/CTOAsYouGo/settings.py b/CTOAsYouGo/CTOAsYouGo/settings.py index b0e7315..7bd20ac 100644 --- a/CTOAsYouGo/CTOAsYouGo/settings.py +++ b/CTOAsYouGo/CTOAsYouGo/settings.py @@ -1,7 +1,7 @@ """ -Django settings for CTOAsYouGo project. +Django settings for gettingstarted project. -Generated by 'django-admin startproject' using Django 4.2.2. +Generated by 'django-admin startproject' using Django 4.2.1. For more information on this file, see https://docs.djangoproject.com/en/4.2/topics/settings/ @@ -10,26 +10,50 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.2/ref/settings/ """ -from pathlib import Path import os +import secrets +from pathlib import Path + +import dj_database_url # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent -# Quick-start development settings - unsuitable for production +# Before using your Heroku app in production, make sure to review Django's deployment checklist: # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ +# Django requires a unique secret key for each Django app, that is used by several of its +# security features. To simplify initial setup (without hardcoding the secret in the source +# code) we set this to a random value every time the app starts. However, this will mean many +# Django features break whenever an app restarts (for example, sessions will be logged out). +# In your production Heroku apps you should set the `DJANGO_SECRET_KEY` config var explicitly. +# Make sure to use a long unique value, like you would for a password. See: +# https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-SECRET_KEY +# https://devcenter.heroku.com/articles/config-vars # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-d8gmbk-g2&ue)k_74169!l@oiue8ra%@d4qm@jc=3&%&*%(oah' +SECRET_KEY = os.environ.get( + "DJANGO_SECRET_KEY", + default=secrets.token_urlsafe(nbytes=64), +) + +# The `DYNO` env var is set on Heroku CI, but it's not a real Heroku app, so we have to +# also explicitly exclude CI: +# https://devcenter.heroku.com/articles/heroku-ci#immutable-environment-variables +IS_HEROKU_APP = "DYNO" in os.environ and not "CI" in os.environ # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +if not IS_HEROKU_APP: + DEBUG = True -ALLOWED_HOSTS = ['localhost', '127.0.0.1', 'ctoasyougo.com', 'www.ctoasyougo.com','ctoasyougo-3015a1b209e7.herokuapp.com'] - - -# Application definition +# On Heroku, it's safe to use a wildcard for `ALLOWED_HOSTS``, since the Heroku router performs +# validation of the Host header in the incoming HTTP request. On other platforms you may need +# to list the expected hostnames explicitly to prevent HTTP Host header attacks. See: +# https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-ALLOWED_HOSTS +if IS_HEROKU_APP: + ALLOWED_HOSTS = ["*"] +else: + ALLOWED_HOSTS = [] INSTALLED_APPS = [ 'django.contrib.admin', @@ -43,6 +67,97 @@ INSTALLED_APPS = [ 'widget_tweaks', ] + +WSGI_APPLICATION = 'CTOAsYouGo.wsgi.application' + +# Database +# https://docs.djangoproject.com/en/4.2/ref/settings/#databases + +if IS_HEROKU_APP: + # In production on Heroku the database configuration is derived from the `DATABASE_URL` + # environment variable by the dj-database-url package. `DATABASE_URL` will be set + # automatically by Heroku when a database addon is attached to your Heroku app. See: + # https://devcenter.heroku.com/articles/provisioning-heroku-postgres + # https://github.com/jazzband/dj-database-url + DATABASES = { + "default": dj_database_url.config( + conn_max_age=600, + conn_health_checks=True, + ssl_require=True, + ), + } +else: + # When running locally in development or in CI, a sqlite database file will be used instead + # to simplify initial setup. Longer term it's recommended to use Postgres locally too. + DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": BASE_DIR / "db.sqlite3", + } + } + + +# Password validation +# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/4.2/topics/i18n/ + +LANGUAGE_CODE = "en-us" + +TIME_ZONE = "UTC" + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.2/howto/static-files/ + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.2/howto/static-files/ +STATIC_ROOT = BASE_DIR / 'static' +STATIC_URL = '/static/' + + +STORAGES = { + # Enable WhiteNoise's GZip and Brotli compression of static assets: + # https://whitenoise.readthedocs.io/en/latest/django.html#add-compression-and-caching-support + "staticfiles": { + "BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage", + }, +} + +# Don't store the original (un-hashed filename) version of static files, to reduce slug size: +# https://whitenoise.readthedocs.io/en/latest/django.html#WHITENOISE_KEEP_ONLY_HASHED_FILES +WHITENOISE_KEEP_ONLY_HASHED_FILES = True + + +# Default primary key field type +# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" + + +# Application definition + MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', @@ -117,11 +232,6 @@ USE_I18N = True USE_TZ = True -# Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/4.2/howto/static-files/ -STATIC_ROOT = BASE_DIR / 'static' -STATIC_URL = '/static/' - # Default primary key field type # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field diff --git a/requirements.txt b/requirements.txt index 53e6350..0ba9233 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ asgiref==3.7.2 +dj-database-url==2.0.0 Django==4.2.2 django-appconf==1.0.5 django-compressor==4.3.1