# -*- coding: utf-8 -*-
"""
Django settings for docker_django project.

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

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

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

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
from __future__ import absolute_import

import os
from datetime import timedelta

# schedules task config
import dj_database_url
from celery.schedules import crontab

# ==============================================================================
# Django 项目配置
# ==============================================================================
APP_ID = 'django_docker'
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
PROJECT_ROOT, PROJECT_MODULE_NAME = os.path.split(PROJECT_PATH)
# BASE_DIR = os.path.dirname(os.path.dirname(PROJECT_PATH))


TIME_ZONE = 'Asia/Shanghai'

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "5(15ds+i2+%ik6z&!yer+ga9m=e%jcqiz_5wszg)r-z!2--b2d"

# 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',
    # Using WhiteNoise in development
    'whitenoise.runserver_nostatic',
    'django.contrib.staticfiles',
    # 'gunicorn',
    'chat',
    'chartapp',
    'chartit',
    'fabric_master',
    'explore',
    'debug_toolbar',
    'debug_toolbar_line_profiler',
)

WHITENOISE_MAX_AGE = 30
# WHITENOISE_STATIC_PREFIX = '/static/'
# ===============================================================
# # celery configuration
INSTALLED_APPS += ("djcelery",)
# INSTALLED_APPS += ("kombu.transport.django",)
CELERY_ALWAYS_EAGER = False  # if set True, all tasks will be executed locally by blocking until the task returns
# CELERY_IMPORTS = ('chat.tasks', )
# CELERY_ENABLE_UTC = False
CELERY_TIMEZONE = TIME_ZONE
CELERYBEAT_SCHEDULER = "djcelery.schedulers.DatabaseScheduler"
CELERYD_CONCURRENCY = 8
CELERYD_MAX_TASKS_PER_CHILD = 100
CELERY_DISABLE_RATE_LIMITS = True
# CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
# CELERY_ACCEPT_CONTENT = ['json', 'yaml']
CELERY_ACCEPT_CONTENT = ['pickle', 'json', 'msgpack', 'yaml']
# import djcelery
# djcelery.setup_loader()
# ===============================================================

# # broker settings
BROKER_URL = 'django://'
# BROKER_URL = 'amqp://guest:guest@localhost:5672//'
# # list of modules to import when celery starts.
# # store schedule in the db
# CELERY_ANNOTATIONS = {'chat.tasks.hello_world': {'rate_limit': '1/s'}}
# http://blog.csdn.net/woshiaotian/article/details/36422781

# CELERY_IGNORE_RESULT = False
CELERY_RESULT_BACKEND = 'djcelery.backends.database:DatabaseBackend'

# Using the database to store task state and results.
# CELERY_RESULT_BACKEND = 'db+scheme://root:root@locahost:3306/django_docker'
# CELERY_RESULT_BACKEND = 'amqp://guest:guest@localhost:5672//'

# redis+socket:///path/to/redis.sock?virtual_host=db_number
# redis://:password@hostname:port/db_number
# CELERY_RESULT_BACKEND = "redis://localhost:6379/0"

# chord used but no CELERY_RESULT_BACKEND config
# [2016-04-22 19:17:39,536: INFO/MainProcess] Task celery.chord_unlock[c4a7ad4f-54f2-43e2-b684-0e81cc0ebb6c] retry: Retry in 1s: AttributeError("'DisabledBackend' object has no attribute '_get_task_meta_for'",)


CELERYBEAT_SCHEDULE = {
    'first_task': {
        'task': 'chat.tasks.hello_world',
        'args': [1, 2, 3],
        'kwargs': {},
        'schedule': timedelta(seconds=90)
    },
    'second_task': {
        'task': 'chat.tasks.add',
        'args': [1, 2],
        'schedule': crontab(hour=0, minute=1)
    }
}

# # queue config
# from kombu import Queue, Exchange
#
# default_exchange = Exchange(name='default', type='direct')
# direct_exchange = Exchange(name='Xdirect', type='direct')
# topic_exchange = Exchange(name='Xtopic', type='topic')
# CELERY_DEFAULT_QUEUE = 'default'  # celery -> default
# # define exchange, queue and binding
# CELERY_QUEUES = (
#     Queue('default', default_exchange, routing_key='default'),
#     Queue('QueueA', direct_exchange, routing_key='keyA'),
#     Queue('QueueB', direct_exchange, routing_key='keyB'),
# )
# # define task router, put task_A(B) to QueueA(B)
# CELERY_ROUTES = {
#     'chat.tasks.task_A': {
#         'queue': 'QueueA',
#         'routing_key': 'keyA',
#     },
#     'chat.tasks.task_B': {
#         'queue': 'QueueB',
#         'routing_key': 'keyB',
#     },
# }

CELERY_QUEUES = {
    "default_queue": {
        "exchange": "default_exchange",
        "exchange_type": "direct",
        "routing_key": "default_change"
    },
    "QueueA": {
        "exchange": "direct_exchange",
        "exchange_type": "direct",
        "routing_key": "keyA"
    },
    "QueueB": {
        "exchange": "direct_exchange",
        "exchange_type": "direct",
        "routing_key": "keyB"
    },
    # "topic_queue": {
    #     "exchange": "topic_exchange",
    #     "exchange_type": "topic",
    #     "routing_key": "topictest.#",
    # },
    # "fanout_queuea": {
    #     "exchange": "fanout_exchange",
    #     "exchange_type": "fanout",
    #     "binding_key": "fanout_bkeya",
    # },
    # "fanout_queueb": {
    #     "exchange": "fanout_exchange",
    #     "exchange_type": "fanout",
    #     "binding_key": "fanout_bkeyb",
    # },
}


class TaskRouter(object):
    def route_for_task(self, task, args=None, kwargs=None):
        if task.startswith('chat.tasks.task_A'):
            return {
                'queue': 'QueueA',
            }
        elif task.startswith('chat.tasks.task_B'):
            return {
                'queue': 'QueueB',
            }
        # elif task.startswith('chat.tasks.hello_world'):
        #     return {
        #         "exchange": "fanout_exchange",
        #     }
        #     return {'exchange': 'video',
        #             'exchange_type': 'topic',
        #             'routing_key': 'video.compress'}
        else:
            return {
                'queue': 'default_queue',
            }
            # else:
            #     return None


CELERY_ROUTES = (TaskRouter(),)
# ===============================================================
MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    # 'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    # 'django.middleware.cache.FetchFromCacheMiddleware',
    'debug_toolbar.middleware.DebugToolbarMiddleware',
)

ROOT_URLCONF = 'config.urls'

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.request",

)

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    # 'django.template.loaders.eggs.Loader',
)

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(BASE_DIR, "templates"),
)

WSGI_APPLICATION = 'config.wsgi.application'

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

DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.sqlite3',
         'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
     }
 }

# Update database configuration with $DATABASE_URL.
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Allow all host headers
ALLOWED_HOSTS = ['*']


#DAO_TEST = bool(os.environ.get('DAO_TEST'))

# DATABASES = {
#    'default': {
#        'ENGINE': 'django.db.backends.mysql',
#        'NAME': os.environ['MYSQL_INSTANCE_NAME'],
#        'USER': os.environ['MYSQL_USERNAME'],
#        'PASSWORD': os.environ['MYSQL_PASSWORD'],
#        'HOST': os.environ['MYSQL_PORT_3306_TCP_ADDR'],
#        'PORT': os.environ['MYSQL_PORT_3306_TCP_PORT']
#    }
# }

#DATABASES = {
#    'default': {
#        'ENGINE': 'django.db.backends.mysql',
#        'NAME': 'django_docker',
#        'USER': 'root',
#        'PASSWORD': 'root',
#        'HOST': 'localhost',
#        'PORT': 3306
#    }
#}

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

TIME_ZONE = 'Asia/Shanghai'

LANGUAGE_CODE = 'zh-hans'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# # ===============================================================================
# # 静态资源设置
# # ===============================================================================
# # 静态资源文件(js,css等）在应用上线更新后, 由于浏览器有缓存, 可能会造成没更新的情况.
# # 所以在引用静态资源的地方，都需要加上这个版本号，如：<script src="/a.js?v=${STATIC_VERSION}"></script>；
# # 如果静态资源修改了以后，上线前修改这个版本号即可
# STATICFILES_DIRS = (
#     os.path.join(PROJECT_ROOT, 'static'),
# )
STATIC_VERSION = 2.2

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
# ==============================================================================
# 应用运行环境配置信息
# ==============================================================================
ENVIRONMENT = os.environ.get('BK_ENV', 'development')
# 应用访问路径
SITE_URL = '/'
# 运行模式， DEVELOP(开发模式)， TEST(测试模式)， PRODUCT(正式模式)
RUN_MODE = 'DEVELOP'
if ENVIRONMENT.endswith('production'):
    RUN_MODE = 'PRODUCT'
    DEBUG = False
    SITE_URL = '/o/%s/' % APP_ID
elif ENVIRONMENT.endswith('testing'):
    RUN_MODE = 'TEST'
    DEBUG = False
    SITE_URL = '/t/%s/' % APP_ID
else:
    RUN_MODE = 'DEVELOP'
    DEBUG = True
TEMPLATE_DEBUG = DEBUG

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
#
# ROOT_URLCONF = 'urls'

# 应用本地静态资源目录
STATIC_URL = '%sstatic/' % SITE_URL


# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

# CACHES = {
#     'default': {
#         'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
#         'LOCATION': 'django_cache_table',
#         'TIMEOUT': 120,
#         'OPTIONS': {
#             'MAX_ENTRIES': 1000
#         }
#     }
# }
# python manage.py createcachetable

# sudo apt-get install memcache
# CACHES = {
#     'default': {
#         'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
#         'LOCATION': 'localhost:11211',
#         # 'LOCATION': 'unix:/tmp/memcached.sock',
#         # 'LOCATION': [
#         #     '172.19.26.240:11211',
#         #     '172.19.26.242:11211',
#         # ]
#     }
# }
# CACHES = {
#     'default': {
#         'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
#         'LOCATION': '/var/tmp/django_cache',
#     }
# }
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
    }
}
try:
    from .local_settings import *
except ImportError:
    pass


# ==============================================================================
# logging
# ==============================================================================
BK_LOG_DIR = os.environ.get('BK_LOG_DIR', '/data/paas/apps/logs/')
LOGGING_DIR = os.path.join('./logs', APP_ID)
LOG_CLASS = 'logging.handlers.RotatingFileHandler'
if RUN_MODE == 'DEVELOP':
    LOG_LEVEL = 'DEBUG'
elif RUN_MODE == 'TEST':
    LOGGING_DIR = os.path.join(BK_LOG_DIR, APP_ID)
    LOG_LEVEL = 'INFO'
elif RUN_MODE == 'PRODUCT':
    LOGGING_DIR = os.path.join(BK_LOG_DIR, APP_ID)
    LOG_LEVEL = 'ERROR'


# ==============================================================================
# Templates
# ==============================================================================
# mako template dir
MAKO_TEMPLATE_DIR = os.path.join(PROJECT_ROOT, 'templates')
MAKO_TEMPLATE_MODULE_DIR = os.path.join(BASE_DIR, 'templates_module', APP_ID)
if RUN_MODE not in ['DEVELOP']:
    MAKO_TEMPLATE_MODULE_DIR = os.path.join(PROJECT_ROOT, 'templates_module', APP_ID)
# Django TEMPLATES配置
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(PROJECT_ROOT, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # the context to the templates
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.request',
                'django.template.context_processors.csrf',
                'common.context_processors.mysetting',   # 自定义模版context，可在页面中使用STATIC_URL等变量
                'django.template.context_processors.i18n',
            ],
        },
    },
]


# 自动建立日志目录
if not os.path.exists(LOGGING_DIR):
    try:
        os.makedirs(LOGGING_DIR)
    except:
        pass

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s [%(asctime)s] %(pathname)s %(lineno)d %(funcName)s %(process)d %(thread)d \n \t %(message)s \n',
            'datefmt': '%Y-%m-%d %H:%M:%S'
        },
        'simple': {
            'format': '%(levelname)s %(message)s \n'
        },
    },
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class': 'django.utils.log.NullHandler',
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'root': {
            'class': LOG_CLASS,
            'formatter': 'verbose',
            'filename': os.path.join(LOGGING_DIR, '%s.log' % APP_ID),
            'maxBytes': 1024 * 1024 * 10,
            'backupCount': 5
        },
        'component': {
            'class': LOG_CLASS,
            'formatter': 'verbose',
            'filename': os.path.join(LOGGING_DIR, 'component.log'),
            'maxBytes': 1024 * 1024 * 10,
            'backupCount': 5
        },
        'wb_mysql': {
            'class': LOG_CLASS,
            'formatter': 'verbose',
            'filename': os.path.join(LOGGING_DIR, 'wb_mysql.log'),
            'maxBytes': 1024 * 1024 * 4,
            'backupCount': 5
        },
    },
    'loggers': {
        'django': {
            'handlers': ['null'],
            'level': 'INFO',
            'propagate': True,
        },
        'django.request': {
            'handlers': ['console'],
            'level': 'ERROR',
            'propagate': True,
        },
        # the root logger ,用于整个project的logger
        'root': {
            'handlers': ['root'],
            'level': LOG_LEVEL,
            'propagate': True,
        },
        # 组件调用日志
        'component': {
            'handlers': ['component'],
            'level': 'WARN',
            'propagate': True,
        },
        # other loggers...
        'django.db.backends': {
            'handlers': ['wb_mysql'],
            'level': 'DEBUG',
            'propagate': True,
        },
    }
}

#===============================================================================
# DEBUG_TOOLBAR 配置
#===============================================================================
DEBUG_TOOLBAR_PANELS = [
    # 'debug_toolbar.panels.versions.VersionsPanel',
    # 'debug_toolbar.panels.timer.TimerPanel',
    # 'debug_toolbar.panels.settings.SettingsPanel',
    # 'debug_toolbar.panels.headers.HeadersPanel',
    # 'debug_toolbar.panels.request.RequestPanel',
    # 'debug_toolbar.panels.sql.SQLPanel',
    # 'debug_toolbar.panels.staticfiles.StaticFilesPanel',
    # 'debug_toolbar.panels.templates.TemplatesPanel',
    # 'debug_toolbar.panels.cache.CachePanel',
    # 'debug_toolbar.panels.signals.SignalsPanel',
    # 'debug_toolbar.panels.logging.LoggingPanel',
    # 'debug_toolbar.panels.redirects.RedirectsPanel',
    # 'debug_toolbar.panels.profiling.ProfilingPanel',
    'debug_toolbar_line_profiler.panel.ProfilingPanel',
    # 'debug_toolbar_user_panel.panels.UserPanel',
]

# from config.settings_env import STATIC_URL

DEBUG_TOOLBAR_CONFIG = {
    # Toolbar options
    'JQUERY_URL': '%s%s' % (STATIC_URL, 'jquery.min.js'),
    # Panel options
    # 'DISABLE_PANELS': ['debug_toolbar.panels.cache.CachePanel'],
    'SQL_WARNING_THRESHOLD': 100,   # milliseconds
}
DEBUG_TOOLBAR_PATCH_SETTINGS = False

# The Debug Toolbar is shown only if your IP is listed in the INTERNAL_IPS setting.
INTERNAL_IPS = ("localhost", "127.0.0.1")