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 <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2022-09-23 21:47:37 -07:00 committed by Tim Abbott
parent 02be415122
commit 9eefc290a9
2 changed files with 18 additions and 18 deletions

View File

@ -2,12 +2,9 @@ import os
import sys import sys
import time import time
from copy import deepcopy from copy import deepcopy
from pathlib import Path
from typing import Any, Dict, List, Tuple, Union from typing import Any, Dict, List, Tuple, Union
from urllib.parse import urljoin from urllib.parse import urljoin
from django.template.loaders import app_directories
import zerver.lib.logging_util import zerver.lib.logging_util
from scripts.lib.zulip_tools import get_tornado_ports from scripts.lib.zulip_tools import get_tornado_ports
from zerver.lib.db import TimeTrackingConnection, TimeTrackingCursor 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. # ... and with the hosts in REALM_HOSTS.
ALLOWED_HOSTS += REALM_HOSTS.values() 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 = ( MIDDLEWARE = (
"zerver.middleware.TagRequests", "zerver.middleware.TagRequests",
"zerver.middleware.SetRemoteAddrFromRealIpHeader", "zerver.middleware.SetRemoteAddrFromRealIpHeader",
@ -675,7 +658,7 @@ non_html_template_engine_settings["OPTIONS"].update(
two_factor_template_options = deepcopy(default_template_engine_settings["OPTIONS"]) two_factor_template_options = deepcopy(default_template_engine_settings["OPTIONS"])
del two_factor_template_options["environment"] del two_factor_template_options["environment"]
del two_factor_template_options["extensions"] 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 = { two_factor_template_engine_settings = {
"NAME": "Two_Factor", "NAME": "Two_Factor",

View File

@ -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