From 9eefc290a9e3efbf5ad4591d8c358fe0377e5bac Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Fri, 23 Sep 2022 21:47:37 -0700 Subject: [PATCH] template_loaders: Extract TwoFactorLoader to new module. This breaks an import cycle that prevented django-stubs from inferring types for django.conf.settings. Signed-off-by: Anders Kaseorg --- zproject/computed_settings.py | 19 +------------------ zproject/template_loaders.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 18 deletions(-) create mode 100644 zproject/template_loaders.py diff --git a/zproject/computed_settings.py b/zproject/computed_settings.py index ba38228e3f..b11926ff2a 100644 --- a/zproject/computed_settings.py +++ b/zproject/computed_settings.py @@ -2,12 +2,9 @@ import os import sys import time from copy import deepcopy -from pathlib import Path from typing import Any, Dict, List, Tuple, Union from urllib.parse import urljoin -from django.template.loaders import app_directories - import zerver.lib.logging_util from scripts.lib.zulip_tools import get_tornado_ports from zerver.lib.db import TimeTrackingConnection, TimeTrackingCursor @@ -161,20 +158,6 @@ ALLOWED_HOSTS += [EXTERNAL_HOST_WITHOUT_PORT, "." + EXTERNAL_HOST_WITHOUT_PORT] # ... and with the hosts in REALM_HOSTS. ALLOWED_HOSTS += REALM_HOSTS.values() - -class TwoFactorLoader(app_directories.Loader): - def get_dirs(self) -> List[Union[str, Path]]: - dirs = super().get_dirs() - # app_directories.Loader returns only a list of - # Path objects by calling get_app_template_dirs - two_factor_dirs: List[Union[str, Path]] = [] - for d in dirs: - assert isinstance(d, Path) - if d.match("two_factor/*"): - two_factor_dirs.append(d) - return two_factor_dirs - - MIDDLEWARE = ( "zerver.middleware.TagRequests", "zerver.middleware.SetRemoteAddrFromRealIpHeader", @@ -675,7 +658,7 @@ non_html_template_engine_settings["OPTIONS"].update( two_factor_template_options = deepcopy(default_template_engine_settings["OPTIONS"]) del two_factor_template_options["environment"] del two_factor_template_options["extensions"] -two_factor_template_options["loaders"] = ["zproject.settings.TwoFactorLoader"] +two_factor_template_options["loaders"] = ["zproject.template_loaders.TwoFactorLoader"] two_factor_template_engine_settings = { "NAME": "Two_Factor", diff --git a/zproject/template_loaders.py b/zproject/template_loaders.py new file mode 100644 index 0000000000..f67ae0a05f --- /dev/null +++ b/zproject/template_loaders.py @@ -0,0 +1,17 @@ +from pathlib import Path +from typing import List, Union + +from django.template.loaders import app_directories + + +class TwoFactorLoader(app_directories.Loader): + def get_dirs(self) -> List[Union[str, Path]]: + dirs = super().get_dirs() + # app_directories.Loader returns only a list of + # Path objects by calling get_app_template_dirs + two_factor_dirs: List[Union[str, Path]] = [] + for d in dirs: + assert isinstance(d, Path) + if d.match("two_factor/*"): + two_factor_dirs.append(d) + return two_factor_dirs