"""
Base settings for Proxito

Some of these settings will eventually be backported into the main settings file,
but currently we have them to be able to run the site with the old middleware for
a staged rollout of the proxito code.
"""

class CommunityProxitoSettingsMixin:

    ROOT_URLCONF = 'readthedocs.proxito.urls'
    USE_SUBDOMAIN = True

    @property
    def MIDDLEWARE(self):  # noqa
        # Use our new middleware instead of the old one
        classes = super().MIDDLEWARE
        classes = list(classes)
        index = classes.index(
            'readthedocs.core.middleware.SubdomainMiddleware'
        )
        classes[index] = 'readthedocs.proxito.middleware.ProxitoMiddleware'

        middleware_to_remove = (
            'readthedocs.core.middleware.SingleVersionMiddleware',
            'csp.middleware.CSPMiddleware',
        )
        for mw in middleware_to_remove:
            if mw in classes:
                classes.remove(mw)

        return classes

"""
Base settings for Proxito

Some of these settings will eventually be backported into the main settings file,
but currently we have them to be able to run the site with the old middleware for
a staged rollout of the proxito code.
"""


class CommunityProxitoSettingsMixin:

    ROOT_URLCONF = 'readthedocs.proxito.urls'
    USE_SUBDOMAIN = True

    @property
    def DATABASES(self):
        # This keeps connections to the DB alive,
        # which reduces latency with connecting to postgres
        dbs = getattr(super(), 'DATABASES', {})
        for db in dbs.keys():
            dbs[db]['CONN_MAX_AGE'] = 86400
        return dbs

    @property
    def MIDDLEWARE(self):  # noqa
        # Use our new middleware instead of the old one
        classes = super().MIDDLEWARE
        classes = list(classes)
        index = classes.index(
            'readthedocs.core.middleware.SubdomainMiddleware'
        )
        classes[index] = 'readthedocs.proxito.middleware.ProxitoMiddleware'

        middleware_to_remove = (
            'readthedocs.core.middleware.SingleVersionMiddleware',
            'csp.middleware.CSPMiddleware',
        )
        for mw in middleware_to_remove:
            if mw in classes:
                classes.remove(mw)

        return classes

Expand the logic in our proxito mixin.
Expand the logic in our proxito mixin.

This makes proxito mixin match production for .com/.org
in the areas where we are overriding the same things.
