
"""
Django settings for rowsandall_app project.

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

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

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""
# -*- coding: utf-8 -*-
import os
from YamJam import yamjam
from django.utils.translation import ugettext_lazy as _

# Read configuration (passwords, keys, secrets) from YamJam configuration
# You have to create your own config.yaml in the project directory
CFG = yamjam()['rowsandallapp']

DEFAULT_CHARSET = 'UTF-8'

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = CFG['secret_key']


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

ALLOWED_HOSTS = CFG['allowed_hosts']


# Application definition

INSTALLED_APPS = [
    'rowers',
    'cvkbrno',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'suit',
    'suit_rq',
    'leaflet',
    'django_rq',
    'django_rq_dashboard',
    'translation_manager',
    'django_mailbox',
    'rest_framework',
    'datetimewidget',
    'rest_framework_swagger',
    'oauth2_provider',
    'corsheaders',
    'analytical',
    'cookielaw',
    'django_extensions',
    'tz_detect',
    'django_social_share',
]

AUTHENTICATION_BACKENDS = (
    'oauth2_provider.backends.OAuth2Backend',
    # Uncomment following if you want to access the admin
    'django.contrib.auth.backends.ModelBackend',
)

MIDDLEWARE_CLASSES = [
#    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.common.BrokenLinkEmailsMiddleware',
    'django.middleware.gzip.GZipMiddleware',
#    'htmlmin.middleware.HtmlMinifyMiddleware',
    'htmlmin.middleware.MarkRequestMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'oauth2_provider.middleware.OAuth2TokenMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'async_messages.middleware.AsyncMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'tz_detect.middleware.TimezoneMiddleware',
    'rowers.middleware.GDPRMiddleWare',
    'rowers.middleware.PowerTimeFitnessMetricMiddleWare',
]

ROOT_URLCONF = 'rowsandall_app.urls'
#HTML_MINIFY = True

#EXCLUDE_FROM_MINIFYING = ('^rowers/flexall',
#                          '^rowers/list-workouts',
#                          '^rowers/list-graphs',
#                          '^admin/',
#                          '^rowers/histo')

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(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',
		'django.template.context_processors.i18n',
#                'context_processors.google_analytics',
                'context_processors.warning_message',
            ],
#	    'loaders': [
#		'django.template.loaders.app_directories.Loader',
#	    ],
	    },
	
    },
]


CORS_ORIGIN_ALLOW_ALL = True

WSGI_APPLICATION = 'rowsandall_app.wsgi.application'


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

DATABASES = {
    'default': {
	'ENGINE': 'django.db.backends.mysql',
	'NAME': CFG['db_name'],
	'USER': CFG['db_user'],
	'PASSWORD': CFG['db_password'],
	'HOST': CFG['db_host'],
	'PORT': CFG['db_port'],
    },
    'slave': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
	},
    'TEST': {
	'CHARSET': 'utf8',
	'COLLATION': 'utf8_general_ci',
	},
}


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


USE_I18N = True

USE_L10N = True

USE_TZ = True
TIME_ZONE = 'UTC'

TZ_DETECT_COUNTRIES = ('US','DE','GB','CZ','FR','IT')

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),    
    os.path.join(BASE_DIR, 'cvkbrno/locale'),    
    )

LANGUAGES = (
    ('cs',_(u'Czech')),
    ('nl',_(u'Dutch'))
    )

LANGUAGE_CODE = 'en-us'
LANGUAGE_COOKIE_NAME = 'wm_lang'

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

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),
		    os.path.join(BASE_DIR, 'static/plots'),]

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')



# user authentication

# user authentication
LOGIN_REDIRECT_URL = '/rowers/list-workouts/'
LOGIN_URL = '/login/'
LOGOUT_URL = '/logout/'

# Update Cache with task progress password

PROGRESS_CACHE_SECRET = CFG['progress_cache_secret']

# Concept 2
C2_CLIENT_ID = CFG['c2_client_id']
C2_CLIENT_SECRET = CFG['c2_client_secret']
C2_REDIRECT_URI = CFG['c2_callback']
#C2_REDIRECT_URI = "http://localhost:8000/call_back"

# Strava

STRAVA_CLIENT_ID = CFG['strava_client_id']
STRAVA_CLIENT_SECRET = CFG['strava_client_secret']
STRAVA_REDIRECT_URI = CFG['strava_callback']


# SportTracks

SPORTTRACKS_CLIENT_ID = CFG['sporttracks_client_id']
SPORTTRACKS_CLIENT_SECRET = CFG['sporttracks_client_secret']
SPORTTRACKS_REDIRECT_URI = CFG['sporttracks_callback']

# Runkeeper

RUNKEEPER_CLIENT_ID = CFG['runkeeper_client_id']
RUNKEEPER_CLIENT_SECRET = CFG['runkeeper_client_secret']
RUNKEEPER_REDIRECT_URI = CFG['runkeeper_callback']

# Polar Flow

POLAR_CLIENT_ID = CFG['polarflow_client_id']
POLAR_CLIENT_SECRET = CFG['polarflow_client_secret']
POLAR_REDIRECT_URI = CFG['polarflow_callback']

# Under Armour

UNDERARMOUR_CLIENT_ID = CFG['underarmour_client_name']
UNDERARMOUR_CLIENT_SECRET = CFG['underarmour_client_secret']
UNDERARMOUR_CLIENT_KEY = CFG['underarmour_client_key']
UNDERARMOUR_REDIRECT_URI = CFG['underarmour_callback']
#UNDERARMOUR_REDIRECT_URI = "http://rowsandall.com/underarmour_callback"
#UNDERARMOUR_REDIRECT_URI = "http://localhost:8000/underarmour_callback"

# TrainingPeaks
TP_CLIENT_ID = CFG["tp_client_id"]
TP_CLIENT_SECRET = CFG["tp_client_secret"]
TP_REDIRECT_URI = CFG["tp_redirect_uri"]
TP_CLIENT_KEY = TP_CLIENT_ID

# Full Site URL
SITE_URL = CFG['site_url']

# RQ stuff

RQ_QUEUES = {
    'default': {
        'HOST': 'localhost',
        'PORT': 6379,
        'DB': 0,
#        'PASSWORD': 'some-password',
        'DEFAULT_TIMEOUT': 360,
    },
    'low': {
        'HOST': 'localhost',
        'PORT': 6379,
        'DB': 0,
#        'PASSWORD': 'some-password',
        'DEFAULT_TIMEOUT': 360,
    },
}


#SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies"
#SESSION_ENGINE = "django.contrib.sessions.backends.cached_db"
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_SAVE_EVERY_REQUEST = True

# admin stuff for error reporting
SERVER_EMAIL='admin@rowsandall.com'
ADMINS = [('Sander','roosendaalsander@gmail.com')]

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': 'localhost:11211',
        'TIMEOUT': 900,
        }
    }

CACHE_MIDDLEWARE_ALIAS = 'default'
CACHE_MIDDLEWARE_SECONDS = 900

# email stuff

#EMAIL_BACKEND = CFG['email_backend']
#EMAIL_HOST = CFG['email_host']
#EMAIL_PORT = CFG['email_port']
#EMAIL_HOST_USER = CFG['email_host_user']
#EMAIL_HOST_PASSWORD = CFG['email_host_password']
#EMAIL_USE_TLS = CFG['email_use_tls']
#DEFAULT_FROM_EMAIL = 'admin@rowsandall.com'

EMAIL_BACKEND = 'django_ses.SESBackend'

AWS_SES_REGION_NAME = 'eu-west-1'
AWS_SES_REGION_ENDPOINT = 'email.eu-west-1.amazonaws.com'

EMAIL_HOST = CFG['aws_smtp']
EMAIL_PORT = CFG['aws_port']
EMAIL_HOST_USER = CFG['aws_smtp_username']
EMAIL_HOST_PASSWORD = CFG['aws_smtp_password']
EMAIL_USE_TLS = CFG['email_use_tls']
DEFAULT_FROM_EMAIL = 'info@rowsandall.com'


# weather stuff

FORECAST_IO_KEY = CFG['forecast_io_key']
GMAPIKEY = CFG['gmapikey']

# OAUTH2

OAUTH2_PROVIDER = {
    # this is the list of available scopes
    'SCOPES': {'read': 'Read scope', 'write': 'Write scope', 'groups': 'Access to your groups'},
    'ALLOWED_REDIRECT_URI_SCHEMES': ["http",
                                     "https",
                                     "rowingcoachexport"]
    # 'OAUTH2_BACKEND_CLASS': 'oauth2_provider.oauth2_backends.JSONOAuthLibCore'
}


# REST Framework

REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated'
#        'rest_framework.permissions.DjangoModelPermissions'
    ],
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'oauth2_provider.ext.rest_framework.OAuth2Authentication',
    ),
    'PAGE_SIZE': 20,
}

SWAGGER_SETTINGS = {
    'SECURITY_DEFINITION': {
        'basic': {
            'type':'basic'
            },
        'oauth2': {
            'type':'oauth2',
            'authorizationUrl':'/rowers/o/authorize',
            'flow': 'implicit',
            }
        },
    'SHOW_REQUEST_HEADERS': True,
    }

# Analytics

CLICKY_SITE_ID = CFG['clicky_site_id']

GOOGLE_ANALYTICS_PROPERTY_ID = CFG['google_analytics_id']
GOOGLE_ANALYTICS_DOMAIN = CFG['google_analytics_domain']

TWEET_ACCESS_TOKEN_KEY = CFG['tweet_access_token_key']
TWEET_ACCESS_TOKEN_SECRET = CFG['tweet_access_token_secret']
TWEET_CONSUMER_KEY = CFG['tweet_consumer_key']
TWEET_CONSUMER_SECRET = CFG['tweet_consumer_secret']

# Environment

try:
    WARNING_MESSAGE = CFG['warning_message']
except KeyError:
    WARNING_MESSAGE = ''

SETTINGS_NAME = 'rowsandall_app.settings'
