# Copyright (C) 2017-2021  The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU Affero General Public License version 3, or any later version
# See top-level LICENSE file for more information

"""
Django production settings for swh-web.
"""

from .common import *  # noqa
from .common import CACHES, MIDDLEWARE, REST_FRAMEWORK, WEBPACK_LOADER, swh_web_config

MIDDLEWARE += [
    "swh.web.common.middlewares.HtmlMinifyMiddleware",
]

if swh_web_config.get("throttling", {}).get("cache_uri"):
    CACHES.update(
        {
            "default": {
                "BACKEND": "django.core.cache.backends.memcached.MemcachedCache",
                "LOCATION": swh_web_config["throttling"]["cache_uri"],
            }
        }
    )

# Setup support for proxy headers
USE_X_FORWARDED_HOST = True
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")

# We're going through seven (or, in that case, 2) proxies thanks to Varnish
REST_FRAMEWORK["NUM_PROXIES"] = 2

db_conf = swh_web_config["production_db"]
if db_conf.get("name", "").startswith("postgresql://"):
    # poor man's support for dsn connection string...
    import psycopg2

    with psycopg2.connect(db_conf.get("name")) as cnx:
        dsn_dict = cnx.get_dsn_parameters()

    db_conf["name"] = dsn_dict.get("dbname")
    db_conf["host"] = dsn_dict.get("host")
    db_conf["port"] = dsn_dict.get("port")
    db_conf["user"] = dsn_dict.get("user")
    db_conf["password"] = dsn_dict.get("password")


# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": db_conf.get("name"),
        "HOST": db_conf.get("host"),
        "PORT": db_conf.get("port"),
        "USER": db_conf.get("user"),
        "PASSWORD": db_conf.get("password"),
    }
}

WEBPACK_LOADER["DEFAULT"]["CACHE"] = True

LOGIN_URL = "/oidc/login/"
LOGIN_REDIRECT_URL = "/oidc/profile/"

@@ -17,17 +17,17 @@
 2017-202
-1
+2
   The So
@@ -325,45 +325,36 @@
 ort 
-*  # noqa%0Afrom .common import CACHES,
+(%0A    CACHES,%0A    DEBUG,%0A   
  MID
@@ -361,16 +361,20 @@
 DLEWARE,
+%0A   
  REST_FR
@@ -381,16 +381,20 @@
 AMEWORK,
+%0A   
  WEBPACK
@@ -401,16 +401,20 @@
 _LOADER,
+%0A   
  swh_web
@@ -420,16 +420,49 @@
 b_config
+,%0A)%0Afrom .common import *  # noqa
 %0A%0AMIDDLE
@@ -1947,20 +1947,25 @@
 CHE%22%5D = 
-True
+not DEBUG
 %0A%0ALOGIN_

Use webpack loader cache when not in debug mode