"""
Django settings for jdav_web project.

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

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

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

import os

deployed = '1' == os.environ.get('DJANGO_DEPLOY', '0')

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY',
			    '6_ew6l1r9_4(8=p8quv(e8b+z+k+*wm7&zxx%mcnnec99a!lpw')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get('DJANGO_DEBUG', '1') == '1'

ALLOWED_HOSTS = os.environ.get('DJANGO_ALLOWED_HOST', '').split(",")

# Define media paths e.g. for image storage
MEDIA_URL = '/media/'
MEDIA_ROOT = os.environ.get('DJANGO_MEDIA_ROOT',
			    os.path.join((os.path.join(BASE_DIR, os.pardir)), "media"))
MEDIA_MEMBERLISTS = os.path.join((os.path.join(BASE_DIR, os.pardir)), "media")

# default primary key auto field type
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

# prevent large files from being unreadable by the server
# see
# https://stackoverflow.com/questions/51439689/django-nginx-error-403-forbidden-when-serving-media-files-over-some-size
FILE_UPLOAD_PERMISSIONS = 0o644

# x forward

USE_X_FORWARDED_HOST = True

# Application definition

INSTALLED_APPS = [
    'startpage.apps.StartpageConfig',
    'material.apps.MaterialConfig',
    'members.apps.MembersConfig',
    'mailer.apps.MailerConfig',
    'ludwigsburgalpin.apps.LudwigsburgalpinConfig',
    #'easy_select2',
    'djcelery_email',
    'django_celery_beat',
    'jet',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'jdav_web.middleware.ForceLangMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'jdav_web.urls'

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',
            ],
        },
    },
]

WSGI_APPLICATION = 'jdav_web.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
	'NAME': os.environ.get('DJANGO_DATABASE_NAME', 'jdav_db'),
	'OPTIONS': {
		'read_default_file': os.environ.get('DJANGO_DATABASE_CONFIG',
						    os.path.join(BASE_DIR, 'my.cnf'))
	},
    }
}


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

LANGUAGE_CODE = 'de-de'

TIME_ZONE = 'Europe/Berlin'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static")
]
# static root where all the static files are collected to
# use python3 manage.py collectstatic to collect static files in the STATIC_ROOT
# this is needed for deployment
STATIC_ROOT = os.environ.get('DJANGO_STATIC_ROOT',
			     os.path.join((os.path.join(BASE_DIR, os.pardir)), "static"))


# Locale files (translations)

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


# Email setup

EMAIL_HOST = os.environ.get('EMAIL_HOST', 'localhost')
EMAIL_PORT = 587 if deployed else 25
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER', '')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD', '')
EMAIL_USE_TLS = True if deployed else False
EMAIL_BACKEND = 'djcelery_email.backends.CeleryEmailBackend'


# Celery Email Setup

CELERY_EMAIL_TASK_CONFIG = {
    'rate_limit' : '1/m'  # * CELERY_EMAIL_CHUNK_SIZE (default: 10)
}


# Admin setup

ADMINS = (('admin', 'christian@merten-moser.de'),)


# Celery and Redis setup
BROKER_URL = os.environ.get('BROKER_URL', 'redis://localhost:6379/0')

# JET options (admin interface)

JET_SIDE_MENU_COMPACT = True
JET_DEFAULT_THEME = 'jdav-green'
JET_CHANGE_FORM_SIBLING_LINKS = False

JET_SIDE_MENU_ITEMS = [
    {'app_label': 'auth', 'permissions': ['auth'], 'items': [
        {'name': 'group', 'permissions': ['auth.group'] },
        {'name': 'user', 'permissions': ['auth.user']},
    ]},
    {'app_label': 'django_celery_beat', 'permissions': ['django_celery_beat'], 'items': [
        {'name': 'crontabschedule'},
        {'name': 'clockedschedule'},
        {'name': 'intervalschedule'},
        {'name': 'periodictask'},
        {'name': 'solarschedule'},
    ]},
    {'app_label': 'ludwigsburgalpin', 'permissions': ['ludwigsburgalpin'], 'items': [
        {'name': 'termin'},
    ]},
    {'app_label': 'mailer', 'items': [
        {'name': 'message'},
        {'name': 'emailaddress'},
    ]},
    {'app_label': 'members', 'items': [
        {'name': 'member'},
        {'name': 'group'},
        {'name': 'memberlist', 'permissions': ['members.view_memberlist']},
        {'name': 'membernotelist'},
        {'name': 'freizeit'},
        {'name': 'klettertreff'},
        {'name': 'activitycategory', 'permissions': ['members.view_activitycategory']},
        {'name': 'memberunconfirmedproxy', 'permissions': ['members.view_memberunconfirmedproxy']},
        {'name': 'memberwaitinglist', 'permissions': ['members.view_memberwaitinglist']},
    ]},
    {'app_label': 'material', 'items': [
        {'name': 'materialcategory', 'permissions': ['material.view_materialcategory']},
        {'name': 'materialpart'},
    ]},
    {'label': 'Externe Links', 'items' : [
        { 'label': 'Packlisten und Co.', 'url': 'https://cloud.jdav-ludwigsburg.de/index.php/s/qxQCTR8JqYSXXCQ'}
    ]},
]

# Waiting list configuration parameters, all numbers are in days


GRACE_PERIOD_WAITING_CONFIRMATION = 30
WAITING_CONFIRMATION_FREQUENCY = 90
