"""
Django settings for snafoo project.

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

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, ...)
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/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ''  # Define me in local_settings.py.

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'snacksdb',
    'widget_tweaks',
]

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

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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 = 'snafoo.wsgi.application'


# Cache
# https://docs.djangoproject.com/en/2.0/topics/cache/
# N.B.: If you have multiple request-handling threads, DO NOT use a
# thread local cache backend (e.g. LocMemCache). MemCachedCache is
# the recommended cache backend for a production instance.

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
    }
}


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

DATABASES = {}  # Define me in local_settings.py.


# 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 = 'America/Chicago'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

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

# +------------------------------------------------------------------------------------------------+
# |                                                                                                |
# |                                   django.contrib.messages                                      |
# |                                                                                                |
# +------------------------------------------------------------------------------------------------+

# Change default message tags to things that bootstrap recognizes.
# https://docs.djangoproject.com/en/2.0/ref/contrib/messages/#message-tags
from django.contrib.messages import constants as messages
MESSAGE_TAGS = {
    messages.DEBUG: 'alert-info',
    messages.INFO: 'alert-info',
    messages.SUCCESS: 'alert-success',
    messages.WARNING: 'alert-warning',
    messages.ERROR: 'alert-danger',
}

# +------------------------------------------------------------------------------------------------+
# |                                                                                                |
# |                                           snafoo                                               |
# |                                                                                                |
# +------------------------------------------------------------------------------------------------+

SNACK_BACKEND_API_KEY = None  # Define me in local_settings.py.
VOTES_PER_MONTH = 3
NOMINATIONS_PER_MONTH = 1
SNACK_SOURCE_CLASS = 'snacksdb.utils.SnackAPISource.SnackAPISource'

# +------------------------------------------------------------------------------------------------+
# |                                                                                                |
# |                 local_settings.py; don't declare anything after this banner!                   |
# |                                                                                                |
# +------------------------------------------------------------------------------------------------+

from .local_settings import *

# Add the EC2 instance's private IPv4 address to ALLOWED_HOSTS
# so that health checks from the ALB will succeed.
import requests

# Amazon maintains a REST API for retrieving instance metadata.
try:
    response = requests.get('http://169.254.169.254/latest/meta-data/local-ipv4')
except requests.exceptions.ConnectionError:
    # If can't connect to the remote host, we're probably in a dev environment.
    pass
else:
    # If the response isn't 200, maybe the server wasn't provisioned with AWS?
    # Anyways, the request wasn't successful, so don't do anything with the response.
    if response.status_code == 200:
        ALLOWED_HOSTS.append(response.text) if response.text not in ALLOWED_HOSTS else None
