"""
Django settings for francy project.

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

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

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

import os

from dotenv import load_dotenv
from pathlib import Path

# Load environment variables
env_path = Path('.') / '.env'
load_dotenv(dotenv_path=env_path)

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.getenv('SECRET_KEY')

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

ALLOWED_HOSTS = []

# Whether users are allowed to create user accounts through the API.
ALLOW_REGISTER = True

# Superuser attributes
ADMIN_USER = os.getenv('ADMIN_USER')
ADMIN_MAIL = os.getenv('ADMIN_MAIL')
ADMIN_PASSWORD = os.getenv('ADMIN_PASSWORD')

# Application definition
MS_CLIENT_ID = os.getenv('MS_CLIENT_ID')
MS_CLIENT_SECRET = os.getenv('MS_CLIENT_SECRET')
MS_REDIRECT_URI = os.getenv('MS_REDIRECT_URI')

# Wether to keep the superuser object persistent on server restart
ADMIN_PERSISTENT = True

VERSION = '0.1.0'


# Application definition

INSTALLED_APPS = [
    # Custom apps
    'api',
    'user',
    'design',
    'mailing',
    'file',

    # GraphQL
    'graphene_django',
    'graphql_jwt.refresh_token.apps.RefreshTokenConfig',  # Refresh tokens

    # Filtering queries
    'django_filters',

    # Mail
    'mail_templated',

    # Default Django apps
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

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

GRAPHENE = {
    'SCHEMA': 'api.schema.schema',
    'MIDDLEWARE': [
        'graphql_jwt.middleware.JSONWebTokenMiddleware',
    ],
}

AUTHENTICATION_BACKENDS = [
    'user.auth_backends.jwt_auth.JSONWebTokenBackend',
    # 'graphql_jwt.backends.JSONWebTokenBackend',  # Default GraphQL JWT
    'user.auth_backends.authentication.AuthenticationBackend',
    # 'django.contrib.auth.backends.ModelBackend',  # Default
]

GRAPHQL_JWT = {
    # Per default, JWT tokens do not expire.
    # See graphql_jwt.settings for more options.
    # 'JWT_VERIFY_EXPIRATION': True,

    # For my understanding, this means that the refresh token expiration date
    # is refreshed when the user shows activity using this refresh token.
    # Through this, the same refresh token can be used indefinitely.
    # The lifecycle of a refresh token: https://django-graphql-jwt.domake.io/en/latest/refresh_token.html
    'JWT_LONG_RUNNING_REFRESH_TOKEN': True,
}

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


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Authentication User model

AUTH_USER_MODEL = 'user.User'


# Password validation
# https://docs.djangoproject.com/en/3.1/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/3.1/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/3.1/howto/static-files/

STATIC_URL = '/static/'

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

# This is were the file module stores user-uploaded content
USERDATA_URL = '/userdata/'
USERDATA_ROOT = os.path.join(BASE_DIR, 'userdata')

EMAIL_DEMO_BACKEND = (os.environ.get('EMAIL_DEMO_BACKEND', 'False') == 'True')

# E-mail configuration
if EMAIL_DEMO_BACKEND:
    EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

EMAIL_HOST = os.getenv('EMAIL_HOST')
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD')
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

# Twilio configuration
# Get TWILIO_SID and TWILIO_AUTH_TOKEN from environment variables
TWILIO_SID = os.environ.get('TWILIO_SID')
TWILIO_AUTH_TOKEN = os.environ.get('TWILIO_AUTH_TOKEN')
TWILIO_SERVICE_ID = os.environ.get('TWILIO_SERVICE_ID')

ALLOWED_PHONE_REGIONS = [
    'AT',
    'DE',
    'CH'
]

"""
Django settings for francy project.

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

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

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

import os

from dotenv import load_dotenv
from pathlib import Path

# Load environment variables
env_path = Path('.') / '.env'
load_dotenv(dotenv_path=env_path)

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.getenv('SECRET_KEY')

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

ALLOWED_HOSTS = []

# Whether users are allowed to create user accounts through the API.
ALLOW_REGISTER = True

# Superuser attributes
ADMIN_USER = os.getenv('ADMIN_USER')
ADMIN_MAIL = os.getenv('ADMIN_MAIL')
ADMIN_PASSWORD = os.getenv('ADMIN_PASSWORD')

# Application definition
MS_CLIENT_ID = os.getenv('MS_CLIENT_ID')
MS_CLIENT_SECRET = os.getenv('MS_CLIENT_SECRET')
MS_REDIRECT_URI = os.getenv('MS_REDIRECT_URI')

# Wether to keep the superuser object persistent on server restart
ADMIN_PERSISTENT = True

VERSION = '0.1.0'


# Application definition

INSTALLED_APPS = [
    # Custom apps
    'api',
    'user',
    'design',
    'mailing',
    'file',

    # GraphQL
    'graphene_django',
    'graphql_jwt.refresh_token.apps.RefreshTokenConfig',  # Refresh tokens

    # Filtering queries
    'django_filters',

    # Mail
    'mail_templated',

    # Default Django apps
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

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

GRAPHENE = {
    'SCHEMA': 'api.schema.schema',
    'MIDDLEWARE': [
        'graphql_jwt.middleware.JSONWebTokenMiddleware',
    ],
}

AUTHENTICATION_BACKENDS = [
    'user.auth_backends.jwt_auth.JSONWebTokenBackend',
    # 'graphql_jwt.backends.JSONWebTokenBackend',  # Default GraphQL JWT
    'user.auth_backends.authentication.AuthenticationBackend',
    # 'django.contrib.auth.backends.ModelBackend',  # Default
]

GRAPHQL_JWT = {
    # Per default, JWT tokens do not expire.
    # See graphql_jwt.settings for more options.
    # 'JWT_VERIFY_EXPIRATION': True,

    # For my understanding, this means that the refresh token expiration date
    # is refreshed when the user shows activity using this refresh token.
    # Through this, the same refresh token can be used indefinitely.
    # The lifecycle of a refresh token: https://django-graphql-jwt.domake.io/en/latest/refresh_token.html
    'JWT_LONG_RUNNING_REFRESH_TOKEN': True,
}

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


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Authentication User model

AUTH_USER_MODEL = 'user.User'


# Password validation
# https://docs.djangoproject.com/en/3.1/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/3.1/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/3.1/howto/static-files/

STATIC_URL = '/static/'

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

# This is were the file module stores user-uploaded content
USERDATA_URL = '/userdata/'
USERDATA_ROOT = os.path.join(BASE_DIR, 'userdata')

EMAIL_DEMO_BACKEND = (os.environ.get('EMAIL_DEMO_BACKEND', 'False') == 'True')

# E-mail configuration
if EMAIL_DEMO_BACKEND:
    EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

EMAIL_HOST = os.getenv('EMAIL_HOST')
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD')
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

# Twilio configuration
# Get TWILIO_SID and TWILIO_AUTH_TOKEN from environment variables
TWILIO_SID = os.environ.get('TWILIO_SID')
TWILIO_AUTH_TOKEN = os.environ.get('TWILIO_AUTH_TOKEN')
TWILIO_SERVICE_ID = os.environ.get('TWILIO_SERVICE_ID')

ALLOWED_PHONE_REGIONS = [
    'AT',
    'DE',
    'CH'
]

Django settings for francy project.  Generated by 'django-admin startproject' using Django 3.1.2.  For more information on this file, see https://docs.djangoproject.com/en/3.1/topics/settings/  For the full list of settings and their values, see https://docs.djangoproject.com/en/3.1/ref/settings/ # Load environment variables # Build paths inside the project like this: BASE_DIR / 'subdir'. # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! # SECURITY WARNING: don't run with debug turned on in production! # Whether users are allowed to create user accounts through the API. # Superuser attributes # Application definition # Wether to keep the superuser object persistent on server restart # Application definition # Custom apps # GraphQL # Refresh tokens # Filtering queries # Mail # Default Django apps # 'graphql_jwt.backends.JSONWebTokenBackend',  # Default GraphQL JWT # 'django.contrib.auth.backends.ModelBackend',  # Default # Per default, JWT tokens do not expire. # See graphql_jwt.settings for more options. # 'JWT_VERIFY_EXPIRATION': True, # For my understanding, this means that the refresh token expiration date # is refreshed when the user shows activity using this refresh token. # Through this, the same refresh token can be used indefinitely. # The lifecycle of a refresh token: https://django-graphql-jwt.domake.io/en/latest/refresh_token.html # Database # https://docs.djangoproject.com/en/3.1/ref/settings/#databases # Authentication User model # Password validation # https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators # Internationalization # https://docs.djangoproject.com/en/3.1/topics/i18n/ # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.1/howto/static-files/ # This is were the file module stores user-uploaded content # E-mail configuration # Twilio configuration # Get TWILIO_SID and TWILIO_AUTH_TOKEN from environment variables