"""
Django settings for imozzle project.

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

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

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

import os
from os.path import join, dirname
from dotenv import load_dotenv

# python-dotenv
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)


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


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

# SECURITY WARNING: keep the secret key used in production secret!
# SECRET_KEY = 'pa&)u4g_s5ao#fnzpt+^q!*bimazucft_1k5=be@8pl_=lcm_9'
SECRET_KEY = os.environ.get('IMOZZLE_DJANGO_SECRET_KEY')

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

# ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['*']

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',  # Django REST framework
    'django_nose',  # django-nose
    'api',
]

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


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        # 'NAME': 'name_dummy',
        # 'USER': 'user_dummy',
        # 'PASSWORD': 'password_dummy',
        # 'HOST': 'example.com',
        # # 'PORT': '3306',
        'OPTIONS': {
            'read_default_file': os.path.abspath(os.path.dirname(__file__)) + '/my.cnf',
            'charset': 'utf8mb4',
        },
    }
    # 'default': {
    #     'ENGINE': 'django.db.backends.sqlite3',
    #     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    # }
}


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

LANGUAGE_CODE = 'ja'  # 'en-us'

TIME_ZONE = 'Asia/Tokyo'  # 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'

# Deploying static files
# http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html#deploying-static-files
# -> python manage.py collectstatic
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

# https://docs.djangoproject.com/en/1.10/topics/logging/#examples
# http://stackoverflow.com/questions/33990693/django-log-to-file-by-date-or-hour
# http://qiita.com/sakamossan/items/a98b949738028ad39a6b
LOGGING = {
    'version': 1,
    # 'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': "%(asctime)s [%(levelname)s] [%(name)s] %(module)s, %(message)s",
            # 'format' : "%(asctime)s [%(levelname)s] [%(name)s] %(module)s, %(process)d, %(thread)d, %(message)s",
            # 'format' : "%(asctime)s [%(levelname)s] [%(name)s:%(lineno)s] %(message)s",
            # 'datefmt' : "%Y/%m/%d %H:%M:%S %z"
        },
    },
    'handlers': {
        'api_file_debug': {
            'level': 'DEBUG',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'filename': 'logs/api_debug.log',
            'when': 'midnight',
            # 'interval': 1,
            # 'backupCount': 10,
            'formatter': 'verbose',
        },
        'api_sql_debug': {
            'level': 'DEBUG',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'filename': 'logs/api_sql_debug.log',
            'when': 'midnight',
            # 'interval': 1,
            # 'backupCount': 10,
            'formatter': 'verbose',
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
        },
    },
    'loggers': {
        'app_api': {
            'handlers': ['api_file_debug', 'console'],
            # 'level': 'DEBUG',
            'level': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG'),
            # 'propagate': True,
        },
        # https://docs.djangoproject.com/en/1.10/topics/logging/#django-db-backends
        'django.db.backends': {
            'handlers': ['api_sql_debug'],
            'level': 'DEBUG',
        },
    },
}


REST_FRAMEWORK = {
    # pagination
    #
    # Available params:
    # - offset
    # - limit: default is PAGE_SIZE
    # http://qiita.com/kimihiro_n/items/86e0a9e619720e57ecd8#%E3%83%9A%E3%82%B8%E3%83%8D%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
    'PAGE_SIZE': 20,  # TODO

    # filter
    # http://qiita.com/kimihiro_n/items/86e0a9e619720e57ecd8#%E3%83%95%E3%82%A3%E3%83%AB%E3%82%BF%E3%83%BC
    'DEFAULT_FILTER_BACKENDS': ('rest_framework.filters.DjangoFilterBackend',),
}

# django-nose
# http://akiyoko.hatenablog.jp/entry/2015/01/01/212712
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

TEST_APPS = (
    'api',
)

# NOSE_ARGS = [
#     '--with-coverage',
#     '--cover-package=' + ",".join(TEST_APPS),
#     '--cover-branches',
#     '--cover-erase',
# ]
