Coverage for fastblocks/adapters/sitemap/dynamic.py: 0%

47 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-21 04:50 -0700

1"""FastBlocks Dynamic Sitemap Adapter. 

2 

3Database-driven sitemap generation for content management systems 

4and applications with dynamic URL structures. 

5""" 

6 

7import datetime as dt 

8import typing as t 

9from contextlib import suppress 

10from uuid import UUID 

11 

12from acb.adapters import AdapterStatus 

13from acb.debug import debug 

14from acb.depends import depends 

15 

16from ._base import SitemapBase, SitemapBaseSettings 

17from .core import BaseSitemap, SitemapApp 

18 

19 

20class DynamicSitemapSettings(SitemapBaseSettings): 

21 pass 

22 

23 

24class DynamicSitemap(BaseSitemap[dict[str, t.Any]], SitemapBase): 

25 sitemap: SitemapApp | None = None 

26 

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 

39 

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}") 

45 

46 return [ 

47 { 

48 "url": f"/{model_name.lower()}/sample-item", 

49 "lastmod": dt.datetime.now(), 

50 "priority": 0.7, 

51 } 

52 ] 

53 

54 def location(self, item: dict[str, t.Any]) -> str: 

55 return item.get("url", "/") 

56 

57 def lastmod(self, item: dict[str, t.Any]) -> dt.datetime | None: 

58 return item.get("lastmod") 

59 

60 def changefreq(self, item: dict[str, t.Any]) -> str: 

61 return item.get("changefreq", self.config.change_freq) 

62 

63 def priority(self, item: dict[str, t.Any]) -> float: 

64 return item.get("priority", 0.5) 

65 

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}") 

76 

77 

78Sitemap = DynamicSitemap 

79 

80MODULE_ID = UUID("01937d86-bf91-72a3-f564-890123456712") 

81MODULE_STATUS = AdapterStatus.STABLE 

82 

83with suppress(Exception): 

84 depends.set(Sitemap)