"""
Django settings for project_settings project.

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

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

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve(strict=True).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 = '4sw8pqyraok*tiuooiy6_px5opzg$z$w$7q9wk=m)qfq#(715c'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    #own_apps
    'project_settings',
    'custom_user',
    # third party
    'rest_framework',
    'rest_framework.authtoken',
    'dj_rest_auth',
    "corsheaders",
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    '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',
]

AUTH_USER_MODEL = 'custom_user.User'

CORS_ALLOW_ALL_ORIGINS = True

ROOT_URLCONF = 'project_settings.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 = 'project_settings.wsgi.application'


REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication',
    ]
}

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

#DATABASES = {
#    'default': {
#        'ENGINE': 'django.db.backends.sqlite3',
#        'NAME': BASE_DIR / 'db.sqlite3',
#    }
#}

# USE FOR LOCAL DB ONLY ELSE COMMENT THIS OUT
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'gauranga',
        'USER': 'simha',
        'PASSWORD': 'krishna',
        'HOST': 'postgresql',
        'PORT': '5432'
    }
}



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


AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
        "OPTIONS": {
            "min_length": 3,
        },
    }
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

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/'


REDIS_HOST = "redis"
REDIS_PORT = "6379"
BROKER_URL = f'redis://{REDIS_HOST}:{REDIS_PORT}/0'
CELERY_BROKER_URL = BROKER_URL
CELERY_RESULT_BACKEND = BROKER_URL

# THE ABOVE SETTINGS SAME AS (NOTE broker and backend and /0 are needed)

# redis = 'redis://:gauranga@redis:6379/0'
# app = Celery(dirname, 
#     broker=redis, 
#     backend=redis
#     )

MEDIA_URL = "/media/"
# note this is the path as per the docker not as per this system
MEDIA_ROOT = str(BASE_DIR / "media")


#####################################################
# MISC: DONT TOUCH THE BELOW <START>
#####################################################

import os

# install snoop for this
# https://github.com/alexmojaki/snoop
import snoop
from cheap_repr import find_repr_function
import six


def path(event):
    return event.code.co_filename[-20:]


find_repr_function(object).maxparts = 100000000
find_repr_function(dict).maxparts = 100000000
find_repr_function(list).maxparts = 100000000
find_repr_function(six.text_type).maxparts = 500000000
find_repr_function(six.binary_type).maxparts = 100000000


snoop.install(enabled=DEBUG, columns=[path, "function"])

if DEBUG:

    PROJECT_HOME = os.path.dirname(os.path.realpath(__file__))
    print(f"PROJECT_HOME :: {PROJECT_HOME}")

    if os.path.exists(
        os.path.join(PROJECT_HOME, "external_config", "api_local_settings_fixed.py")
    ):
        from .external_config.api_local_settings_fixed import *  # noqa

    if os.path.exists(
        os.path.join(PROJECT_HOME, "external_config", "api_local_settings_var.py")
    ):
        from .external_config.api_local_settings_var import *  # noqa

#####################################################
# MISC: DONT TOUCH THE BELOW <END>
#####################################################