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

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

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

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


reload(sys)
sys.setdefaultencoding('utf-8')

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '(cls2rl1ax0u5!3$d#7ilr++x2ra!@wn!0ysa$d1^^$*_4d2*g'


# This value can be used to know whether it is currently a test instance or not.
TESTING = any("py.test" in s for s in sys.argv) or 'test' in sys.argv

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

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

# Application definition

ESEFPY_APPS = (
    'esefpy.audit',
    'esefpy.web.content',
    'esefpy.web.esefauth',
    'esefpy.web.nav',
    'esefpy.web.utils',
)

DJANGO_APPS = (
    'django.contrib.contenttypes',
    'django.contrib.auth',
    'django.contrib.admin',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

THIRD_PARTY_APPS = (
    'mptt',
    'rest_framework',
    'translations',
    'bower',
    'guardian',
    'rest_framework_jwt',
    'rest_framework.authtoken',
    'rest_auth',
    'reversion',
    'flat'
)

LOCAL_APPS = (
    'apps.defaultapp',
)

INSTALLED_APPS = ESEFPY_APPS + THIRD_PARTY_APPS + DJANGO_APPS + LOCAL_APPS

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    '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',
    'esefpy.common.django.middlewares.tls.TLSMiddleware',
    'esefpy.audit.middlewares.WatchMiddleware',
)

DEFAULT_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.static",
    "django.core.context_processors.tz",
)

ESEF_TEMPLATE_CONTEXT_PROCESSORS = (
    'esefpy.web.utils.context_processors.egemswatch',
    'esefpy.web.utils.context_processors.esef_brand',
    'esefpy.web.utils.context_processors.esef_brand_logo',
    'esefpy.web.utils.context_processors.esef_footer',
)

TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_TEMPLATE_CONTEXT_PROCESSORS + ESEF_TEMPLATE_CONTEXT_PROCESSORS

ROOT_URLCONF = 'apps.defaultapp.urls'

WSGI_APPLICATION = 'apps.defaultapp.wsgi.application'

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

LANGUAGE_CODE = 'tr'
TIME_ZONE = 'Europe/Istanbul'
USE_I18N = True
USE_L10N = True
USE_TZ = True
DATE_FORMAT = 'd-m-Y H:i:s'
DATETIME_FORMAT = 'j M Y H:i:s'
LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale/'),
)


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

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'etc/static_collected')


# 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',
)

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

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

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'esefpy.web.esefauth.backends.sf_django_model_permission.SfDjangoModelPermissions',
        'esefpy.web.esefauth.backends.sf_django_api_permission.SfDjangoAPIPermissions',
    ],
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.BrowsableAPIRenderer',
        'rest_framework.renderers.JSONRenderer'
    ),
    'EXCEPTION_HANDLER': 'esefpy.web.esefauth.backends.sf_exception_handler.sf_exception_handler',
}

AUTHENTICATION_BACKENDS = (
    'esefpy.web.esefauth.backends.sf_authenticate_backend.SfAuthenticateBackend',
    'guardian.backends.ObjectPermissionBackend',
)

ANONYMOUS_USER_ID = -1
#
# LOGIN_URL = '/login'
# LOGIN_REDIRECT_URL = '/'
# LOGOUT_URL = '/logout/'

# Esef Settings
# Brand text
ESEF_BRAND = 'EPY Seed'
EGEMSWATCH_THEME = 'steel'  # Egemswatch theme name to fit egemswatch.{{ EGEMSWATCH_THEME }}.css
# Brand logo url
# ESEF_BRAND_LOGO = STATIC_URL + 'esefseed/src/images/logo.png'
# Footer text
ESEF_FOOTER = ESEF_BRAND + " 2014, Egemsoft"

PROJECT_NAME = None

AUTHENTICATION_BACKENDS = (
    'esefpy.web.esefauth.backends.sf_authenticate_backend.SfAuthenticateBackend',
)

AUTH_USER_MODEL = "esefauth.User"
REST_AUTH_SERIALIZERS = {
    'USER_DETAILS_SERIALIZER': 'esefpy.web.esefauth.serializers.user_details_serializer.UserDetailsSerializer'
}

OLD_PASSWORD_FIELD_ENABLED = True



# CELERY
USING_CELERY = False

if USING_CELERY:
    BROKER_URL = 'amqp://guest:guest@0.0.0.0:5672//'
    CELERY_RESULT_BACKEND = 'djcelery.backends.database.DatabaseBackend'
    CELERYD_LOG_LEVEL = 'DEBUG'

    CELERYBEAT_SCHEDULER = 'esefpy.common.celery.scheduler.ha_database_scheduler.HADatabaseScheduler'

    CELERY_TIMEZONE = 'Europe/Istanbul'
    CELERY_SEND_EVENTS = True

    from djcelery import setup_loader

    setup_loader()




# Email Settings
DEFAULT_FROM_EMAIL = 'EsefSeed'
SERVER_EMAIL = 'EsefSeed'
EMAIL_USE_TLS = True
EMAIL_PORT = 25
EMAIL_HOST_USER = "notification"
EMAIL_HOST_PASSWORD = "n123456"
EMAIL_HOST = "195.175.250.71"


# Logging Settings
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'default': {
            'format': '(%(module)s) (%(name)s) (%(asctime)s) (%(levelname)s) %(message)s',
            'datefmt': "%Y-%b-%d %H:%M:%S"
        }
    },
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler',
            'formatter': 'default',

        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'default',
        },
        'defaultapp': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'maxBytes': 1024 * 1024 * 5,  # 5 MB,
            'backupCount': 0,
            'filename': 'etc/logs/defaultapp.log',
            'formatter': 'default',
        },
    },
    'root': {
        'handlers': ['console'],
        'level': 'ERROR'
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': 'ERROR'
        },
        'apps.defaultapp': {
            'handlers': ['console', 'defaultapp'],
            'level': 'DEBUG'
        }
    }
}