"""
Django settings for mysite project.

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

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 os

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


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

# SECURITY WARNING: keep the secret key used in production secret!
# SECRET_KEY = '&%k^s3mn&jufb5t*3%fg^!^25597(jzn^4h1^3hcpqr2s!2o%5'
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', '&%k^s3mn&jufb5t*3%fg^!^25597(jzn^4h1^3hcpqr2s!2o%5')

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

ALLOWED_HOSTS = ['34.64.199.233']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'home',
]

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',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'csp.middleware.CSPMiddleware',
]

# SSL/HTTPS 헤더 설정
#SECURE_SSL_REDIRECT = False
#SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# HSTS
SECURE_HSTS_SECONDS = 31536000  # 365 * 24 * 60 * 60
SECURE_HSTS_PRELOAD = True
SECURE_HSTS_INCLUDE_SUBDOMAINS = True

# X-Content-Type-Options
SECURE_CONTENT_TYPE_NOSNIFF = True

# X-XSS-Proection
SECURE_BROWSER_XSS_FILTER = True

CSP_DEFAULT_SRC = ("'self'", "'unsafe-inline'", "https://fonts.googleapis.com", "http://*.daumcdn.net", "https://cdnjs.cloudflare.com")
CSP_SCRIPT_SRC = ("'self'", "'unsafe-inline'")
CSP_SCRIPT_SRC_ELEM = ("'self'", "'unsafe-inline'","https://fonts.googleapis.com", "https://kit.fontawesome.com",
                    "https://code.jquery.com", "https://cdn.jsdelivr.net", "http://cdnjs.cloudflare.com",
                    "http://*.daumcdn.net")
CSP_MEDIA_SRC = ("'self'", "https://t1.kakaocdn.net")
CSP_CONNECT_SRC = ("'self'", "https://ka-f.fontawesome.com")
CSP_IMG_SRC =  ("'self'", "https://dimg.donga.com", "http://cdn.kormedi.com", "http://img.hani.co.kr", "http://bycherry1.cafe24.com",
                "https://stlog1-local.kakao.com", "http://*.daumcdn.net", 'data:')
CSP_FONT_SRC = ("'self'", "https://fonts.gstatic.com", "https://ka-f.fontawesome.com")
CSP_FRAME_SRC = ("'self'", "https://www.youtube.com")

CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = ['localhost']
CORS_URLS_REGEX = r'^/api/.*$'

CORS_ALLOW_CREDENTIALS = True 

CSRF_TRUSTED_ORIGINS = CORS_ORIGIN_WHITELIST


# X-Frame-Options
# X_FRAME_OPTIONS = 'DENY'

# HttpOnly
SESSION_COOKIE_DOMAIN = '34.64.199.233'
SESSION_COOKIE_HTTPONLY = True
CSRF_COOKIE_HTTPONLY = True

# Secure
SESSION_COOKIE_SECURE = False
CSRF_COOKIE_SECURE = False

# SameSite
SESSION_COOKIE_SAMESITE = 'Lax'
CSRF_COOKIE_SAMESITE = 'Lax'


ROOT_URLCONF = 'mysite.urls'

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',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'


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

DATABASES = {
        'default': {
            'ENGINE': 'djongo',
            'NAME': 'mydb',
        }
}


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

LANGUAGE_CODE = 'ko-kr'

TIME_ZONE = 'Asia/Seoul'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

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

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
