<reponame>Suhas-G/Melton-App-Server
"""
Django settings for api project.
Generated by 'django-admin startproject' using Django 3.0.3.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""

import os
from datetime import timedelta
from datetime import datetime
import environ
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration


# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

env = environ.Env()
environ.Env.read_env(os.path.join(BASE_DIR, ".env"))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env.str("DJANGO_SECRET_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env.bool("DJANGO_DEBUG", default=False)

ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS")

sentry_sdk.init(
    dsn=env.str("SENTRY_DSN"),
    integrations=[DjangoIntegration()],
    # If you wish to associate users to errors (assuming you are using
    # django.contrib.auth) you may enable sending PII data.
    send_default_pii=True,
)

# Application definition

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "django.forms",
    "storages",
    "markdownx",
    "rest_framework",
    "rest_framework.authtoken",
    "authentication",
    "store",
    "posts",
]

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "api.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": ["templates"],
        "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",
            ],
        },
    },
]

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "staticfiles"),
]

WSGI_APPLICATION = "api.wsgi.application"


# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases


DATABASES = {
    "dev": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": os.path.join(BASE_DIR, "db.sqlite3"),
    },
    "prod": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": env.str("DB_NAME", default="melton_foundation"),
        "USER": env.str("DB_USER", default="user"),
        "PASSWORD": env.str("DB_PASSWORD", default="password"),
        "HOST": env.str("DB_HOST", default="localhost"),
        "PORT": env.str("DB_PORT", default="5432"),
    },
}

default_database = env.str("DB_DEFAULT", "dev")
DATABASES["default"] = DATABASES[default_database]


# Password validation
# https://docs.djangoproject.com/en/3.0/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/3.0/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_L10N = True

USE_TZ = True

FORM_RENDERER = "django.forms.renderers.TemplatesSetting"

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")

AUTH_USER_MODEL = "authentication.AppUser"

TOKEN_SETTINGS = {
    "IDLE_TOKEN_LIFESPAN": timedelta(hours=1),
    "EXPIRING_TOKEN_LIFESPAN": timedelta(days=14),
}


# Secrets
GAUTH_ANDROID_CLIENT_ID = env.str("GAUTH_ANDROID_CLIENT_ID")
GAUTH_IOS_CLIENT_ID = env.str("GAUTH_IOS_CLIENT_ID")

APPLE_OAUTH_CLIENT_ID = env.str("APPLE_OAUTH_CLIENT_ID")
APPLE_OAUTH_KEY_ID = env.str("APPLE_OAUTH_KEY_ID")
APPLE_OAUTH_TEAM_ID = env.str("APPLE_OAUTH_TEAM_ID")
APPLE_OATH_PRIVATE_KEY = env.str("APPLE_OATH_PRIVATE_KEY", multiline=True)


AWS_ACCESS_KEY_ID = env.str("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = env.str("AWS_SECRET_ACCESS_KEY")
AWS_STORAGE_BUCKET_NAME = env.str("AWS_STORAGE_BUCKET_NAME")
AWS_S3_CUSTOM_DOMAIN = "%s.s3.amazonaws.com" % AWS_STORAGE_BUCKET_NAME
AWS_DEFAULT_ACL = None

DEFAULT_FILE_STORAGE = "api.storage_backends.MediaStorage"


MARKDOWNX_MEDIA_PATH = datetime.now().strftime("markdownx/%Y/%m/%d")
MARKDOWNX_IMAGE_MAX_SIZE = {"size": (3840, 0), "quality": 90}
MARKDOWNX_MARKDOWN_EXTENSIONS = ["markdown.extensions.extra"]
MARKDOWNX_UPLOAD_MAX_SIZE = 5 * 1024 * 1024

LOGIN_URL = "/admin/login/"

# email settings
EMAIL_USE_TLS = True
EMAIL_HOST = env.str("EMAIL_HOST")
EMAIL_PORT = env.int("EMAIL_PORT")
EMAIL_HOST_USER = env.str("EMAIL_HOST_USER")
EMAIL_HOST_PASSWORD = env.str("EMAIL_HOST_PASSWORD")
DEFAULT_FROM_EMAIL = env.str("EMAIL_HOST_USER")
EMAIL_SUBJECT_PREFIX = "[Melton App] "

# Mail notifications
ADMINS = [x.split(":") for x in env.list("DJANGO_ADMINS", default=[])]
MANAGERS = [x.split(":") for x in env.list("DJANGO_MANAGERS", default=[])]
EMAIL_REGISTER_NOTIFICATION = env.bool("EMAIL_REGISTER_NOTIFICATION", default=False)

# Feature Flag for autoapproving new users
AUTOAPPROVE_WITHOUT_ADMIN_APPROVAL = env.bool(
    "AUTOAPPROVE_WITHOUT_ADMIN_APPROVAL", default=False
)

# Dashboard customisation
SITE_HEADER = "Melton Foundation Dashboard"
SITE_TITLE = "Melton Foundation Admin"

"""
Django settings for api project.
Generated by 'django-admin startproject' using Django 3.0.3.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""

import os
from datetime import timedelta
from datetime import datetime
import environ
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration


# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

env = environ.Env()
environ.Env.read_env(os.path.join(BASE_DIR, ".env"))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env.str("DJANGO_SECRET_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env.bool("DJANGO_DEBUG", default=False)

ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS")

sentry_sdk.init(
    dsn=env.str("SENTRY_DSN"),
    integrations=[DjangoIntegration()],
    # If you wish to associate users to errors (assuming you are using
    # django.contrib.auth) you may enable sending PII data.
    send_default_pii=True,
)

# Application definition

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "django.forms",
    "storages",
    "markdownx",
    "rest_framework",
    "rest_framework.authtoken",
    "authentication",
    "store",
    "posts",
]

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "api.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": ["templates"],
        "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",
            ],
        },
    },
]

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "staticfiles"),
]

WSGI_APPLICATION = "api.wsgi.application"


# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases


DATABASES = {
    "dev": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": os.path.join(BASE_DIR, "db.sqlite3"),
    },
    "prod": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": env.str("DB_NAME", default="melton_foundation"),
        "USER": env.str("DB_USER", default="user"),
        "PASSWORD": env.str("DB_PASSWORD", default="password"),
        "HOST": env.str("DB_HOST", default="localhost"),
        "PORT": env.str("DB_PORT", default="5432"),
    },
}

default_database = env.str("DB_DEFAULT", "dev")
DATABASES["default"] = DATABASES[default_database]


# Password validation
# https://docs.djangoproject.com/en/3.0/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/3.0/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_L10N = True

USE_TZ = True

FORM_RENDERER = "django.forms.renderers.TemplatesSetting"

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")

AUTH_USER_MODEL = "authentication.AppUser"

TOKEN_SETTINGS = {
    "IDLE_TOKEN_LIFESPAN": timedelta(hours=1),
    "EXPIRING_TOKEN_LIFESPAN": timedelta(days=14),
}


# Secrets
GAUTH_ANDROID_CLIENT_ID = env.str("GAUTH_ANDROID_CLIENT_ID")
GAUTH_IOS_CLIENT_ID = env.str("GAUTH_IOS_CLIENT_ID")

APPLE_OAUTH_CLIENT_ID = env.str("APPLE_OAUTH_CLIENT_ID")
APPLE_OAUTH_KEY_ID = env.str("APPLE_OAUTH_KEY_ID")
APPLE_OAUTH_TEAM_ID = env.str("APPLE_OAUTH_TEAM_ID")
APPLE_OATH_PRIVATE_KEY = env.str("APPLE_OATH_PRIVATE_KEY", multiline=True)


AWS_ACCESS_KEY_ID = env.str("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = env.str("AWS_SECRET_ACCESS_KEY")
AWS_STORAGE_BUCKET_NAME = env.str("AWS_STORAGE_BUCKET_NAME")
AWS_S3_CUSTOM_DOMAIN = "%s.s3.amazonaws.com" % AWS_STORAGE_BUCKET_NAME
AWS_DEFAULT_ACL = None

DEFAULT_FILE_STORAGE = "api.storage_backends.MediaStorage"


MARKDOWNX_MEDIA_PATH = datetime.now().strftime("markdownx/%Y/%m/%d")
MARKDOWNX_IMAGE_MAX_SIZE = {"size": (3840, 0), "quality": 90}
MARKDOWNX_MARKDOWN_EXTENSIONS = ["markdown.extensions.extra"]
MARKDOWNX_UPLOAD_MAX_SIZE = 5 * 1024 * 1024

LOGIN_URL = "/admin/login/"

# email settings
EMAIL_USE_TLS = True
EMAIL_HOST = env.str("EMAIL_HOST")
EMAIL_PORT = env.int("EMAIL_PORT")
EMAIL_HOST_USER = env.str("EMAIL_HOST_USER")
EMAIL_HOST_PASSWORD = env.str("EMAIL_HOST_PASSWORD")
DEFAULT_FROM_EMAIL = env.str("EMAIL_HOST_USER")
EMAIL_SUBJECT_PREFIX = "[Melton App] "

# Mail notifications
ADMINS = [x.split(":") for x in env.list("DJANGO_ADMINS", default=[])]
MANAGERS = [x.split(":") for x in env.list("DJANGO_MANAGERS", default=[])]
EMAIL_REGISTER_NOTIFICATION = env.bool("EMAIL_REGISTER_NOTIFICATION", default=False)

# Feature Flag for autoapproving new users
AUTOAPPROVE_WITHOUT_ADMIN_APPROVAL = env.bool(
    "AUTOAPPROVE_WITHOUT_ADMIN_APPROVAL", default=False
)

# Dashboard customisation
SITE_HEADER = "Melton Foundation Dashboard"
SITE_TITLE = "Melton Foundation Admin"
Django settings for api project. Generated by 'django-admin startproject' using Django 3.0.3. For more information on this file, see https://docs.djangoproject.com/en/3.0/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/3.0/ref/settings/ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! # SECURITY WARNING: don't run with debug turned on in production! # If you wish to associate users to errors (assuming you are using # django.contrib.auth) you may enable sending PII data. # Application definition # Database # https://docs.djangoproject.com/en/3.0/ref/settings/#databases # Password validation # https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators # Internationalization # https://docs.djangoproject.com/en/3.0/topics/i18n/ # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.0/howto/static-files/ # Secrets # email settings # Mail notifications # Feature Flag for autoapproving new users # Dashboard customisation