Coverage for fastblocks/adapters/sitemap/dynamic.py: 0%
47 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-10-09 00:47 -0700
« prev ^ index » next coverage.py v7.10.7, created at 2025-10-09 00:47 -0700
1"""FastBlocks Dynamic Sitemap Adapter.
3Database-driven sitemap generation for content management systems
4and applications with dynamic URL structures.
5"""
7import datetime as dt
8import typing as t
9from contextlib import suppress
10from uuid import UUID
12from acb.adapters import AdapterStatus
13from acb.debug import debug
14from acb.depends import depends
16from ._base import SitemapBase, SitemapBaseSettings
17from .core import BaseSitemap, SitemapApp
20class DynamicSitemapSettings(SitemapBaseSettings):
21 pass
24class DynamicSitemap(BaseSitemap[dict[str, t.Any]], SitemapBase): # type: ignore[override,misc]
25 sitemap: SitemapApp | None = None
27 async def items(self) -> list[dict[str, t.Any]]:
28 strategy_options = self.config.strategy_options
29 model_configs = strategy_options.get("model_configs", [])
30 all_items = []
31 for model_config in model_configs:
32 try:
33 items = await self._get_model_items(model_config)
34 all_items.extend(items)
35 except Exception as e:
36 debug(f"DynamicSitemap: Error loading model {model_config}: {e}")
37 debug(f"DynamicSitemap: Generated {len(all_items)} dynamic URLs")
38 return all_items
40 async def _get_model_items(
41 self, model_config: dict[str, t.Any]
42 ) -> list[dict[str, t.Any]]:
43 model_name = model_config.get("model", "Unknown")
44 debug(f"DynamicSitemap: Loading items from model {model_name}")
46 return [
47 {
48 "url": f"/{model_name.lower()}/sample-item",
49 "lastmod": dt.datetime.now(),
50 "priority": 0.7,
51 }
52 ]
54 def location(self, item: dict[str, t.Any]) -> str:
55 return t.cast(str, item.get("url", "/"))
57 def lastmod(self, item: dict[str, t.Any]) -> dt.datetime | None:
58 return item.get("lastmod")
60 def changefreq(self, item: dict[str, t.Any]) -> str:
61 return t.cast(str, item.get("changefreq", self.config.change_freq))
63 def priority(self, item: dict[str, t.Any]) -> float:
64 return t.cast(float, item.get("priority", 0.5))
66 async def init(self) -> None:
67 if not self.config.domain:
68 msg = "domain must be set in sitemap settings"
69 raise ValueError(msg)
70 self.sitemap = SitemapApp(
71 self,
72 domain=self.config.domain,
73 cache_ttl=self.config.cache_ttl,
74 )
75 debug(f"DynamicSitemap: Initialized with domain={self.config.domain}")
78Sitemap = DynamicSitemap
80MODULE_ID = UUID("01937d86-bf91-72a3-f564-890123456712")
81MODULE_STATUS = AdapterStatus.STABLE
83with suppress(Exception):
84 depends.set(Sitemap)