Refine runtime config and redirect settings
This commit is contained in:
@@ -226,6 +226,7 @@ def _render_config_page(
|
||||
"config_saved": False,
|
||||
"config_sections": build_config_sections(auth_db_session, settings),
|
||||
"ticktick_oauth_ready": is_ticktick_oauth_ready(settings),
|
||||
"ticktick_redirect_uri": settings.ticktick_redirect_uri,
|
||||
"ticktick_oauth_notice": None,
|
||||
"ticktick_oauth_error": None,
|
||||
},
|
||||
|
||||
@@ -78,6 +78,7 @@ def config_page(
|
||||
"config_saved": request.query_params.get("saved") == "1",
|
||||
"config_sections": build_config_sections(auth_db_session, settings),
|
||||
"ticktick_oauth_ready": is_ticktick_oauth_ready(settings),
|
||||
"ticktick_redirect_uri": settings.ticktick_redirect_uri,
|
||||
"ticktick_oauth_notice": ticktick_oauth_notice,
|
||||
"ticktick_oauth_error": ticktick_oauth_error,
|
||||
}
|
||||
@@ -109,6 +110,7 @@ async def config_submit(
|
||||
"config_saved": False,
|
||||
"config_sections": build_config_sections(auth_db_session, settings),
|
||||
"ticktick_oauth_ready": is_ticktick_oauth_ready(settings),
|
||||
"ticktick_redirect_uri": settings.ticktick_redirect_uri,
|
||||
"ticktick_oauth_notice": None,
|
||||
"ticktick_oauth_error": None,
|
||||
}
|
||||
@@ -135,6 +137,7 @@ async def config_submit(
|
||||
"config_saved": False,
|
||||
"config_sections": build_config_sections(auth_db_session, refreshed_settings),
|
||||
"ticktick_oauth_ready": is_ticktick_oauth_ready(refreshed_settings),
|
||||
"ticktick_redirect_uri": refreshed_settings.ticktick_redirect_uri,
|
||||
"ticktick_oauth_notice": None,
|
||||
"ticktick_oauth_error": None,
|
||||
}
|
||||
|
||||
+20
-5
@@ -7,10 +7,9 @@ from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
class Settings(BaseSettings):
|
||||
app_name: str = "Home Automation Backend (Python)"
|
||||
app_env: str = "development"
|
||||
app_env: str = "production"
|
||||
app_debug: bool = False
|
||||
app_host: str = "0.0.0.0"
|
||||
app_port: int = 8000
|
||||
app_hostname: str = "localhost:8000"
|
||||
app_database_url: str = "sqlite:///./data/app.db"
|
||||
|
||||
location_database_url: str = "sqlite:///./data/locationRecorder.db"
|
||||
@@ -18,7 +17,6 @@ class Settings(BaseSettings):
|
||||
|
||||
ticktick_client_id: str = ""
|
||||
ticktick_client_secret: str = ""
|
||||
ticktick_redirect_uri: str = ""
|
||||
ticktick_token: str = ""
|
||||
|
||||
home_assistant_base_url: str = ""
|
||||
@@ -32,12 +30,13 @@ class Settings(BaseSettings):
|
||||
auth_bootstrap_password: str = "admin"
|
||||
auth_session_cookie_name: str = "home_automation_session"
|
||||
auth_session_ttl_hours: int = 12
|
||||
auth_cookie_secure_override: bool | None = None
|
||||
auth_cookie_secure_override: bool | None = True
|
||||
|
||||
model_config = SettingsConfigDict(
|
||||
env_file=".env",
|
||||
env_file_encoding="utf-8",
|
||||
case_sensitive=False,
|
||||
extra="ignore",
|
||||
)
|
||||
|
||||
@computed_field
|
||||
@@ -45,6 +44,22 @@ class Settings(BaseSettings):
|
||||
def is_development(self) -> bool:
|
||||
return self.app_env.lower() == "development"
|
||||
|
||||
@computed_field
|
||||
@property
|
||||
def app_base_url(self) -> str:
|
||||
hostname = self.app_hostname.strip().rstrip("/")
|
||||
if not hostname:
|
||||
return ""
|
||||
scheme = "http" if self.is_development else "https"
|
||||
return f"{scheme}://{hostname}"
|
||||
|
||||
@computed_field
|
||||
@property
|
||||
def ticktick_redirect_uri(self) -> str:
|
||||
if not self.app_base_url:
|
||||
return ""
|
||||
return f"{self.app_base_url}/ticktick/auth/code"
|
||||
|
||||
@staticmethod
|
||||
def _sqlite_path_from_url(database_url: str) -> Path | None:
|
||||
prefix = "sqlite:///"
|
||||
|
||||
@@ -278,7 +278,7 @@ class TickTickClient:
|
||||
return self.settings.ticktick_client_secret.strip()
|
||||
|
||||
def _redirect_uri(self) -> str:
|
||||
return self.settings.ticktick_redirect_uri.strip()
|
||||
return self.settings.ticktick_redirect_uri
|
||||
|
||||
def _require_auth_config(self) -> None:
|
||||
if not self.is_configured():
|
||||
@@ -288,7 +288,7 @@ class TickTickClient:
|
||||
)
|
||||
if not self._redirect_uri():
|
||||
raise TickTickConfigError(
|
||||
"TickTick integration is missing TICKTICK_REDIRECT_URI."
|
||||
"TickTick integration is missing APP_HOSTNAME for OAuth callback generation."
|
||||
)
|
||||
|
||||
def _require_token(self) -> None:
|
||||
|
||||
@@ -26,8 +26,7 @@ CONFIG_FIELDS: tuple[ConfigField, ...] = (
|
||||
ConfigField("System", "APP_NAME", "app_name", "App Name"),
|
||||
ConfigField("System", "APP_ENV", "app_env", "App Env"),
|
||||
ConfigField("System", "APP_DEBUG", "app_debug", "App Debug"),
|
||||
ConfigField("System", "APP_HOST", "app_host", "App Host"),
|
||||
ConfigField("System", "APP_PORT", "app_port", "App Port"),
|
||||
ConfigField("System", "APP_HOSTNAME", "app_hostname", "App Hostname"),
|
||||
ConfigField(
|
||||
"Authentication",
|
||||
"AUTH_SESSION_COOKIE_NAME",
|
||||
@@ -62,12 +61,6 @@ CONFIG_FIELDS: tuple[ConfigField, ...] = (
|
||||
"TickTick Client Secret",
|
||||
secret=True,
|
||||
),
|
||||
ConfigField(
|
||||
"TickTick",
|
||||
"TICKTICK_REDIRECT_URI",
|
||||
"ticktick_redirect_uri",
|
||||
"TickTick Redirect URI",
|
||||
),
|
||||
ConfigField("TickTick", "TICKTICK_TOKEN", "ticktick_token", "TickTick Token", secret=True),
|
||||
ConfigField(
|
||||
"Home Assistant",
|
||||
@@ -190,9 +183,9 @@ def save_config_value(
|
||||
|
||||
def is_ticktick_oauth_ready(settings: Settings) -> bool:
|
||||
return bool(
|
||||
settings.ticktick_client_id
|
||||
settings.app_hostname
|
||||
and settings.ticktick_client_id
|
||||
and settings.ticktick_client_secret
|
||||
and settings.ticktick_redirect_uri
|
||||
)
|
||||
|
||||
|
||||
@@ -244,14 +237,12 @@ def _settings_payload(settings: Settings) -> dict[str, Any]:
|
||||
"app_name": settings.app_name,
|
||||
"app_env": settings.app_env,
|
||||
"app_debug": settings.app_debug,
|
||||
"app_host": settings.app_host,
|
||||
"app_port": settings.app_port,
|
||||
"app_hostname": settings.app_hostname,
|
||||
"app_database_url": settings.app_database_url,
|
||||
"location_database_url": settings.location_database_url,
|
||||
"poo_database_url": settings.poo_database_url,
|
||||
"ticktick_client_id": settings.ticktick_client_id,
|
||||
"ticktick_client_secret": settings.ticktick_client_secret,
|
||||
"ticktick_redirect_uri": settings.ticktick_redirect_uri,
|
||||
"ticktick_token": settings.ticktick_token,
|
||||
"home_assistant_base_url": settings.home_assistant_base_url,
|
||||
"home_assistant_auth_token": settings.home_assistant_auth_token,
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
{% endif %}
|
||||
|
||||
{% if config_saved %}
|
||||
<div class="notice">config saved to .env. Some changes may require an app restart.</div>
|
||||
<div class="notice">config saved to the app database. Some changes may require an app restart.</div>
|
||||
{% endif %}
|
||||
|
||||
{% if ticktick_oauth_error %}
|
||||
@@ -88,10 +88,11 @@
|
||||
<div class="integration-action-row">
|
||||
<div>
|
||||
<p class="integration-action-title">TickTick OAuth</p>
|
||||
<p class="integration-action-copy">Redirect URI: {{ ticktick_redirect_uri or "configure APP_HOSTNAME to generate the callback URI" }}</p>
|
||||
{% if ticktick_oauth_ready %}
|
||||
<p class="integration-action-copy">Use the saved TickTick client settings to start the authorization flow.</p>
|
||||
{% else %}
|
||||
<p class="integration-action-copy">Fill in TickTick Client ID, Client Secret, and Redirect URI before starting OAuth.</p>
|
||||
<p class="integration-action-copy">Fill in App Hostname, TickTick Client ID, and TickTick Client Secret before starting OAuth.</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% if ticktick_oauth_ready %}
|
||||
|
||||
Reference in New Issue
Block a user