"""
Django settings for smartnews 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, ...)
import os
import sys
from cassandra import ConsistencyLevel


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '!$s$(s*l8mv5s0&g3a4fg*%^-#-8w)!up@03d+#e$d1n8fici1'

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

ALLOWED_HOSTS = []


# Application definition

STATIC_URL = '/static/'
STATIC_ROOT = 'static/'

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'djcelery',
    'scraper_module',
)
# TODO: Otra vez sopa.. Ver como evitar esto
# INSTALLED_APPS = ('django_cassandra_engine',) + INSTALLED_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',
    'django.middleware.security.SecurityMiddleware',
)

FILE_UPLOAD_HANDLERS = (
    'django.core.files.uploadhandler.TemporaryFileUploadHandler',
)

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


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

DATABASES={
}

DATABASES['default'] = {
    'ENGINE': 'django_cassandra_engine',
    'NAME': 'smartnews_dev',
    'USER': 'cassandra',
    'PASSWORD': 'cassandra',
    'TEST_NAME': 'db_smartnews_dev',
    'HOST': '127.0.0.1',
    'OPTIONS': {
        'replication': {
            'strategy_class': 'SimpleStrategy',
            'replication_factor': 1
        },
        'connection': {
            'consistency': ConsistencyLevel.ONE,
            'lazy_connect': True,
            'retry_connect': True
            # + All connection options for cassandra.cluster.Cluster()
        },
        'session': {
            'default_timeout': 10,
            'default_fetch_size': 10000
            # + All options for cassandra.cluster.Session()
        }
    }
}


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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'

# CELERY
CELERY_ALWAYS_EAGER = False
BROKER_URL = 'amqp://guest:guest@localhost:5672//'
# CELERY_RESULT_BACKEND = 'redis://?new_join=1'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
CELERY_CHORD_PROPAGATES = True


# Only add pickle to this list if your broker is secured
CELERY_ACKS_LATE = False
CELERY_ACCEPT_CONTENT = ['pickle']
CELERY_TASK_SERIALIZER = 'pickle'
CELERY_RESULT_SERIALIZER = 'pickle'
CELERY_STORE_ERRORS_EVEN_IF_IGNORED = False
# The 'CELERYD_LOG_FILE' setting is scheduled for deprecation in  version 2.4 and removal in version v4.0.
CELERYD_LOG_FILE = '/var/log/scrapers.log'

# http://docs.celeryproject.org/en/master/configuration.html#celeryd-task-time-limit
CELERYD_TASK_SOFT_TIME_LIMIT = 60 * 60 * 60  # 5 minutes
CELERYD_TASK_TIME_LIMIT = CELERYD_TASK_SOFT_TIME_LIMIT + 10
CELERYD_HIJACK_ROOT_LOGGER = False

BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 3600}

#DISCOGS API CONSTANTS
DISCOGS_PUBLIC_KEY = 'qraKoVdMtegdzntquyXkBXqjjRKaZArEKfUcRHly'

TESTING = sys.argv[1:2] == ['test']
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

NOSE_ARGS = ['--verbosity=2', '--with-coverage', '--cover-erase',
             '--cover-html', '--cover-html-dir=/vagrant/python-tests/',
             '--cover-package=web']

STOP_WORDS_DIR = '/home/vagrant/stop_words/'

import ssl
from functools import wraps

# workaround for ssl errors based on this issue:
# https://github.com/rg3/youtube-dl/issues/4578
def sslwrap(func):
    @wraps(func)
    def bar(*args, **kw):
        kw['ssl_version'] = ssl.PROTOCOL_TLSv1
        return func(*args, **kw)
    return bar

ssl.wrap_socket = sslwrap(ssl.wrap_socket)
os.environ.setdefault('STOP_WORDS_DIR','/home/vagrant/stop_words/')

from stop_words import get_stop_words
STOP_WORDS = get_stop_words('spanish')