diff --git a/Dockerfile b/Dockerfile index 7aea5989..3eec661f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -72,6 +72,7 @@ ENV WORKERS="2" ENV CELERY_WORKERS="2" ENV GOOGLE_TRACKING_ID="" ENV AZURE_APPINSIGHTS_IKEY="" +ENV DJANGO_SETTINGS_MODULE="config.settings.production" USER doccano EXPOSE ${PORT} diff --git a/app.json b/app.json index 71a7e76f..fcdf3524 100644 --- a/app.json +++ b/app.json @@ -25,15 +25,10 @@ "required": false, "value": "" }, - "DEBUG": { - "description": "Debug mode or not.", - "required": false, - "value": "False" - }, - "ALLOW_SIGNUP": { - "description": "Allow users to signup themselves or not", - "required": false, - "value": "True" + "DJANGO_SETTINGS_MODULE": { + "description": "Django Settings Module. You don't have to change this.", + "required": true, + "value": "config.settings.production" } }, "stack": "container", diff --git a/backend/api/tests/test_config.py b/backend/api/tests/test_config.py index 2f7bd3ba..aa73dece 100644 --- a/backend/api/tests/test_config.py +++ b/backend/api/tests/test_config.py @@ -2,10 +2,9 @@ from contextlib import contextmanager from importlib import reload from os import environ +from config.settings import base as settings from django.test import TestCase -from config import settings - class TestDatabaseUrl(TestCase): def test_sslmode_defaults_to_required(self): diff --git a/backend/cli.py b/backend/cli.py index d1c97bf7..9c91e621 100644 --- a/backend/cli.py +++ b/backend/cli.py @@ -7,8 +7,8 @@ import sys from .config.celery import app -os.environ["DEBUG"] = "False" os.environ["STANDALONE"] = "True" +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production") base = os.path.abspath(os.path.dirname(__file__)) sys.path.append(base) manage_path = os.path.join(base, "manage.py") diff --git a/backend/config/celery.py b/backend/config/celery.py index 7c25aa9e..7a53a290 100644 --- a/backend/config/celery.py +++ b/backend/config/celery.py @@ -2,7 +2,7 @@ import os from celery import Celery -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings") +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.development") app = Celery("config") diff --git a/backend/config/settings/__init__.py b/backend/config/settings/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/backend/config/settings.py b/backend/config/settings/base.py similarity index 93% rename from backend/config/settings.py rename to backend/config/settings/base.py index 60811f40..1a095d59 100644 --- a/backend/config/settings.py +++ b/backend/config/settings/base.py @@ -10,8 +10,6 @@ https://docs.djangoproject.com/en/2.0/ref/settings/ Any setting that is configured via an environment variable may also be set in a `.env` file in the project base directory. """ -import importlib.util -import sys from os import path import dj_database_url @@ -19,7 +17,7 @@ from environs import Env, EnvError from furl import furl # Build paths inside the project like this: path.join(BASE_DIR, ...) -BASE_DIR = path.dirname(path.dirname(path.abspath(__file__))) +BASE_DIR = path.dirname(path.dirname(path.dirname(path.abspath(__file__)))) env = Env() env.read_env(path.join(BASE_DIR, ".env"), recurse=False) @@ -80,8 +78,7 @@ MIDDLEWARE = [ "django.middleware.clickjacking.XFrameOptionsMiddleware", "corsheaders.middleware.CorsMiddleware", ] -if DEBUG: - MIDDLEWARE.append("api.middleware.RangesMiddleware") + ROOT_URLCONF = "config.urls" WSGI_APPLICATION = "config.wsgi.application" @@ -184,16 +181,6 @@ LOGIN_URL = "/login/" LOGIN_REDIRECT_URL = "/projects/" LOGOUT_REDIRECT_URL = "/" -# dynamic import to avoid installing psycopg2 on pip installation. -name = "django_heroku" -spec = importlib.util.find_spec(name) -if spec is not None: - module = importlib.util.module_from_spec(spec) - sys.modules[name] = module - spec.loader.exec_module(module) - module.settings(locals(), test_runner=False) - - # Database # https://docs.djangoproject.com/en/2.0/ref/settings/#databases DATABASES = { @@ -250,9 +237,6 @@ if not EMAIL_HOST: EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" -if DEBUG: - CORS_ORIGIN_WHITELIST = ("http://127.0.0.1:3000", "http://0.0.0.0:3000", "http://localhost:3000") - # User media files MEDIA_ROOT = path.join(BASE_DIR, "media") MEDIA_URL = "/media/" diff --git a/backend/config/settings/development.py b/backend/config/settings/development.py new file mode 100644 index 00000000..57acffce --- /dev/null +++ b/backend/config/settings/development.py @@ -0,0 +1,4 @@ +from .base import * # noqa: F403 + +MIDDLEWARE.append("api.middleware.RangesMiddleware") # noqa: F405 +CORS_ORIGIN_WHITELIST = ("http://127.0.0.1:3000", "http://0.0.0.0:3000", "http://localhost:3000") diff --git a/backend/config/settings/heroku.py b/backend/config/settings/heroku.py new file mode 100644 index 00000000..080fac7d --- /dev/null +++ b/backend/config/settings/heroku.py @@ -0,0 +1,5 @@ +import django_heroku + +from .base import * # noqa: F401,F403 + +django_heroku.settings(locals(), test_runner=False) diff --git a/backend/config/settings/production.py b/backend/config/settings/production.py new file mode 100644 index 00000000..255a475e --- /dev/null +++ b/backend/config/settings/production.py @@ -0,0 +1,3 @@ +from .base import * # noqa: F401,F403 + +DEBUG = False diff --git a/backend/config/wsgi.py b/backend/config/wsgi.py index e4a1b25a..2946a143 100644 --- a/backend/config/wsgi.py +++ b/backend/config/wsgi.py @@ -11,6 +11,6 @@ import os from django.core.wsgi import get_wsgi_application -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings") +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production") application = get_wsgi_application() diff --git a/backend/manage.py b/backend/manage.py index 312127d5..04e1173b 100755 --- a/backend/manage.py +++ b/backend/manage.py @@ -3,7 +3,7 @@ import os import sys if __name__ == "__main__": - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings") + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.development") try: from django.core.management import execute_from_command_line except ImportError as exc: diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 3edc4133..0896daa4 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -17,6 +17,7 @@ services: DATABASE_URL: "postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}?sslmode=disable" ALLOW_SIGNUP: "False" DEBUG: "True" + DJANGO_SETTINGS_MODULE: "config.settings.development" ports: - 8000:8000 depends_on: @@ -38,6 +39,7 @@ services: PYTHONUNBUFFERED: "1" CELERY_BROKER_URL: "amqp://${RABBITMQ_DEFAULT_USER}:${RABBITMQ_DEFAULT_PASS}@rabbitmq" DATABASE_URL: "postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}?sslmode=disable" + DJANGO_SETTINGS_MODULE: "config.settings.development" depends_on: - postgres - rabbitmq diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 65e173a3..17846134 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -17,6 +17,7 @@ services: DATABASE_URL: "postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}?sslmode=disable" ALLOW_SIGNUP: "False" DEBUG: "False" + DJANGO_SETTINGS_MODULE: "config.settings.production" depends_on: - postgres networks: @@ -35,6 +36,7 @@ services: PYTHONUNBUFFERED: "1" CELERY_BROKER_URL: "amqp://${RABBITMQ_DEFAULT_USER}:${RABBITMQ_DEFAULT_PASS}@rabbitmq" DATABASE_URL: "postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}?sslmode=disable" + DJANGO_SETTINGS_MODULE: "config.settings.production" depends_on: - postgres - rabbitmq