"""
Django settings for cadpoint project.

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

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

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

from pathlib import Path
import socket

try:
    # В репозитории хранится только шаблон секретов, а реальный файл остаётся локальным.
    from .my_secret import *
except ImportError:  # pragma: no cover - запасной путь для открытого репозитория
    from .my_secret_example import *


# 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.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = MY_SECRET_KEY

# SECURITY WARNING: don't run with debug turned on in production!
if socket.gethostname() in (MY_HOST_HOME, MY_HOST_WORK):
    DEBUG = True
else:
    # Все остальные хосты (подразумевается продакшн)
    DEBUG = False


ALLOWED_HOSTS = [
    '127.0.0.1',
    'localhost',
    '192.168.1.30',             # разработка домашний
    '10.10.5.6',                # разработка офис
    '90.156.203.25',            # продакшн хостинг masterhost
    'cadpoint.ru',              # продакшн хостинг
    'www.cadpoint.ru',          # продакшн хостинг
    'new.cadpoint.ru',          # продакшн хостинг
]

#########################################
# Настройки сообщений об ошибках когда все упало и т.п.
ADMINS = (
    ('S.Erjemin', 'erjemin@gmail.com'),
)


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'easy_thumbnails',
    'filer.apps.FilerConfig',
    'mptt.apps.MpttConfig',
    # # 'ckeditor_uploader',
    'ckeditor',
    'taggit.apps.TaggitAppConfig',
    # 'fontawesome-free'
    'web.apps.WebConfig',
]

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 = 'cadpoint.urls'

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

WSGI_APPLICATION = 'cadpoint.wsgi.application'

# Password validation
# https://docs.djangoproject.com/en/3.2/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.2/topics/i18n/
LANGUAGE_CODE = 'ru-RU'         # <--------- RUSSIAN
# TIME_ZONE = 'Etc/GMT+3'       #
TIME_ZONE = 'Europe/Moscow'     #
USE_I18N = True
USE_L10N = True
USE_TZ = True                   # учитывать часовой пояс
FIRST_DAY_OF_WEEK = 1           # неделя начинается с понедельника
DEFAULT_CHARSET = 'utf-8'


# настройки THUMBNAIL (батарейка по созданию превьюшек)
THUMBNAIL_HIGH_RESOLUTION = True  # Для easy_thumbnails поддержки retina-дисплеев (MacBooks, iOS и т.п.)
THUMBNAIL_PROCESSORS = (
    'easy_thumbnails.processors.colorspace',
    'easy_thumbnails.processors.autocrop',
    #'easy_thumbnails.processors.scale_and_crop',
    'filer.thumbnail_processors.scale_and_crop_with_subject_location',
    'easy_thumbnails.processors.filters',
)
THUMBNAIL_ALIASES = {
    '': {
        'x64': {'size': (64, 64), 'crop': True},
        'x680': {'size': (680, 680), 'crop': True},
        'x1140': {'size': (1140, 1140), 'crop': True},
    },
}
THUMBNAIL_QUALITY = 85
THUMBNAIL_TRANSPARENCY_EXTENSION = 'png'
THUMBNAIL_WIDGET_OPTIONS = {'size': (64, 64)}


CKEDITOR_UPLOAD_PATH = "uploads/"
CKEDITOR_BASEPATH = "/static/ckeditor/ckeditor/"
CKEDITOR_FILENAME_GENERATOR = 'utils.get_filename'
# конфигуратор ckeditor https://ckeditor.com/latest/samples/toolbarconfigurator/index.html#basic
CKEDITOR_CONFIGS = {
    'default': {
        'toolbar_mini': [
            {'name': 'document', 'items': ['Source', '-', ]},
            {'name': 'basicstyles', 'items': ['Bold', 'Italic', 'Underline', 'NumberedList', 'BulletedList',
                                              'Format', '-', 'RemoveFormat']},
            {'name': 'my_custom_tools', 'items': ['Preview', 'Maximize']},
        ],
        'toolbar': 'mini',  # put selected toolbar config here
        'height': '110',
        'toolbarCanCollapse': True,
    },
    'fine': {
        'toolbar_fine': [
            {'name': 'document', 'items': ['Source', '-' ]},
            {'name': 'clipboard', 'items': ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo']},
            {'name': 'basicstyles',
             'items': ['Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat']},
            {'name': 'my_custom_tools', 'items': ['Preview', 'Maximize']},
            '/',
            {'name': 'paragraph',
             'items': ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', 'CreateDiv', '-',
                       'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', 'Styles', 'Format', 'Iframe']},
            {'name': 'links', 'items': ['Link', 'Unlink', 'Anchor']},
            {'name': 'insert', 'items': ['Image', 'Table', 'HorizontalRule', 'SpecialChar']},
        ],
        'toolbar': 'fine',
        # 'removeButtons': 'Save,NewPage,ExportPdf,Preview,Print,Templates,Find,Replace,SelectAll,Scayt,Form,'
        #                  'Checkbox,Radio,TextField,Textarea,Select,Button,ImageButton,HiddenField,Format,'
        #                  'Font,FontSize,Maximize,ShowBlocks,About,Styles,Flash,Smiley,PageBreak,Iframe,BidiLtr,'
        #                  'BidiRtl,Language,JustifyBlock,JustifyRight,JustifyCenter,JustifyLeft,Indent,Outdent,'
        #                  'Strike,TextColor,BGColor,
        'toolbarCanCollapse': True,
        # 'extraPlugins': 'filer',
        # 'editor': [
        #     {'name': 'filebrowserBrowseUrl', 'items': ''},
        #     {'name': 'filebrowserUploadUrl', 'items': ''},
        # ],
    },
}

FILER_SUBJECT_LOCATION_IMAGE_DEBUG = True
FILER_CANONICAL_URL = 'sharing/'


STATIC_URL = '/static/'
MEDIA_URL = '/media/'

if DEBUG:     # DEBUG: заменяем настройки прода, на настройки девопа
    MEDIA_ROOT = MY_MEDIA_ROOT_DEV
    # STATIC_ROOT = MY_STATIC_ROOT_DEV1
    STATICFILES_DIRS = [MY_STATIC_ROOT_DEV, ]
    #########################################
    # настройки для почтового сервера
    EMAIL_HOST = MY_EMAIL_HOST_DEV  # SMTP server
    EMAIL_PORT = MY_EMAIL_PORT_DEV  # для SSL/https
    EMAIL_HOST_USER = MY_EMAIL_HOST_USER_DEV  # login or ''
    EMAIL_HOST_PASSWORD = MY_EMAIL_HOST_PASSWORD_DEV  # password
    EMAIL_FROM = MY_EMAIL_FROM_DEV  # мейл, от имени которого отправляются письма
    DATABASES = {
        'default': {
            'ENGINE': "django.db.backends.mysql",
            'HOST': MY_DATABASE_HOST_DEV,  # Set to "" for localhost. Not used with sqlite3.
            'PORT': MY_DATABASE_PORT_DEV,  # Set to "" for default. Not used with sqlite3.
            'NAME': MY_DATABASE_NAME_DEV,  # Not used with sqlite3.
            'USER': MY_DATABASE_USER_DEV,  # Not used with sqlite3.
            'PASSWORD': MY_DATABASE_PASSWORD_DEV,  # Not used with sqlite3.
            # 'OPTIONS': { 'autocommit': True, }
        }
    }
    MIDDLEWARE += ['debug_toolbar.middleware.DebugToolbarMiddleware', ]
    INSTALLED_APPS += ['debug_toolbar', ]
    INTERNAL_IPS = ['127.0.0.1', '192.168.1.30', '10.10.5.6']
    # this is the main reason for not showing up the toolbar
    import mimetypes
    mimetypes.add_type("application/javascript", ".js", True)
    DEBUG_TOOLBAR_CONFIG = {'INTERCEPT_REDIRECTS': False, }
else:
    MEDIA_ROOT = MY_MEDIA_ROOT_PROD
    STATIC_ROOT = MY_STATIC_ROOT_PROD
    # STATICFILES_DIRS = [MY_STATIC_ROOT_PROD1, ]
    #########################################
    # настройки для почтового сервера
    EMAIL_HOST = MY_EMAIL_HOST_PROD  # SMTP server
    EMAIL_PORT = MY_EMAIL_PORT_PROD  # для SSL/https
    EMAIL_HOST_USER = MY_EMAIL_HOST_USER_PROD  # login or ''
    EMAIL_HOST_PASSWORD = MY_EMAIL_HOST_PASSWORD_PROD  # password
    EMAIL_FROM = MY_EMAIL_FROM_PROD  # мейл, от имени которого отправляются письма
    DATABASES = {
        'default': {
            'ENGINE': "django.db.backends.mysql",
            'HOST': MY_DATABASE_HOST_PROD,  # Set to "" for localhost. Not used with sqlite3.
            'PORT': MY_DATABASE_PORT_PROD,  # Set to "" for default. Not used with sqlite3.
            'NAME': MY_DATABASE_NAME_PROD,  # Not used with sqlite3.
            'USER': MY_DATABASE_USER_PROD,  # Not used with sqlite3.
            'PASSWORD': MY_DATABASE_PASSWORD_PROD,  # Not used with sqlite3.
            # 'OPTIONS': { 'autocommit': True, }
        }
    }

SERVER_EMAIL = DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
EMAIL_USE_TLS = True
EMAIL_SUBJECT_PREFIX = '[CADPOINT.RU]: '  # префикс для оповещений об ошибках и необработанных исключениях

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
