<reponame>Qkessler/CloudQuestions
"""
Django settings for CloudQuestions_web project.

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

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
import json

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

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get("DEBUG") == "True"
DEV = os.environ.get("DEV") == "True"

# CREDENTIALS
if not DEV:
    with open(BASE_DIR + '/CloudQuestions_web/secret.json', 'r') as f:
        secret = json.load(f)
    SECRET_KEY = secret["DJANGO_SECRET"]
    EMAIL_HOST_USER = secret["EMAIL_HOST_USER"]
    EMAIL_HOST_PASSWORD = secret["EMAIL_HOST_PASSWORD"]
    DEFAULT_FROM_EMAIL = secret["DEFAULT_FROM_EMAIL"]
    SERVER_EMAIL = secret["SERVER_EMAIL"]
    POSTGRES_PASSWORD = secret["POSTGRES_PASSWORD"]
    SOCIAL_AUTH_GITHUB_KEY = secret["SOCIAL_AUTH_GITHUB_KEY"]
    SOCIAL_AUTH_GITHUB_SECRET = secret["SOCIAL_AUTH_GITHUB_SECRET"]
    SOCIAL_AUTH_TWITTER_KEY = secret["SOCIAL_AUTH_TWITTER_KEY"]
    SOCIAL_AUTH_TWITTER_SECRET = secret["SOCIAL_AUTH_TWITTER_SECRET"]
    SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = secret["SOCIAL_AUTH_GOOGLE_OAUTH2_KEY"]
    SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = secret["SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET"]
    RECAPTCHA_PUBLIC_KEY = secret["RECAPTCHA_PUBLIC_KEY"]
    RECAPTCHA_PRIVATE_KEY = secret["RECAPTCHA_PRIVATE_KEY"]
    CALENDAR_API_KEY = secret["CALENDAR_API_KEY"]
    CALENDAR_CLIENT_ID = secret["CALENDAR_CLIENT_ID"]
    CALENDAR_CLIENT_SECRET = secret["CALENDAR_CLIENT_SECRET"]
else:
    SECRET_KEY = os.environ.get("DJANGO_SECRET")
    EMAIL_HOST_USER = os.environ.get("EMAIL_HOST_USER")
    EMAIL_HOST_PASSWORD = os.environ.get("EMAIL_HOST_PASSWORD")
    DEFAULT_FROM_EMAIL = os.environ.get("DEFAULT_FROM_EMAIL")
    SERVER_EMAIL = os.environ.get("SERVER_EMAIL")
    SOCIAL_AUTH_GITHUB_KEY = os.environ.get("SOCIAL_AUTH_GITHUB_KEY")
    SOCIAL_AUTH_GITHUB_SECRET = os.environ.get("SOCIAL_AUTH_GITHUB_SECRET")
    SOCIAL_AUTH_TWITTER_KEY = os.environ.get("SOCIAL_AUTH_TWITTER_KEY")
    SOCIAL_AUTH_TWITTER_SECRET = os.environ.get("SOCIAL_AUTH_TWITTER_SECRET")
    SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = os.environ.get("SOCIAL_AUTH_GOOGLE_OAUTH2_KEY")
    SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = os.environ.get("SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET")
    RECAPTCHA_PUBLIC_KEY = os.environ.get("RECAPTCHA_PUBLIC_KEY")
    RECAPTCHA_PRIVATE_KEY = os.environ.get("RECAPTCHA_PRIVATE_KEY")
    CALENDAR_API_KEY = os.environ.get("CALENDAR_API_KEY")
    CALENDAR_CLIENT_ID = os.environ.get("CALENDAR_CLIENT_ID")
    CALENDAR_CLIENT_SECRET = os.environ.get("CALENDAR_CLIENT_SECRET")

# Had to include both localhost and IP because Django doesn't default to localhost.    
ALLOWED_HOSTS = ['cloudquestions.es', 'www.cloudquestions.es', '127.0.0.1', 'localhost']

LOGIN_REDIRECT_URL = 'questions:questions'
SOCIAL_AUTH_LOGIN_ERROR_URL = 'accounts:settings'
SOCIAL_AUTH_LOGIN_REDIRECT_URL = 'accounts:settings'
SOCIAL_AUTH_RAISE_EXCEPTIONS = False

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'questions.apps.QuestionsConfig',
    'accounts.apps.AccountsConfig',
    'social_django',
    'captcha',
    'crispy_forms'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'social_django.middleware.SocialAuthExceptionMiddleware'
]

ROOT_URLCONF = 'CloudQuestions_web.urls'

CRISPY_TEMPLATE_PACK = 'bootstrap4'

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',
                'social_django.context_processors.backends',
                'social_django.context_processors.login_redirect'
            ],
        },
    },
]

WSGI_APPLICATION = 'CloudQuestions_web.wsgi.application'

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

if not DEV:
    database_config = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'cloudquestions',
            'USER': 'qkessler',
            'PASSWORD': <PASSWORD>,
            'HOST': 'localhost',
            'PORT': '',
        }
    }
else:
    database_config = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'questions.sqlite'),
        }
    }

DATABASES = database_config

# Dates.
DATE_FORMAT = "%d/%m/%Y"

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'

LANGUAGES = [
    ('en-us','English'),
    ('es', 'Spanish'),
    ('fr', 'French')
]

TEMPLATE_CONTEXT_PROCESSORS = {
    'django.core.context_processors.i18n',
}

LOCALE_PATHS = [
    os.path.join(BASE_DIR, 'locale')
]

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'assets'),
]

# Email configuration.
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

# Social login.
AUTHENTICATION_BACKENDS = [
    'social_core.backends.google.GoogleOAuth2',
    'social_core.backends.twitter.TwitterOAuth',
    'social_core.backends.github.GithubOAuth2',
    'django.contrib.auth.backends.ModelBackend',
]

SOCIAL_AUTH_PIPELINE = (
    'social_core.pipeline.social_auth.social_details',
    'social_core.pipeline.social_auth.social_uid',
    'social_core.pipeline.social_auth.auth_allowed',
    'social_core.pipeline.social_auth.social_user',
    'social_core.pipeline.user.get_username',
    'social_core.pipeline.social_auth.associate_by_email',
    'social_core.pipeline.user.create_user',
    'social_core.pipeline.social_auth.associate_user',
    'social_core.pipeline.social_auth.load_extra_data',
    'social_core.pipeline.user.user_details')

"""
Django settings for CloudQuestions_web project.

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

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
import json

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

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get("DEBUG") == "True"
DEV = os.environ.get("DEV") == "True"

# CREDENTIALS
if not DEV:
    with open(BASE_DIR + '/CloudQuestions_web/secret.json', 'r') as f:
        secret = json.load(f)
    SECRET_KEY = secret["DJANGO_SECRET"]
    EMAIL_HOST_USER = secret["EMAIL_HOST_USER"]
    EMAIL_HOST_PASSWORD = secret["EMAIL_HOST_PASSWORD"]
    DEFAULT_FROM_EMAIL = secret["DEFAULT_FROM_EMAIL"]
    SERVER_EMAIL = secret["SERVER_EMAIL"]
    POSTGRES_PASSWORD = secret["POSTGRES_PASSWORD"]
    SOCIAL_AUTH_GITHUB_KEY = secret["SOCIAL_AUTH_GITHUB_KEY"]
    SOCIAL_AUTH_GITHUB_SECRET = secret["SOCIAL_AUTH_GITHUB_SECRET"]
    SOCIAL_AUTH_TWITTER_KEY = secret["SOCIAL_AUTH_TWITTER_KEY"]
    SOCIAL_AUTH_TWITTER_SECRET = secret["SOCIAL_AUTH_TWITTER_SECRET"]
    SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = secret["SOCIAL_AUTH_GOOGLE_OAUTH2_KEY"]
    SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = secret["SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET"]
    RECAPTCHA_PUBLIC_KEY = secret["RECAPTCHA_PUBLIC_KEY"]
    RECAPTCHA_PRIVATE_KEY = secret["RECAPTCHA_PRIVATE_KEY"]
    CALENDAR_API_KEY = secret["CALENDAR_API_KEY"]
    CALENDAR_CLIENT_ID = secret["CALENDAR_CLIENT_ID"]
    CALENDAR_CLIENT_SECRET = secret["CALENDAR_CLIENT_SECRET"]
else:
    SECRET_KEY = os.environ.get("DJANGO_SECRET")
    EMAIL_HOST_USER = os.environ.get("EMAIL_HOST_USER")
    EMAIL_HOST_PASSWORD = os.environ.get("EMAIL_HOST_PASSWORD")
    DEFAULT_FROM_EMAIL = os.environ.get("DEFAULT_FROM_EMAIL")
    SERVER_EMAIL = os.environ.get("SERVER_EMAIL")
    SOCIAL_AUTH_GITHUB_KEY = os.environ.get("SOCIAL_AUTH_GITHUB_KEY")
    SOCIAL_AUTH_GITHUB_SECRET = os.environ.get("SOCIAL_AUTH_GITHUB_SECRET")
    SOCIAL_AUTH_TWITTER_KEY = os.environ.get("SOCIAL_AUTH_TWITTER_KEY")
    SOCIAL_AUTH_TWITTER_SECRET = os.environ.get("SOCIAL_AUTH_TWITTER_SECRET")
    SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = os.environ.get("SOCIAL_AUTH_GOOGLE_OAUTH2_KEY")
    SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = os.environ.get("SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET")
    RECAPTCHA_PUBLIC_KEY = os.environ.get("RECAPTCHA_PUBLIC_KEY")
    RECAPTCHA_PRIVATE_KEY = os.environ.get("RECAPTCHA_PRIVATE_KEY")
    CALENDAR_API_KEY = os.environ.get("CALENDAR_API_KEY")
    CALENDAR_CLIENT_ID = os.environ.get("CALENDAR_CLIENT_ID")
    CALENDAR_CLIENT_SECRET = os.environ.get("CALENDAR_CLIENT_SECRET")

# Had to include both localhost and IP because Django doesn't default to localhost.    
ALLOWED_HOSTS = ['cloudquestions.es', 'www.cloudquestions.es', '127.0.0.1', 'localhost']

LOGIN_REDIRECT_URL = 'questions:questions'
SOCIAL_AUTH_LOGIN_ERROR_URL = 'accounts:settings'
SOCIAL_AUTH_LOGIN_REDIRECT_URL = 'accounts:settings'
SOCIAL_AUTH_RAISE_EXCEPTIONS = False

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'questions.apps.QuestionsConfig',
    'accounts.apps.AccountsConfig',
    'social_django',
    'captcha',
    'crispy_forms'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'social_django.middleware.SocialAuthExceptionMiddleware'
]

ROOT_URLCONF = 'CloudQuestions_web.urls'

CRISPY_TEMPLATE_PACK = 'bootstrap4'

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',
                'social_django.context_processors.backends',
                'social_django.context_processors.login_redirect'
            ],
        },
    },
]

WSGI_APPLICATION = 'CloudQuestions_web.wsgi.application'

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

if not DEV:
    database_config = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'cloudquestions',
            'USER': 'qkessler',
            'PASSWORD': <PASSWORD>,
            'HOST': 'localhost',
            'PORT': '',
        }
    }
else:
    database_config = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'questions.sqlite'),
        }
    }

DATABASES = database_config

# Dates.
DATE_FORMAT = "%d/%m/%Y"

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'

LANGUAGES = [
    ('en-us','English'),
    ('es', 'Spanish'),
    ('fr', 'French')
]

TEMPLATE_CONTEXT_PROCESSORS = {
    'django.core.context_processors.i18n',
}

LOCALE_PATHS = [
    os.path.join(BASE_DIR, 'locale')
]

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'assets'),
]

# Email configuration.
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

# Social login.
AUTHENTICATION_BACKENDS = [
    'social_core.backends.google.GoogleOAuth2',
    'social_core.backends.twitter.TwitterOAuth',
    'social_core.backends.github.GithubOAuth2',
    'django.contrib.auth.backends.ModelBackend',
]

SOCIAL_AUTH_PIPELINE = (
    'social_core.pipeline.social_auth.social_details',
    'social_core.pipeline.social_auth.social_uid',
    'social_core.pipeline.social_auth.auth_allowed',
    'social_core.pipeline.social_auth.social_user',
    'social_core.pipeline.user.get_username',
    'social_core.pipeline.social_auth.associate_by_email',
    'social_core.pipeline.user.create_user',
    'social_core.pipeline.social_auth.associate_user',
    'social_core.pipeline.social_auth.load_extra_data',
    'social_core.pipeline.user.user_details')
Django settings for CloudQuestions_web project.  Generated by 'django-admin startproject' using Django 3.0.5.  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, ...) # SECURITY WARNING: don't run with debug turned on in production! # CREDENTIALS # Had to include both localhost and IP because Django doesn't default to localhost. # Application definition # Database # https://docs.djangoproject.com/en/3.0/ref/settings/#databases # Dates. # Internationalization # https://docs.djangoproject.com/en/3.0/topics/i18n/ # Static files (CSS, JavaScript, Images) # Email configuration. # Social login.