<reponame>webquills/webquills<filename>webquills/settings.py
"""
Django settings for worktracker project.

Generated by 'django-admin startproject' using Django 3.1.7.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

from pathlib import Path

import environ

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Get environment settings
env = environ.Env()
DOTENV = BASE_DIR / ".env"
if DOTENV.exists() and not env("IGNORE_ENV_FILE", default=False):
    environ.Env.read_env(DOTENV.open())

# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = "en-US"
TIME_ZONE = "America/New_York"
USE_I18N = True
USE_L10N = True
USE_TZ = True

SITE_ID = 1

#######################################################################
# Integrations/Resources: settings likely to vary between environments
#######################################################################
# SECRET_KEY intentionally has no default, and will error if not provided
# in the environment. This ensures you don't accidentally run with an
# insecure configuration in production.
SECRET_KEY = env("SECRET_KEY")
DEBUG = env("DEBUG", default=False)
ALLOWED_HOSTS = env("ALLOWED_HOSTS", default=[])

# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
SQLITE_DB = BASE_DIR / "var" / "db.sqlite3"
DATABASES = {"default": env.db("DATABASE_URL", default=f"sqlite:///{SQLITE_DB}")}
CACHES = {"default": env.cache("CACHE_URL", default="locmemcache://")}
# Email settings don't use a dict. Add to local vars instead.
# https://django-environ.readthedocs.io/en/latest/#email-settings
EMAIL_CONFIG = env.email_url("EMAIL_URL", default="consolemail://")
vars().update(EMAIL_CONFIG)

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = "/static/"
STATIC_ROOT = BASE_DIR / "var" / "static"
if not STATIC_ROOT.exists():
    STATIC_ROOT.mkdir(parents=True, exist_ok=True)
MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "var" / "media"
if not MEDIA_ROOT.exists():
    MEDIA_ROOT.mkdir(parents=True, exist_ok=True)
# ManifestStaticFilesStorage is recommended in production, to prevent outdated
# Javascript / CSS assets being served from cache.
# See https://docs.djangoproject.com/en/3.1/ref/contrib/staticfiles/#manifeststaticfilesstorage
STATICFILES_STORAGE = "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"

# CELERY settings
# If the environment has not provided settings, assume there is no broker
# and run celery tasks in-process. This means you MUST provide
# CELERY_TASK_ALWAYS_EAGER=False in your environment to actually use celery.
CELERY_TASK_ALWAYS_EAGER = env("CELERY_TASK_ALWAYS_EAGER", default=True)
CELERY_TASK_EAGER_PROPAGATES = env("CELERY_TASK_EAGER_PROPAGATES", default=True)
CELERY_BROKER_URL = env("CELERY_BROKER_URL", default="redis://localhost:6379/7")
CELERY_RESULT_BACKEND = env("CELERY_RESULT_BACKEND", default="")
CELERY_TIME_ZONE = TIME_ZONE
CELERY_BEAT_SCHEDULER = "django_celery_beat.schedulers:DatabaseScheduler"

#######################################################################
# Application Composition: Fixed values regardless of environment.
#######################################################################
ROOT_URLCONF = "webquills.urls"
WSGI_APPLICATION = "webquills.wsgi.application"
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"

# The order of installed apps determines the search order for templates.
# Template loader uses first template it finds, so in order to override
# templates from 3rd parties, list our apps first, stock stuff last.
INSTALLED_APPS = [
    # Our apps
    "webquills.theme_bs4_2021",
    "webquills.sites",  # enhanced sites framework
    "webquills.linkmgr",
    "webquills.core",
    # third party apps
    "bootstrap4",
    "django_celery_beat",
    "taggit",
    "tinymce",
    # core Django apps
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.messages",
    "django.contrib.sessions",
    "django.contrib.staticfiles",
]

MIDDLEWARE = [
    # Do security stuff in Apache, not Django.
    # https://docs.djangoproject.com/en/3.2/ref/middleware/#django.middleware.security.SecurityMiddleware
    #    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",  # required for auth
    "django.middleware.common.CommonMiddleware",  # trailing /
    # "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "webquills.sites.middleware.SitesMiddleware",  # set request.site
    # Set this in Apache, not Django.
    # https://docs.djangoproject.com/en/3.2/ref/clickjacking/
    # "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
                "django.template.context_processors.media",
                "django.template.context_processors.static",
                "webquills.theme_bs4_2021.context_processors.theme",
            ]
        },
    }
]

#######################################################################
# Configure 3rd party modules we use.
#######################################################################
TAGGIT_CASE_INSENSITIVE = True
TAG_SPACES_ALLOWED = False

TINYMCE_DEFAULT_CONFIG = {
    "height": "320px",
    "width": "960px",
    "menubar": "edit view insert format tools table help",
    "pagebreak_separator": "<!-- pagebreak --><span id=continue-reading></span>",
    "plugins": "advlist autoresize charmap code codesample help hr image imagetools "
    "link lists media pagebreak paste searchreplace table toc visualblocks "
    "visualchars wordcount",
    "toolbar": "undo redo | bold italic strikethrough | styleselect | removeformat | "
    "numlist bullist indent outdent | image pagebreak | code",
    "image_advtab": True,
    "image_caption": True,
    "image_class_list": [
        {"title": "Responsive", "value": "img-fluid"},
        {"title": "Left", "value": "float-left"},
        {"title": "Right", "value": "float-right"},
    ],
    "image_list": "/mce/recent_images.json",
}

#######################################################################
# DEVELOPMENT: If running in a dev environment, loosen restrictions
# and add debugging tools.
#######################################################################
if DEBUG:
    ALLOWED_HOSTS = ["*"]
    # Use the basic storage with no manifest
    STATICFILES_STORAGE = "django.contrib.staticfiles.storage.StaticFilesStorage"
    try:
        import debug_toolbar

        INSTALLED_APPS.append("debug_toolbar")
        MIDDLEWARE.append("debug_toolbar.middleware.DebugToolbarMiddleware")
        INTERNAL_IPS = [
            "127.0.0.1",
        ]
        # See also urls.py for debug_toolbar urls
    except ImportError:
        # Dev tools are optional
        pass

    try:
        import django_extensions

        INSTALLED_APPS.append("django_extensions")
    except ImportError:
        # Dev tools are optional
        pass
