from .base import *
from .base import env

# Sentry
#
# import logging
# import sentry_sdk
# from sentry_sdk.integrations.logging import LoggingIntegration
# from sentry_sdk.integrations.django import DjangoIntegration
# from sentry_sdk.integrations.celery import CeleryIntegration
# from sentry_sdk.integrations.redis import RedisIntegration


# General
#
DEBUG = False
SECRET_KEY = env("DJANGO_SECRET_KEY")
# Final hostname the user will type into browser's address bar
DOMAIN_NAME = env("DJANGO_DOMAIN_NAME")
ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS")
ADMIN_URL = env("DJANGO_ADMIN_URL")

# Databases
#
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "HOST": env("POSTGRES_HOST"),
        "PORT": env("POSTGRES_PORT"),
        "NAME": env("POSTGRES_DB"),
        "USER": env("POSTGRES_USER"),
        "PASSWORD": env("POSTGRES_PASSWORD"),
        "ATOMIC_REQUESTS": True,
        "CONN_MAC_AGE": 60,
    }
}

# Security - HTTPs configuration
# https://docs.djangoproject.com/en/dev/topics/security/#ssl-https
#
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
# TODO: set this to 60 seconds first and then to 518400 once you prove the former works
SECURE_HSTS_SECONDS = 60
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True
SECURE_CONTENT_TYPE_NOSNIFF = True

# Logging
#
# https://docs.djangoproject.com/en/dev/ref/settings/#logging
# See https://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.

LOGGING = {
    "version": 1,
    "disable_existing_loggers": True,
    "formatters": {
        "verbose": {
            "format": "%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s"
        }
    },
    "handlers": {
        "console": {
            "level": "DEBUG",
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        }
    },
    "root": {
        "level": "INFO",
        "handlers": ["console"],
    },
    "loggers": {
        "django.db.backends": {
            "level": "ERROR",
            "handlers": ["console"],
            "propagate": False,
        },
        # Sentry
        #
        # Errors logged by the SDK itself
        # "sentry_sdk": {
        #     "level": "ERROR",
        #     "handlers": ["console"],
        #     "propagate": False,
        # },
        "django.security.DisallowedHost": {
            "level": "ERROR",
            "handlers": ["console"],
            "propagate": False,
        },
    },
}

# Email
#
DEFAULT_FROM_EMAIL = f"{{ cookiecutter.project_name }} <noreply@{DOMAIN_NAME}>"
SERVER_EMAIL = DEFAULT_FROM_EMAIL
EMAIL_SUBJECT_PREFIX = "[{{ cookiecutter.project_name }}]"

# django-cors-headers
#
CORS_ALLOWED_ORIGINS = env.list("DJANGO_CORS_ALLOWED_ORIGINS")

# whitenoise static/media file server
#
MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "whitenoise.middleware.WhiteNoiseMiddleware",  # whitenoise
    "corsheaders.middleware.CorsMiddleware",  # django-cors-headers
    "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.common.BrokenLinkEmailsMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"

# S3 static/media file storage
#
# # https://django-storages.readthedocs.io/en/latest/#installation
# INSTALLED_APPS += ["storages"]  # noqa F405
# AWS_ACCESS_KEY_ID = env("DJANGO_AWS_ACCESS_KEY_ID")
# AWS_SECRET_ACCESS_KEY = env("DJANGO_AWS_SECRET_ACCESS_KEY")
# AWS_STORAGE_BUCKET_NAME = env("DJANGO_AWS_STORAGE_BUCKET_NAME")
# AWS_QUERYSTRING_AUTH = False
# # DO NOT change these unless you know what you're doing.
# _AWS_EXPIRY = 60 * 60 * 24 * 7
# AWS_S3_OBJECT_PARAMETERS = {
#     "CacheControl": f"max-age={_AWS_EXPIRY}, s-maxage={_AWS_EXPIRY}, must-revalidate"
# }
# AWS_S3_REGION_NAME = env("DJANGO_AWS_S3_REGION_NAME", default=None)
# # https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#cloudfront
# AWS_S3_CUSTOM_DOMAIN = env("DJANGO_AWS_S3_CUSTOM_DOMAIN", default=None)
# aws_s3_domain = AWS_S3_CUSTOM_DOMAIN or f"{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com"
#
# # STATIC
# # ------------------------
# STATICFILES_STORAGE = "apps.utils.storages.StaticRootS3Boto3Storage"
# COLLECTFAST_STRATEGY = "collectfast.strategies.boto3.Boto3Strategy"
# STATIC_URL = f"https://{aws_s3_domain}/static/"
# # MEDIA
# # ------------------------------------------------------------------------------
# DEFAULT_FILE_STORAGE = "apps.utils.storages.MediaRootS3Boto3Storage"
# MEDIA_URL = f"https://{aws_s3_domain}/media/"

# Redis caching
#
# CACHES = {
#     "default": {
#         "BACKEND": "django_redis.cache.RedisCache",
#         "LOCATION": env("REDIS_URL"),
#         "OPTIONS": {
#             "CLIENT_CLASS": "django_redis.client.DefaultClient",
#             # Mimicing memcache behavior.
#             # https://github.com/jazzband/django-redis#memcached-exceptions-behavior
#             "IGNORE_EXCEPTIONS": True,
#         },
#     }
# }

# anymain[mailgun]
#
# https://anymail.readthedocs.io/en/stable/installation/#installing-anymail
# INSTALLED_APPS += ["anymail"]  # noqa F405
# https://docs.djangoproject.com/en/dev/ref/settings/#email-backend
# https://anymail.readthedocs.io/en/stable/installation/#anymail-settings-reference
# https://anymail.readthedocs.io/en/stable/esps/mailgun/
# EMAIL_BACKEND = "anymail.backends.mailgun.EmailBackend"
# ANYMAIL = {
#     "MAILGUN_API_KEY": env("MAILGUN_API_KEY"),
#     "MAILGUN_SENDER_DOMAIN": env("MAILGUN_DOMAIN"),
#     "MAILGUN_API_URL": env("MAILGUN_API_URL", default="https://api.mailgun.net/v3"),
# }

# Collectfast
#
# https://github.com/antonagestam/collectfast#installation
# INSTALLED_APPS = ["collectfast"] + INSTALLED_APPS

# Sentry
#
# SENTRY_DSN = env("SENTRY_DSN")
# SENTRY_LOG_LEVEL = env.int("DJANGO_SENTRY_LOG_LEVEL", logging.INFO)

# sentry_logging = LoggingIntegration(
#     level=SENTRY_LOG_LEVEL,  # Capture info and above as breadcrumbs
#     event_level=logging.ERROR,  # Send errors as events
# )
# integrations = [
#     sentry_logging,
#     DjangoIntegration(),
#     CeleryIntegration(),
#     RedisIntegration(),
# ]
# sentry_sdk.init(
#     dsn=SENTRY_DSN,
#     integrations=integrations,
#     environment=env("SENTRY_ENVIRONMENT", default="production"),
#     traces_sample_rate=env.float("SENTRY_TRACES_SAMPLE_RATE", default=0.0),
# )

from .base import *
from .base import env

# Sentry
#
# import logging
# import sentry_sdk
# from sentry_sdk.integrations.logging import LoggingIntegration
# from sentry_sdk.integrations.django import DjangoIntegration
# from sentry_sdk.integrations.celery import CeleryIntegration
# from sentry_sdk.integrations.redis import RedisIntegration


# General
#
DEBUG = False
SECRET_KEY = env("DJANGO_SECRET_KEY")
# Final hostname the user will type into browser's address bar
DOMAIN_NAME = env("DJANGO_DOMAIN_NAME")
ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS")
ADMIN_URL = env("DJANGO_ADMIN_URL")

# Databases
#
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "HOST": env("POSTGRES_HOST"),
        "PORT": env("POSTGRES_PORT"),
        "NAME": env("POSTGRES_DB"),
        "USER": env("POSTGRES_USER"),
        "PASSWORD": env("POSTGRES_PASSWORD"),
        "ATOMIC_REQUESTS": True,
        "CONN_MAC_AGE": 60,
    }
}

# Security - HTTPs configuration
# https://docs.djangoproject.com/en/dev/topics/security/#ssl-https
#
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
# TODO: set this to 60 seconds first and then to 518400 once you prove the former works
SECURE_HSTS_SECONDS = 60
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True
SECURE_CONTENT_TYPE_NOSNIFF = True

# Logging
#
# https://docs.djangoproject.com/en/dev/ref/settings/#logging
# See https://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.

LOGGING = {
    "version": 1,
    "disable_existing_loggers": True,
    "formatters": {
        "verbose": {
            "format": "%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s"
        }
    },
    "handlers": {
        "console": {
            "level": "DEBUG",
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        }
    },
    "root": {
        "level": "INFO",
        "handlers": ["console"],
    },
    "loggers": {
        "django.db.backends": {
            "level": "ERROR",
            "handlers": ["console"],
            "propagate": False,
        },
        # Sentry
        #
        # Errors logged by the SDK itself
        # "sentry_sdk": {
        #     "level": "ERROR",
        #     "handlers": ["console"],
        #     "propagate": False,
        # },
        "django.security.DisallowedHost": {
            "level": "ERROR",
            "handlers": ["console"],
            "propagate": False,
        },
    },
}

# Email
#
DEFAULT_FROM_EMAIL = f"{{ cookiecutter.project_name }} <noreply@{DOMAIN_NAME}>"
SERVER_EMAIL = DEFAULT_FROM_EMAIL
EMAIL_SUBJECT_PREFIX = "[{{ cookiecutter.project_name }}]"

# django-cors-headers
#
CORS_ALLOWED_ORIGINS = env.list("DJANGO_CORS_ALLOWED_ORIGINS")

# whitenoise static/media file server
#
MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "whitenoise.middleware.WhiteNoiseMiddleware",  # whitenoise
    "corsheaders.middleware.CorsMiddleware",  # django-cors-headers
    "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.common.BrokenLinkEmailsMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"

# S3 static/media file storage
#
# # https://django-storages.readthedocs.io/en/latest/#installation
# INSTALLED_APPS += ["storages"]  # noqa F405
# AWS_ACCESS_KEY_ID = env("DJANGO_AWS_ACCESS_KEY_ID")
# AWS_SECRET_ACCESS_KEY = env("DJANGO_AWS_SECRET_ACCESS_KEY")
# AWS_STORAGE_BUCKET_NAME = env("DJANGO_AWS_STORAGE_BUCKET_NAME")
# AWS_QUERYSTRING_AUTH = False
# # DO NOT change these unless you know what you're doing.
# _AWS_EXPIRY = 60 * 60 * 24 * 7
# AWS_S3_OBJECT_PARAMETERS = {
#     "CacheControl": f"max-age={_AWS_EXPIRY}, s-maxage={_AWS_EXPIRY}, must-revalidate"
# }
# AWS_S3_REGION_NAME = env("DJANGO_AWS_S3_REGION_NAME", default=None)
# # https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#cloudfront
# AWS_S3_CUSTOM_DOMAIN = env("DJANGO_AWS_S3_CUSTOM_DOMAIN", default=None)
# aws_s3_domain = AWS_S3_CUSTOM_DOMAIN or f"{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com"
#
# # STATIC
# # ------------------------
# STATICFILES_STORAGE = "apps.utils.storages.StaticRootS3Boto3Storage"
# COLLECTFAST_STRATEGY = "collectfast.strategies.boto3.Boto3Strategy"
# STATIC_URL = f"https://{aws_s3_domain}/static/"
# # MEDIA
# # ------------------------------------------------------------------------------
# DEFAULT_FILE_STORAGE = "apps.utils.storages.MediaRootS3Boto3Storage"
# MEDIA_URL = f"https://{aws_s3_domain}/media/"

# Redis caching
#
# CACHES = {
#     "default": {
#         "BACKEND": "django_redis.cache.RedisCache",
#         "LOCATION": env("REDIS_URL"),
#         "OPTIONS": {
#             "CLIENT_CLASS": "django_redis.client.DefaultClient",
#             # Mimicing memcache behavior.
#             # https://github.com/jazzband/django-redis#memcached-exceptions-behavior
#             "IGNORE_EXCEPTIONS": True,
#         },
#     }
# }

# anymain[mailgun]
#
# https://anymail.readthedocs.io/en/stable/installation/#installing-anymail
# INSTALLED_APPS += ["anymail"]  # noqa F405
# https://docs.djangoproject.com/en/dev/ref/settings/#email-backend
# https://anymail.readthedocs.io/en/stable/installation/#anymail-settings-reference
# https://anymail.readthedocs.io/en/stable/esps/mailgun/
# EMAIL_BACKEND = "anymail.backends.mailgun.EmailBackend"
# ANYMAIL = {
#     "MAILGUN_API_KEY": env("MAILGUN_API_KEY"),
#     "MAILGUN_SENDER_DOMAIN": env("MAILGUN_DOMAIN"),
#     "MAILGUN_API_URL": env("MAILGUN_API_URL", default="https://api.mailgun.net/v3"),
# }

# Collectfast
#
# https://github.com/antonagestam/collectfast#installation
# INSTALLED_APPS = ["collectfast"] + INSTALLED_APPS

# Sentry
#
# SENTRY_DSN = env("SENTRY_DSN")
# SENTRY_LOG_LEVEL = env.int("DJANGO_SENTRY_LOG_LEVEL", logging.INFO)

# sentry_logging = LoggingIntegration(
#     level=SENTRY_LOG_LEVEL,  # Capture info and above as breadcrumbs
#     event_level=logging.ERROR,  # Send errors as events
# )
# integrations = [
#     sentry_logging,
#     DjangoIntegration(),
#     CeleryIntegration(),
#     RedisIntegration(),
# ]
# sentry_sdk.init(
#     dsn=SENTRY_DSN,
#     integrations=integrations,
#     environment=env("SENTRY_ENVIRONMENT", default="production"),
#     traces_sample_rate=env.float("SENTRY_TRACES_SAMPLE_RATE", default=0.0),
# )

# Sentry # # import logging # import sentry_sdk # from sentry_sdk.integrations.logging import LoggingIntegration # from sentry_sdk.integrations.django import DjangoIntegration # from sentry_sdk.integrations.celery import CeleryIntegration # from sentry_sdk.integrations.redis import RedisIntegration # General # # Final hostname the user will type into browser's address bar # Databases # # Security - HTTPs configuration # https://docs.djangoproject.com/en/dev/topics/security/#ssl-https # # TODO: set this to 60 seconds first and then to 518400 once you prove the former works # Logging # # https://docs.djangoproject.com/en/dev/ref/settings/#logging # See https://docs.djangoproject.com/en/dev/topics/logging for # more details on how to customize your logging configuration. # Sentry # # Errors logged by the SDK itself # "sentry_sdk": { #     "level": "ERROR", #     "handlers": ["console"], #     "propagate": False, # }, # Email # # django-cors-headers # # whitenoise static/media file server # # whitenoise # django-cors-headers # S3 static/media file storage # # # https://django-storages.readthedocs.io/en/latest/#installation # INSTALLED_APPS += ["storages"]  # noqa F405 # AWS_ACCESS_KEY_ID = env("DJANGO_AWS_ACCESS_KEY_ID") # AWS_SECRET_ACCESS_KEY = env("DJANGO_AWS_SECRET_ACCESS_KEY") # AWS_STORAGE_BUCKET_NAME = env("DJANGO_AWS_STORAGE_BUCKET_NAME") # AWS_QUERYSTRING_AUTH = False # # DO NOT change these unless you know what you're doing. # _AWS_EXPIRY = 60 * 60 * 24 * 7 # AWS_S3_OBJECT_PARAMETERS = { #     "CacheControl": f"max-age={_AWS_EXPIRY}, s-maxage={_AWS_EXPIRY}, must-revalidate" # } # AWS_S3_REGION_NAME = env("DJANGO_AWS_S3_REGION_NAME", default=None) # # https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#cloudfront # AWS_S3_CUSTOM_DOMAIN = env("DJANGO_AWS_S3_CUSTOM_DOMAIN", default=None) # aws_s3_domain = AWS_S3_CUSTOM_DOMAIN or f"{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com" # # # STATIC # # ------------------------ # STATICFILES_STORAGE = "apps.utils.storages.StaticRootS3Boto3Storage" # COLLECTFAST_STRATEGY = "collectfast.strategies.boto3.Boto3Strategy" # STATIC_URL = f"https://{aws_s3_domain}/static/" # # MEDIA # # ------------------------------------------------------------------------------ # DEFAULT_FILE_STORAGE = "apps.utils.storages.MediaRootS3Boto3Storage" # MEDIA_URL = f"https://{aws_s3_domain}/media/" # Redis caching # # CACHES = { #     "default": { #         "BACKEND": "django_redis.cache.RedisCache", #         "LOCATION": env("REDIS_URL"), #         "OPTIONS": { #             "CLIENT_CLASS": "django_redis.client.DefaultClient", #             # Mimicing memcache behavior. #             # https://github.com/jazzband/django-redis#memcached-exceptions-behavior #             "IGNORE_EXCEPTIONS": True, #         }, #     } # } # anymain[mailgun] # # https://anymail.readthedocs.io/en/stable/installation/#installing-anymail # INSTALLED_APPS += ["anymail"]  # noqa F405 # https://docs.djangoproject.com/en/dev/ref/settings/#email-backend # https://anymail.readthedocs.io/en/stable/installation/#anymail-settings-reference # https://anymail.readthedocs.io/en/stable/esps/mailgun/ # EMAIL_BACKEND = "anymail.backends.mailgun.EmailBackend" # ANYMAIL = { #     "MAILGUN_API_KEY": env("MAILGUN_API_KEY"), #     "MAILGUN_SENDER_DOMAIN": env("MAILGUN_DOMAIN"), #     "MAILGUN_API_URL": env("MAILGUN_API_URL", default="https://api.mailgun.net/v3"), # } # Collectfast # # https://github.com/antonagestam/collectfast#installation # INSTALLED_APPS = ["collectfast"] + INSTALLED_APPS # Sentry # # SENTRY_DSN = env("SENTRY_DSN") # SENTRY_LOG_LEVEL = env.int("DJANGO_SENTRY_LOG_LEVEL", logging.INFO) # sentry_logging = LoggingIntegration( #     level=SENTRY_LOG_LEVEL,  # Capture info and above as breadcrumbs #     event_level=logging.ERROR,  # Send errors as events # ) # integrations = [ #     sentry_logging, #     DjangoIntegration(), #     CeleryIntegration(), #     RedisIntegration(), # ] # sentry_sdk.init( #     dsn=SENTRY_DSN, #     integrations=integrations, #     environment=env("SENTRY_ENVIRONMENT", default="production"), #     traces_sample_rate=env.float("SENTRY_TRACES_SAMPLE_RATE", default=0.0), # )