Skip to content

Commit 4d6d6c2

Browse files
committed
refactor(site_lock): follow the module layout (moves only)
constants, settings and state move to site_lock/core/; the middleware, unlock page (with its template) and attempt limiter to site_lock/web/; tests mirror them. site_lock.settings stays as a deprecated alias until 0.0.46. Claude-Session: https://claude.ai/code/session_01FXHA3ZicrtZ4C5QPEgxfgq
1 parent 4328284 commit 4d6d6c2

20 files changed

Lines changed: 85 additions & 76 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ All notable changes to this project are documented in this file. The format is b
1818

1919
- Scaffold new modules with `core/` and `web/` areas and mirrored nested Python tests; existing modules migrate individually. See `docs/module-layout.md`.
2020
- `mail` follows the module layout: `mail.core.{constants,settings,state,data_compatibility}` and `mail.delivery.{sender,runtime}`. `mail.settings` and `mail.constants` still import, with a `DeprecationWarning`, until 0.0.46.
21+
- `site_lock` follows the module layout: `site_lock.core.{constants,settings,state}` and `site_lock.web.{middleware,page,rate_limit}` (the unlock template moves with the page). `site_lock.settings` still imports, with a `DeprecationWarning`, until 0.0.46.
2122

2223
### Fixed
2324

‎docs/module-layout.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,6 @@ when a later module PR moves files.
210210
| permissions | 12 | 14 | flat |
211211
| settings | 31 | 43 | flat |
212212
| sharing | 13 | 6 | flat |
213-
| site_lock | 8 | 5 | flat |
213+
| site_lock | 2 (+1 shim) | 0 | follows this layout |
214214
| tenants | 19 | 23 | flat |
215215
| users | 31 | 97 | flat |

‎host/tests/test_module_layout.py‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,6 @@
127127
services.py settings.py tenant_cleanup.py user_cleanup.py
128128
""".split()
129129
),
130-
"site_lock": frozenset(
131-
"""
132-
constants.py middleware.py page.py rate_limit.py settings.py state.py templates
133-
""".split()
134-
),
135130
"tenants": frozenset(
136131
"""
137132
_deletion.py _membership_ops.py _owner_guard.py components constants.py deps.py

‎modules/settings/tests/test_lenient_related_fields_validators.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from simple_module_core.settings_hydrate import RELATED_FIELDS_CTX, build_settings
1515
from simple_module_core.stored_settings import construct_leniently
1616
from simple_module_hosting.host_settings import HostSettings
17-
from site_lock.settings import SiteLockSettings
17+
from site_lock.core.settings import SiteLockSettings
1818
from users.settings import UsersSettings
1919

2020

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""What the site lock builds on: constants, settings and app state."""
File renamed without changes.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Site Lock module settings — DB-backed via ``register_module_settings``.
2+
3+
The field is named ``password`` so the settings admin UI masks it
4+
automatically (``settings._module_settings.is_secret_field``).
5+
"""
6+
7+
from __future__ import annotations
8+
9+
from pydantic import model_validator
10+
from pydantic_settings import SettingsConfigDict
11+
from simple_module_core.settings_base import DbBackedSettings
12+
from simple_module_core.stored_settings import field_error
13+
14+
_BLANK_PASSWORD_MSG = "password must be set before enabling the site lock"
15+
16+
17+
class SiteLockSettings(DbBackedSettings):
18+
"""Site-wide shared-password gate configuration."""
19+
20+
# ``DbBackedSettings`` (not ``BaseSettings``) so the DB is genuinely the
21+
# only source: omitting ``env_prefix`` would leave pydantic-settings
22+
# reading each field from its bare name — GH #283.
23+
model_config = SettingsConfigDict(extra="ignore")
24+
25+
enabled: bool = False
26+
password: str = ""
27+
message: str = ""
28+
29+
@model_validator(mode="after")
30+
def _password_required_when_enabled(self) -> SiteLockSettings:
31+
"""Refuse to gate the site behind an empty password.
32+
33+
Without this, flipping ``enabled`` on before setting a password would
34+
lock every visitor out from behind a secret that is the empty string.
35+
Raising here makes ``apply_changes_and_reload`` reject the change so
36+
the settings screen shows a validation error instead.
37+
38+
The error is deliberately pinned to ``enabled`` rather than raised as a
39+
bare ``ValueError``. A plain raise from a model validator carries
40+
``loc=[]``, and the shared settings form only renders errors it can
41+
attach to a field (``ModuleForm.onSave`` keys them by ``loc[-1]``).
42+
With an empty ``loc`` the 422 was silently swallowed: the admin saw the
43+
toggle stay on, no error, and had every reason to believe the site was
44+
locked when it was still wide open.
45+
"""
46+
if self.enabled and not self.password.strip():
47+
raise field_error(
48+
type(self),
49+
"enabled",
50+
"site_lock_password_required",
51+
_BLANK_PASSWORD_MSG,
52+
value=self.enabled,
53+
related=["enabled", "password"],
54+
)
55+
return self
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
from dataclasses import dataclass, field
66

7-
from site_lock import constants as c
8-
from site_lock.rate_limit import AttemptLimiter
9-
from site_lock.settings import SiteLockSettings
7+
from site_lock.core import constants as c
8+
from site_lock.core.settings import SiteLockSettings
9+
from site_lock.web.rate_limit import AttemptLimiter
1010

1111

1212
def _default_limiter() -> AttemptLimiter:

‎modules/site_lock/site_lock/module.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from simple_module_core.module_settings import register_module_settings
99
from simple_module_hosting.session import preserve_across_sign_in
1010

11-
from site_lock import constants as c
11+
from site_lock.core import constants as c
1212

1313
if TYPE_CHECKING:
1414
from fastapi import FastAPI
@@ -26,8 +26,8 @@ class SiteLockModule(ModuleBase):
2626
)
2727

2828
def register_settings(self, app: FastAPI) -> None:
29-
from site_lock.settings import SiteLockSettings
30-
from site_lock.state import SiteLockState
29+
from site_lock.core.settings import SiteLockSettings
30+
from site_lock.core.state import SiteLockState
3131

3232
# Signing in replaces the session; the unlock marker must outlive it.
3333
preserve_across_sign_in(app, c.SESSION_KEY)
@@ -39,6 +39,6 @@ def register_settings(self, app: FastAPI) -> None:
3939
)
4040

4141
def register_middleware(self, app: FastAPI) -> None:
42-
from site_lock.middleware import SiteLockMiddleware
42+
from site_lock.web.middleware import SiteLockMiddleware
4343

4444
app.add_middleware(SiteLockMiddleware)
Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,11 @@
1-
"""Site Lock module settings — DB-backed via ``register_module_settings``.
2-
3-
The field is named ``password`` so the settings admin UI masks it
4-
automatically (``settings._module_settings.is_secret_field``).
5-
"""
1+
"""Moved to :mod:`site_lock.core.settings`; this alias goes in 0.0.46 (module layout)."""
62

73
from __future__ import annotations
84

9-
from pydantic import model_validator
10-
from pydantic_settings import SettingsConfigDict
11-
from simple_module_core.settings_base import DbBackedSettings
12-
from simple_module_core.stored_settings import field_error
13-
14-
_BLANK_PASSWORD_MSG = "password must be set before enabling the site lock"
15-
16-
17-
class SiteLockSettings(DbBackedSettings):
18-
"""Site-wide shared-password gate configuration."""
19-
20-
# ``DbBackedSettings`` (not ``BaseSettings``) so the DB is genuinely the
21-
# only source: omitting ``env_prefix`` would leave pydantic-settings
22-
# reading each field from its bare name — GH #283.
23-
model_config = SettingsConfigDict(extra="ignore")
24-
25-
enabled: bool = False
26-
password: str = ""
27-
message: str = ""
28-
29-
@model_validator(mode="after")
30-
def _password_required_when_enabled(self) -> SiteLockSettings:
31-
"""Refuse to gate the site behind an empty password.
5+
import importlib
6+
import sys
327

33-
Without this, flipping ``enabled`` on before setting a password would
34-
lock every visitor out from behind a secret that is the empty string.
35-
Raising here makes ``apply_changes_and_reload`` reject the change so
36-
the settings screen shows a validation error instead.
8+
from simple_module_core.deprecation import deprecated_module
379

38-
The error is deliberately pinned to ``enabled`` rather than raised as a
39-
bare ``ValueError``. A plain raise from a model validator carries
40-
``loc=[]``, and the shared settings form only renders errors it can
41-
attach to a field (``ModuleForm.onSave`` keys them by ``loc[-1]``).
42-
With an empty ``loc`` the 422 was silently swallowed: the admin saw the
43-
toggle stay on, no error, and had every reason to believe the site was
44-
locked when it was still wide open.
45-
"""
46-
if self.enabled and not self.password.strip():
47-
raise field_error(
48-
type(self),
49-
"enabled",
50-
"site_lock_password_required",
51-
_BLANK_PASSWORD_MSG,
52-
value=self.enabled,
53-
related=["enabled", "password"],
54-
)
55-
return self
10+
deprecated_module("site_lock.settings", remove_in="0.0.46", use="site_lock.core.settings")
11+
sys.modules[__name__] = importlib.import_module("site_lock.core.settings")

0 commit comments

Comments
 (0)