|
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).""" |
6 | 2 |
|
7 | 3 | from __future__ import annotations |
8 | 4 |
|
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 |
32 | 7 |
|
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 |
37 | 9 |
|
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