"""
Django settings for anodyne project.

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

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

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

import os

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

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '2=4xw8rppm!#v&4kicf#2nwqx!!ekg=vk(vr(qapx*5_nvm0c&'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_LOCATION = os.path.dirname(os.path.dirname(__file__))
ALLOWED_HOSTS = ['*']

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'rest_framework',
    'rest_framework.authtoken',
    'api',
    'dashboard',
    'django_filters',
    'crispy_forms',
    'django.contrib.postgres',
]

REST_FRAMEWORK = {
    # TODO: to user Token authentication uncomment below
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework_simplejwt.authentication.JWTAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ],
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
    'PAGE_SIZE': 100,
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    )

}

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    '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 = 'anodyne.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_LOCATION, 'api', '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',
            ],
        },
    },
]
CORS_ORIGIN_ALLOW_ALL = True

WSGI_APPLICATION = 'anodyne.wsgi.application'

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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'anodyne_db',
        'USER': 'anodyne',
        'PASSWORD': '@n0dyn3',
        'HOST': 'ec2-15-206-209-236.ap-south-1.compute.amazonaws.com',
        'PORT': '5432',
    }
}
# Password validation
# https://docs.djangoproject.com/en/2.0/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/2.0/topics/i18n/

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Kolkata'
USE_I18N = True
USE_L10N = True
USE_TZ = False

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_ROOT = os.path.join(BASE_LOCATION, 'api', 'static')
STATIC_URL = '/static/'

# STATIC_ROOT = os.path.join(BASE_LOCATION, 'static')
# STATIC_URL = '/static/'
# STATICFILES_DIRS = (
#     os.path.join(BASE_DIR, 'static'),
# )
# ValidationError at /api/ if below commented
# specifying the User model to be used for authentication
AUTH_USER_MODEL = 'api.User'

ANONYMOUS_USER_NAME = 'AnonymousUser'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'vepolink@anodyne.in'
EMAIL_HOST_PASSWORD = 'awecdelhi@340'
APP_NAME = 'VepoLink'
#https://myaccount.google.com/security
# https://myaccount.google.com/lesssecureapps

# Logging Setup
# can set environ LOGLEVEL=debug/info/error etc
LOGLEVEL = os.environ.get('LOGLEVEL', 'debug').upper()
LOG_PATH = os.path.join('/var/log/', 'vepolink')
TMP_PATH = '/tmp/'
if not os.path.exists(LOG_PATH):
    os.makedirs(LOG_PATH)

LOG_FILE = os.path.join(LOG_PATH, 'vepolink.log')
CELERY_LOG_FILE = os.path.join(LOG_PATH, 'celerytasks.log')
REQ_LOG_FILE = os.path.join(LOG_PATH, 'requests.log')
READINGS_LOG_FILE = os.path.join(LOG_PATH, 'readings.log')
VIEW_LOG_FILE = os.path.join(LOG_PATH, 'views.log')
RECEPTION_LOG_FILE = os.path.join(LOG_PATH, 'reception.log')
ALERT_LOG_FILE = os.path.join(LOG_PATH, 'alerts.log')

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format': '%(asctime)s (%(levelname)s) [%(module)s:%(lineno)d]: %(message)s',
        },
    },
    'handlers': {
        'default': {
            'level': LOGLEVEL,
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': LOG_FILE,
            'maxBytes': 1024 * 1024 * 50,  # 50 MB
            'backupCount': 10,
            'formatter': 'standard',
        },
        'celery_logs': {
            'level': LOGLEVEL,
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': CELERY_LOG_FILE,
            'maxBytes': 1024 * 1024 * 2,  # 4 MB
            'backupCount': 4,
            'formatter': 'standard',
        },
        'reception_logs': {
            'level': LOGLEVEL,
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': RECEPTION_LOG_FILE,
            'maxBytes': 1024 * 1024 * 40,  # 4 MB
            'backupCount': 5,
            'formatter': 'standard',
        },
        'request_handler': {
            'level': LOGLEVEL,
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': REQ_LOG_FILE,
            'maxBytes': 1024 * 1024 * 2,  # 2 MB
            'backupCount': 2,
            'formatter': 'standard',
        },
        'reading_logs': {
            'level': LOGLEVEL,
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': READINGS_LOG_FILE,
            'maxBytes': 1024 * 1024 * 2,  # 2 MB
            'backupCount': 2,
            'formatter': 'standard',
        },
        'view_logs': {
            'level': LOGLEVEL,
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': VIEW_LOG_FILE,
            'maxBytes': 1024 * 1024 * 2,  # 2 MB
            'backupCount': 2,
            'formatter': 'standard',
        },
        'alert_logs': {
            'level': LOGLEVEL,
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': ALERT_LOG_FILE,
            'maxBytes': 1024 * 1024 * 4,  # 4 MB
            'backupCount': 2,
            'formatter': 'standard',
        },
        'console': {
            'class': 'logging.StreamHandler',
            'stream': sys.stdout,
        },
    },
    'loggers': {
        'vepolink': {
            'handlers': ['console', 'default'],
            'level': 'DEBUG',
            'propagate': True
        },
        'alerts': {
            'handlers': ['alert_logs'],
            'level': 'DEBUG',
            'propagate': True
        },
        'django.request': {
            'handlers': ['request_handler'],
            'level': 'DEBUG',
            'propagate': False
        },
        'celery': {
            'handlers': ['celery_logs'],
            'propagate': True
        },
        'reception': {
            'handlers': ['reception_logs'],
            'level': 'DEBUG',
        },
        'readings': {
            'handlers': ['reading_logs'],
            'level': 'DEBUG',
        },
        'views': {
            'handlers': ['view_logs'],
            'level': 'DEBUG',
        },
    }
}
DATE_FMT = '%Y-%m-%d %H:%M:%S'
CRISPY_TEMPLATE_PACK = 'bootstrap4'
# The value can be a hardcoded URL or a URL name. (login_required)
# The default value for LOGIN_REDIRECT_URL is /accounts/profile/.
LOGIN_REDIRECT_URL = 'login/'