Exemple #1
0
def preserve_request_params(request, url):
    url = yurl.URL(url)
    new_query = urllib.parse.parse_qs(url.query)
    query = urllib.parse.parse_qs(yurl.URL(request.get_full_path()).query)
    query.update(new_query)
    query = urllib.parse.urlencode(query, doseq=True)
    return str(url.replace(query=query))
    def storage_settings_for_media(self, settings, env):
        import yurl
        from aldryn_django.storage import parse_storage_url
        if env('DEFAULT_STORAGE_DSN'):
            settings['DEFAULT_STORAGE_DSN'] = env('DEFAULT_STORAGE_DSN')
        settings['MEDIA_URL'] = env('MEDIA_URL', '/media/')
        if 'DEFAULT_STORAGE_DSN' in settings:
            settings.update(parse_storage_url(settings['DEFAULT_STORAGE_DSN']))
        media_host = yurl.URL(settings['MEDIA_URL']).host
        settings['MEDIA_URL_IS_ON_OTHER_DOMAIN'] = (
            media_host and media_host not in settings['ALLOWED_HOSTS'])
        settings['MEDIA_ROOT'] = env(
            'MEDIA_ROOT', os.path.join(settings['DATA_ROOT'], 'media'))
        settings['MEDIA_HEADERS'] = []

        cmds = {}
        if os.path.exists('/usr/bin/pngout'):
            cmds[
                'png'] = '/usr/bin/pngout {filename} {filename}.png -s0 -y -force && mv {filename}.png {filename}'
        if os.path.exists('/usr/bin/jpegoptim'):
            cmds[
                'jpeg'] = '/usr/bin/jpegoptim --max=90 --overwrite --strip-all --all-progressive {filename}'
        if os.path.exists('/usr/bin/gifsicle'):
            cmds['gif'] = '/usr/bin/gifsicle --batch --optimize=2 {filename}'
        settings['THUMBNAIL_OPTIMIZE_COMMAND'] = cmds
    def storage_settings_for_media(self, settings, env):
        import yurl
        from aldryn_django.storage import parse_storage_url

        if env("DEFAULT_STORAGE_DSN"):
            settings["DEFAULT_STORAGE_DSN"] = env("DEFAULT_STORAGE_DSN")
        settings["MEDIA_URL"] = env("MEDIA_URL", "/media/")
        if "DEFAULT_STORAGE_DSN" in settings:
            settings.update(parse_storage_url(settings["DEFAULT_STORAGE_DSN"]))
        media_host = yurl.URL(settings["MEDIA_URL"]).host
        settings["MEDIA_URL_IS_ON_OTHER_DOMAIN"] = (
            media_host and media_host not in settings["ALLOWED_HOSTS"]
        )
        settings["MEDIA_ROOT"] = env(
            "MEDIA_ROOT", os.path.join(settings["DATA_ROOT"], "media")
        )
        settings["MEDIA_HEADERS"] = []

        cmds = {}
        if os.path.exists("/usr/bin/pngout"):
            cmds[
                "png"
            ] = "/usr/bin/pngout {filename} {filename}.png -s0 -y -force && mv {filename}.png {filename}"
        if os.path.exists("/usr/bin/jpegoptim"):
            cmds[
                "jpeg"
            ] = "/usr/bin/jpegoptim --max=90 --overwrite --strip-all --all-progressive {filename}"
        if os.path.exists("/usr/bin/gifsicle"):
            cmds["gif"] = "/usr/bin/gifsicle --batch --optimize=2 {filename}"
        settings["THUMBNAIL_OPTIMIZE_COMMAND"] = cmds
Exemple #4
0
    def configure_elastic_apm(self, settings, env):
        from aldryn_addons.utils import boolean_ish
        import yurl
        elastic_dsn = yurl.URL(env('DEFAULT_APM_DSN'))

        if elastic_dsn:
            service_name = elastic_dsn.username
            secret_token = elastic_dsn.authorization
            server_url = str(elastic_dsn.replace(userinfo=''))

            server_env = env('STAGE', 'local').lower()
            if server_env in {
                    'live',
            }:
                elastic_send_debug = False
            else:
                elastic_send_debug = True
            elastic_send_debug = boolean_ish(
                env('ELASTIC_SEND_DEBUG', elastic_send_debug))

            elastic_service_name = '{}-{}'.format(service_name, server_env)
            elastic_service_name = env('ELASTIC_SERVICE_NAME',
                                       elastic_service_name)

            settings['ELASTIC_APM'] = {
                'DEBUG': elastic_send_debug,
                'SERVER_URL': server_url,
                'SERVICE_NAME': elastic_service_name,
                'SECRET_TOKEN': secret_token,
            }
    def storage_settings_for_static(self, data, settings, env):
        import yurl
        use_gzip = not env('DISABLE_GZIP')
        use_manifest = data['use_manifeststaticfilesstorage']
        if use_gzip:
            if use_manifest:
                storage = 'aldryn_django.storage.ManifestGZippedStaticFilesStorage'
            else:
                storage = 'aldryn_django.storage.GZippedStaticFilesStorage'
        else:
            if use_manifest:
                storage = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
            else:
                storage = 'django.contrib.staticfiles.storage.StaticFilesStorage'
        settings['STATICFILES_STORAGE'] = storage

        settings['STATIC_URL'] = env('STATIC_URL', '/static/')
        static_host = yurl.URL(settings['STATIC_URL']).host
        settings['STATIC_URL_IS_ON_OTHER_DOMAIN'] = (
            static_host and static_host not in settings['ALLOWED_HOSTS']
        )
        settings['STATIC_ROOT'] = env(
            'STATIC_ROOT',
            os.path.join(settings['BASE_DIR'], 'static_collected'),
        )
        settings['STATIC_HEADERS'] = [
            # Set far-future expiration headers for static files with hashed
            # filenames. Also set cors headers to * for fonts.
            (r'.*\.[0-9a-f]{10,16}\.(eot|ttf|otf|woff)', {
                'Access-Control-Allow-Origin': '*',
                'Cache-Control': 'public, max-age={}'.format(3600 * 24 * 365),
            }),
            (r'.*\.[0-9a-f]{10,16}\.[a-z]+', {
                'Cache-Control': 'public, max-age={}'.format(3600 * 24 * 365),
            }),
            # Set default expiration headers for all remaining static files.
            # *Has to be last* as processing stops at the first matching
            # pattern it finds. Also set cors headers to * for fonts.
            (r'.*\.(eot|ttf|otf|woff)', {
                'Access-Control-Allow-Origin': '*',
                'Cache-Control': 'public, max-age={}'.format(
                    settings['STATICFILES_DEFAULT_MAX_AGE'],
                ),
            }),
            ('.*', {
                'Cache-Control': 'public, max-age={}'.format(
                    settings['STATICFILES_DEFAULT_MAX_AGE'],
                ),
            }),
        ]
        settings['STATICFILES_DIRS'] = env(
            'STATICFILES_DIRS',
            [os.path.join(settings['BASE_DIR'], 'static')]
        )
 def __init__(self, url):
   # logging.debug(url)
   import yurl
   # noinspection PyArgumentList
   parse_result = yurl.URL(url=url)
   import re
   url_without_credentials = re.sub(parse_result.username + ":" + parse_result.authorization, "", url)
   from cloudant.client import CouchDB
   self.client = CouchDB(user=parse_result.username, auth_token=parse_result.authorization,
                         url=url_without_credentials, connect=True, auto_renew=True)
   # logging.debug(self.client)
   assert self.client is not None, logging.error(self.client)
 def storage_settings_for_media(self, settings, env):
     import yurl
     from aldryn_django.storage import parse_storage_url
     if env('DEFAULT_STORAGE_DSN'):
         settings['DEFAULT_STORAGE_DSN'] = env('DEFAULT_STORAGE_DSN')
     settings['MEDIA_URL'] = env('MEDIA_URL', '/media/')
     if 'DEFAULT_STORAGE_DSN' in settings:
         settings.update(parse_storage_url(settings['DEFAULT_STORAGE_DSN']))
     settings['MEDIA_URL_IS_ON_OTHER_DOMAIN'] = bool(
         yurl.URL(settings['MEDIA_URL']).host)
     settings['MEDIA_ROOT'] = env(
         'MEDIA_ROOT', os.path.join(settings['DATA_ROOT'], 'media'))
 def storage_settings_for_static(self, settings, env):
     import yurl
     settings['STATIC_URL'] = env('STATIC_URL', '/static/')
     settings['STATIC_URL_IS_ON_OTHER_DOMAIN'] = bool(
         yurl.URL(settings['STATIC_URL']).host)
     settings['STATIC_ROOT'] = env(
         'STATIC_ROOT',
         os.path.join(settings['BASE_DIR'], 'static_collected'),
     )
     settings['STATICFILES_DIRS'] = env('STATICFILES_DIRS', [
         os.path.join(settings['BASE_DIR'], 'static'),
     ])
Exemple #9
0
def get_uwsgi_vassal_opts(settings, port):
    vassal_opts = get_uwsgi_regular_opts(settings, port) + [
        'chdir = /app',
        'vacuum = true',  # vacuum makes uwsgi delete sockets on exit
    ]

    if boolean_ish(os.environ.get('ENABLE_UWSGI_CHEAPER', 'on')):
        vassal_opts.extend([
            'cheaper = 1',
            'cheaper-algo = busyness',
            'cheaper-initial = 1',
            'cheaper-business-verbose = 1',
            'cheaper-business-backlog-alert = 10',
            'cheaper-overload = 20',
        ])

    if not settings['ENABLE_SYNCING']:
        serve_static = False

        if yurl.URL(settings['STATIC_URL']
                    ).host or not settings['STATIC_URL_IS_ON_OTHER_DOMAIN']:
            # TODO: we're currently starting the static url hosting on all vassals if STATIC_HOST is set.
            # We rely on the fallback for the unknown static domain to end up calling on the main vassal.
            # We should rather do this only on the main vassal or create a custom vassal just for static files.
            serve_static = True
            vassal_opts.extend(
                get_uwsgi_static_serving_opts(settings['STATIC_URL'],
                                              settings['STATIC_ROOT'],
                                              settings['STATIC_HEADERS']))

        if not settings['MEDIA_URL_IS_ON_OTHER_DOMAIN']:
            serve_static = True
            vassal_opts.extend(
                get_uwsgi_static_serving_opts(settings['MEDIA_URL'],
                                              settings['MEDIA_ROOT'],
                                              settings['MEDIA_HEADERS']))

        if serve_static:
            vassal_opts.extend([
                'offload-threads = 2',  # Start 2 offloading threads for each worker
                'static-cache-paths = 86400',
                'static-cache-paths-name = staticpaths',
                'cache2 = name=staticpaths,items=5000,blocksize=1k,purge_lru,ignore_full',
                'static-gzip-all = true',  # Serve .gz files if that version is available
            ])

    return [
        'vassal-set = {}'.format(opt.replace(' = ', '=', 1))
        for opt in vassal_opts
    ]
Exemple #10
0
 def request_from_url(self, url):
     url = yurl.URL(url)
     secure = (url.scheme == 'https')
     # Django < 1.7 does not have the `secure` parameter for the request
     # factory methods. Manually set the required kwargs here instead.
     kwargs = {
         'SERVER_NAME': url.host,
         'SERVER_PORT': str('443') if secure else str('80'),
         'wsgi.url_scheme': str('https') if secure else str('http'),
     }
     return self.factory.get(
         url.full_path,
         **kwargs
     )
Exemple #11
0
    def render_tag(self, context, url, params, remove):
        if url is None:
            url = context['request'].get_full_path()

        url = yurl.URL(url)

        query = urllib.parse.parse_qs(url.query)
        for k in remove:
            query.pop(k, None)
        for k, v in params.items():
            query[k] = [v]
        query = urllib.parse.urlencode(query, doseq=True)

        return str(url.replace(query=query))
Exemple #12
0
def get_uwsgi_static_serving_opts(base_url, root, header_patterns):
    base_path = yurl.URL(base_url).path.lstrip('/')
    opts = [
        'static-map = /{}={}'.format(base_path, root),
        'route = {} addheader:Vary: Accept-Encoding'.format(
            os.path.join('^', base_path, '.*')),
    ]

    for pattern, headers in header_patterns:
        pattern = os.path.join('^', base_path, pattern)
        for k, v in headers.items():
            opts.append('route = {} addheader:{}: {}'.format(pattern, k, v))
        opts.append('route = {} last:'.format(pattern, k, v))

    return opts
Exemple #13
0
def get_redirect_url(current_url, config, https=None):
    """
    priorities are (primary domain and aliases are treated the same):
        exact redirect match > exact alias match > pattern redirect match > pattern alias match
    :param current_url: the url that is being called
    :param config: redirect configuration for this url
    :param want_https: whether redirects should go to https (None keeps the current scheme)
    :return: None for no redirect or an url to redirect to
    """
    primary_domain = config['domain']
    domains = set(config.get('aliases', [])) | set((primary_domain,))
    domain_patterns = compile_regexes(domains)
    redirect_domains = set(config.get('redirects', []))
    redirect_domain_patterns = compile_regexes(redirect_domains)
    url = yurl.URL(current_url)
    if https is None:
        target_scheme = url.scheme
    else:
        target_scheme = 'https' if https else 'http'
    redirect_url = None
    if url.is_host_ip() or url.is_host_ipv4():
        # don't redirect for ips
        return
    if url.host in domains and url.scheme == target_scheme:
        # exact host and scheme match: Nothing to do
        return
    if url.host in domains and url.scheme != target_scheme:
        # exact alias match, but scheme mismatch: redirect to changed scheme
        redirect_url = url.replace(scheme=target_scheme)
    elif url.host in redirect_domains:
        # exact redirect match: redirect
        redirect_url = url.replace(scheme=target_scheme, host=primary_domain)
    elif url.host in domains:
        # exact alias match: nothing to do
        return
    elif match_any(redirect_domain_patterns, url.host):
        # pattern redirect match: redirect
        redirect_url = url.replace(scheme=target_scheme, host=primary_domain)
    elif match_any(domain_patterns, url.host):
        # pattern alias match
        if url.scheme != target_scheme:
            # pattern alias match and scheme mismatch: redirect
            redirect_url = url.replace(scheme=target_scheme)
        else:
            return

    if redirect_url:
        return '{}'.format(redirect_url)
Exemple #14
0
def before_all(context):
    context.config = get_config()
    context.base_url = yurl.URL(context.config['s3-url'])

    def request(self, path):
        return requests.get(
            self.base_url.replace(path=context.prefix + '/' +
                                  path).as_string())

    context.request = types.MethodType(request, context)

    context.connection = S3Connection(context.config['access-key'],
                                      context.config['secret-access-key'])
    context.bucket = context.connection.get_bucket(context.config['bucket'])

    # tests must be able to run concurrently; scope to a random prefix
    context.prefix = rand_token()
Exemple #15
0
    def to_settings(self, data, settings):
        from functools import partial
        from aldryn_addons.utils import djsenv
        import yurl

        env = partial(djsenv, settings=settings)

        custom_media_domain = env("CUSTOM_MEDIA_DOMAIN")

        if custom_media_domain and "AWS_MEDIA_BUCKET_PREFIX" in settings:
            settings.update({
                "AWS_MEDIA_DOMAIN":
                custom_media_domain,
                "MEDIA_URL":
                yurl.URL(
                    scheme="https",
                    host=custom_media_domain,
                    path=settings["AWS_MEDIA_BUCKET_PREFIX"],
                ).as_string().rstrip("/") + "/",
            })

        return settings
    def to_settings(self, data, settings):
        import dj_database_url
        import warnings
        import yurl
        from functools import partial
        from aldryn_addons.utils import boolean_ish, djsenv
        env = partial(djsenv, settings=settings)

        # BASE_DIR should already be set by aldryn-addons
        settings['BASE_DIR'] = env('BASE_DIR', required=True)
        settings['DATA_ROOT'] = env('DATA_ROOT',
                                    os.path.join(settings['BASE_DIR'], 'data'))
        settings['SECRET_KEY'] = env('SECRET_KEY', 'this-is-not-very-random')
        settings['DEBUG'] = boolean_ish(env('DEBUG', False))
        settings['TEMPLATE_DEBUG'] = boolean_ish(
            env('TEMPLATE_DEBUG', settings['DEBUG']))

        settings['DATABASE_URL'] = env('DATABASE_URL')
        if settings['DATABASE_URL']:
            pass
        elif env('DJANGO_MODE') == 'build':
            settings['DATABASE_URL'] = 'sqlite://:memory:'
        else:
            settings['DATABASE_URL'] = 'sqlite:///{}'.format(
                os.path.join(settings['DATA_ROOT'], 'db.sqlite3'))
            warnings.warn(
                'no database configured. Falling back to DATABASE_URL={0}'.
                format(settings['DATABASE_URL']),
                RuntimeWarning,
            )
        settings['DATABASES']['default'] = dj_database_url.parse(
            settings['DATABASE_URL'])

        settings['ROOT_URLCONF'] = env('ROOT_URLCONF', 'urls')
        settings['ADDON_URLS'].append('aldryn_django.urls')
        settings['ADDON_URLS_I18N'].append('aldryn_django.i18n_urls')

        settings['WSGI_APPLICATION'] = 'wsgi.application'

        settings['STATIC_URL'] = env('STATIC_URL', '/static/')
        settings['STATIC_URL_IS_ON_OTHER_DOMAIN'] = bool(
            yurl.URL(settings['STATIC_URL']).host)
        settings['STATIC_ROOT'] = env(
            'STATIC_ROOT',
            os.path.join(settings['BASE_DIR'], 'static_collected'),
        )
        settings['STATICFILES_DIRS'] = env('STATICFILES_DIRS', [
            os.path.join(settings['BASE_DIR'], 'static'),
        ])

        settings['MEDIA_URL'] = env('MEDIA_URL', '/media/')
        settings['MEDIA_URL_IS_ON_OTHER_DOMAIN'] = bool(
            yurl.URL(settings['MEDIA_URL']).host)
        settings['MEDIA_ROOT'] = env(
            'MEDIA_ROOT', os.path.join(settings['DATA_ROOT'], 'media'))

        settings['INSTALLED_APPS'].extend([
            'django.contrib.auth',
            'django.contrib.contenttypes',
            'django.contrib.sessions',
            'django.contrib.sites',
            'django.contrib.messages',
            'django.contrib.admin',
            'django.contrib.staticfiles',
            'aldryn_django',
        ])
        if 'sqlite3' not in settings['DATABASES']['default']['ENGINE']:
            settings['INSTALLED_APPS'].append('south')

        settings['TEMPLATE_CONTEXT_PROCESSORS'].extend([
            'django.core.context_processors.request',
            'aldryn_django.context_processors.debug',
        ])
        settings['MIDDLEWARE_CLASSES'].extend([
            'django.middleware.locale.LocaleMiddleware',
            'aldryn_django.middleware.CurrentSiteMiddleware',
        ])

        settings['TEMPLATE_DIRS'] = env(
            'TEMPLATE_DIRS',
            [os.path.join(settings['BASE_DIR'], 'templates')],
        )
        settings['SITE_ID'] = env('SITE_ID', 1)

        self.domain_settings(data, settings, env=env)
        self.server_settings(settings, env=env)
        self.logging_settings(settings, env=env)
        # Order matters, sentry settings rely on logging being configured.
        self.sentry_settings(settings, env=env)
        self.cache_settings(settings, env=env)
        self.storage_settings(settings, env=env)
        self.i18n_settings(data, settings, env=env)
        self.migration_settings(settings, env=env)
        return settings
def parse_storage_url(url):
    config = {}
    url = parse.urlparse(url)

    scheme = url.scheme.split('+', 1)

    config['DEFAULT_FILE_STORAGE'] = SCHEMES[scheme[0]]

    if scheme[0] == 's3':
        query = parse.parse_qs(url.query)
        media_domain = query.get('domain', [None])[0]
        signature_ver = query.get('auth', ['s3v4'])[0]
        endpoint = url.hostname.rsplit('.', 3)
        bucket_name = endpoint[0]
        storage_host = '.'.join(endpoint[1:])

        if signature_ver == 's3v4':
            os.environ['S3_USE_SIGV4'] = 'True'
        elif signature_ver == 's3':
            os.environ['S3_USE_SIGV4'] = ''
        else:
            raise ImproperlyConfigured(
                'Unknown signature version: {}'.format(signature_ver))

        config.update({
            'AWS_MEDIA_ACCESS_KEY_ID':
            parse.unquote(url.username or ''),
            'AWS_MEDIA_SECRET_ACCESS_KEY':
            parse.unquote(url.password or ''),
            'AWS_MEDIA_STORAGE_BUCKET_NAME':
            bucket_name,
            'AWS_MEDIA_STORAGE_HOST':
            storage_host,
            'AWS_MEDIA_BUCKET_PREFIX':
            url.path.lstrip('/'),
            'AWS_MEDIA_DOMAIN':
            media_domain,
        })

        if not media_domain:
            media_domain = '.'.join([
                config['AWS_MEDIA_STORAGE_BUCKET_NAME'],
                config['AWS_MEDIA_STORAGE_HOST'],
            ])
        media_url = yurl.URL(
            scheme='https',
            host=media_domain,
            path=config['AWS_MEDIA_BUCKET_PREFIX'],
        )
        config['MEDIA_URL'] = media_url.as_string()
    elif scheme[0] == 'djfs':
        hostname = ('{}:{}'.format(url.hostname, url.port)
                    if url.port else url.hostname)
        config.update({
            'DJEESE_STORAGE_ID':
            url.username or '',
            'DJEESE_STORAGE_KEY':
            url.password or '',
            'DJEESE_STORAGE_HOST':
            parse.urlunparse((
                scheme[1],
                hostname,
                url.path,
                url.params,
                url.query,
                url.fragment,
            )),
        })
        media_url = yurl.URL(
            scheme=scheme[1],
            host=url.hostname,
            path=url.path,
            port=url.port or '',
        )
        config['MEDIA_URL'] = media_url.as_string()
    if config['MEDIA_URL'] and not config['MEDIA_URL'].endswith('/'):
        # Django (or something else?) silently sets MEDIA_URL to an empty
        # string if it does not end with a '/'
        config['MEDIA_URL'] = '{}/'.format(config['MEDIA_URL'])
    return config
Exemple #18
0
    def to_settings(self, data, settings):
        import dj_database_url
        import warnings
        import yurl
        from functools import partial
        from aldryn_addons.utils import boolean_ish, djsenv
        env = partial(djsenv, settings=settings)

        # BASE_DIR should already be set by aldryn-addons
        settings['BASE_DIR'] = env('BASE_DIR', required=True)
        settings['DATA_ROOT'] = env('DATA_ROOT',
                                    os.path.join(settings['BASE_DIR'], 'data'))
        settings['SECRET_KEY'] = env('SECRET_KEY', 'this-is-not-very-random')
        settings['DEBUG'] = boolean_ish(env('DEBUG', False))

        settings['DATABASE_URL'] = env('DATABASE_URL')
        settings['CACHE_URL'] = env('CACHE_URL')
        if env('DJANGO_MODE') == 'build':
            # In build mode we don't have any connected services like db or
            # cache available. So we need to configure those things in a way
            # they can run without real backends.
            settings['DATABASE_URL'] = 'sqlite://:memory:'
            settings['CACHE_URL'] = 'locmem://'

        if not settings['DATABASE_URL']:
            settings['DATABASE_URL'] = 'sqlite:///{}'.format(
                os.path.join(settings['DATA_ROOT'], 'db.sqlite3'))
            warnings.warn(
                'no database configured. Falling back to DATABASE_URL={0}'.
                format(settings['DATABASE_URL']),
                RuntimeWarning,
            )
        if not settings['CACHE_URL']:
            settings['CACHE_URL'] = 'locmem://'
            warnings.warn(
                'no cache configured. Falling back to CACHE_URL={0}'.format(
                    settings['CACHE_URL']),
                RuntimeWarning,
            )

        settings['DATABASES']['default'] = dj_database_url.parse(
            settings['DATABASE_URL'])

        settings['ROOT_URLCONF'] = env('ROOT_URLCONF', 'urls')
        settings['ADDON_URLS'].append('aldryn_django.urls')
        settings['ADDON_URLS_I18N'].append('aldryn_django.i18n_urls')

        settings['WSGI_APPLICATION'] = 'wsgi.application'

        settings['STATIC_URL'] = env('STATIC_URL', '/static/')
        settings['STATIC_URL_IS_ON_OTHER_DOMAIN'] = bool(
            yurl.URL(settings['STATIC_URL']).host)
        settings['STATIC_ROOT'] = env(
            'STATIC_ROOT',
            os.path.join(settings['BASE_DIR'], 'static_collected'),
        )
        settings['STATICFILES_DIRS'] = env('STATICFILES_DIRS', [
            os.path.join(settings['BASE_DIR'], 'static'),
        ])

        settings['MEDIA_URL'] = env('MEDIA_URL', '/media/')
        settings['MEDIA_URL_IS_ON_OTHER_DOMAIN'] = bool(
            yurl.URL(settings['MEDIA_URL']).host)
        settings['MEDIA_ROOT'] = env(
            'MEDIA_ROOT', os.path.join(settings['DATA_ROOT'], 'media'))

        settings['INSTALLED_APPS'].extend([
            'django.contrib.auth',
            'django.contrib.contenttypes',
            'django.contrib.sessions',
            'django.contrib.sites',
            'django.contrib.messages',
            'django.contrib.admin',
            'django.contrib.staticfiles',
            'aldryn_django',
        ])

        settings['TEMPLATES'] = [
            {
                'BACKEND':
                'django.template.backends.django.DjangoTemplates',
                'DIRS':
                env(
                    'TEMPLATE_DIRS',
                    [os.path.join(settings['BASE_DIR'], 'templates')],
                ),
                'OPTIONS': {
                    'debug':
                    boolean_ish(env('TEMPLATE_DEBUG', settings['DEBUG'])),
                    'context_processors': [
                        'django.contrib.auth.context_processors.auth',
                        'django.contrib.messages.context_processors.messages',
                        'django.core.context_processors.i18n',
                        'django.core.context_processors.debug',
                        'django.core.context_processors.request',
                        'django.core.context_processors.media',
                        'django.core.context_processors.csrf',
                        'django.core.context_processors.tz',
                        'django.core.context_processors.static',
                        'aldryn_django.context_processors.debug',
                    ],
                    'loaders': [
                        'django.template.loaders.filesystem.Loader',
                        'django.template.loaders.app_directories.Loader',
                        'django.template.loaders.eggs.Loader',
                    ],
                },
            },
        ]

        settings['MIDDLEWARE_CLASSES'] = [
            'django.contrib.sessions.middleware.SessionMiddleware',
            # 'django.middleware.common.CommonMiddleware',
            'django.middleware.csrf.CsrfViewMiddleware',
            'django.contrib.auth.middleware.AuthenticationMiddleware',
            # 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
            'django.contrib.messages.middleware.MessageMiddleware',
            'django.middleware.locale.LocaleMiddleware',
            'django.contrib.sites.middleware.CurrentSiteMiddleware',
            'django.middleware.common.CommonMiddleware',
            'django.middleware.clickjacking.XFrameOptionsMiddleware',
            # 'django.middleware.security.SecurityMiddleware',
        ]

        settings['SITE_ID'] = env('SITE_ID', 1)

        self.domain_settings(data, settings, env=env)
        self.security_settings(data, settings, env=env)
        self.server_settings(settings, env=env)
        self.logging_settings(settings, env=env)
        # Order matters, sentry settings rely on logging being configured.
        self.sentry_settings(settings, env=env)
        self.cache_settings(settings, env=env)
        self.storage_settings(settings, env=env)
        self.i18n_settings(data, settings, env=env)
        self.migration_settings(settings, env=env)
        return settings
Exemple #19
0
    async def check_for_updates(self):
        mani = str(yurl.URL(self.source).replace(fragment=None))
        if mani not in self._requests:
            self._requests[mani] = asyncio.ensure_future(self._real_update(mani))

        await self._requests[mani]
    def storage_settings_for_static(self, data, settings, env):
        import yurl

        use_gzip = not env("DISABLE_GZIP")
        use_manifest = data["use_manifeststaticfilesstorage"]
        if use_gzip:
            if use_manifest:
                storage = (
                    "aldryn_django.storage.ManifestGZippedStaticFilesStorage"
                )
            else:
                storage = "aldryn_django.storage.GZippedStaticFilesStorage"
        else:
            if use_manifest:
                storage = "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
            else:
                storage = (
                    "django.contrib.staticfiles.storage.StaticFilesStorage"
                )
        settings["STATICFILES_STORAGE"] = storage

        settings["STATIC_URL"] = env("STATIC_URL", "/static/")
        static_host = yurl.URL(settings["STATIC_URL"]).host
        settings["STATIC_URL_IS_ON_OTHER_DOMAIN"] = (
            static_host and static_host not in settings["ALLOWED_HOSTS"]
        )
        settings["STATIC_ROOT"] = env(
            "STATIC_ROOT",
            os.path.join(settings["BASE_DIR"], "static_collected"),
        )
        settings["STATIC_HEADERS"] = [
            # Set far-future expiration headers for static files with hashed
            # filenames. Also set cors headers to * for fonts.
            (
                r".*\.[0-9a-f]{10,16}\.(eot|ttf|otf|woff)",
                {
                    "Access-Control-Allow-Origin": "*",
                    "Cache-Control": "public, max-age={}".format(
                        3600 * 24 * 365
                    ),
                },
            ),
            (
                r".*\.[0-9a-f]{10,16}\.[a-z]+",
                {
                    "Cache-Control": "public, max-age={}".format(
                        3600 * 24 * 365
                    )
                },
            ),
            # Set default expiration headers for all remaining static files.
            # *Has to be last* as processing stops at the first matching
            # pattern it finds. Also set cors headers to * for fonts.
            (
                r".*\.(eot|ttf|otf|woff)",
                {
                    "Access-Control-Allow-Origin": "*",
                    "Cache-Control": "public, max-age={}".format(
                        settings["STATICFILES_DEFAULT_MAX_AGE"]
                    ),
                },
            ),
            (
                ".*",
                {
                    "Cache-Control": "public, max-age={}".format(
                        settings["STATICFILES_DEFAULT_MAX_AGE"]
                    )
                },
            ),
        ]
        settings["STATICFILES_DIRS"] = env(
            "STATICFILES_DIRS", [os.path.join(settings["BASE_DIR"], "static")]
        )