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

55 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-02 20:00 -0800

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 

12 

13# Custom implementations for ACB compatibility 

14class AdapterStatus: 

15 """Custom AdapterStatus for Oneiric compatibility.""" 

16 

17 STABLE = "STABLE" 

18 BETA = "BETA" 

19 ALPHA = "ALPHA" 

20 EXPERIMENTAL = "EXPERIMENTAL" 

21 

22 

23def debug(msg: str) -> None: 

24 """Custom debug function for Oneiric compatibility.""" 

25 print(f"[DEBUG] {msg}") 

26 

27 

28from oneiric.core.resolution import Resolver 

29 

30# Oneiric resolver for dependency injection 

31depends = Resolver() 

32 

33 

34def import_adapter(adapter_name: str) -> None: 

35 """Custom implementation for Oneiric compatibility.""" 

36 return None 

37 

38 

39from ._base import SitemapBase, SitemapBaseSettings 

40from .core import BaseSitemap, SitemapApp 

41 

42 

43class DynamicSitemapSettings(SitemapBaseSettings): 

44 pass 

45 

46 

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

48 sitemap: SitemapApp | None = None 

49 

50 async def items(self) -> list[dict[str, t.Any]]: 

51 strategy_options = self.config.strategy_options 

52 model_configs = strategy_options.get("model_configs", []) 

53 all_items = [] 

54 for model_config in model_configs: 

55 try: 

56 items = await self._get_model_items(model_config) 

57 all_items.extend(items) 

58 except Exception as e: 

59 debug(f"DynamicSitemap: Error loading model {model_config}: {e}") 

60 debug(f"DynamicSitemap: Generated {len(all_items)} dynamic URLs") 

61 return all_items 

62 

63 async def _get_model_items( 

64 self, model_config: dict[str, t.Any] 

65 ) -> list[dict[str, t.Any]]: 

66 model_name = model_config.get("model", "Unknown") 

67 debug(f"DynamicSitemap: Loading items from model {model_name}") 

68 

69 return [ 

70 { 

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

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

73 "priority": 0.7, 

74 } 

75 ] 

76 

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

78 return t.cast(str, item.get("url", "/")) 

79 

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

81 return item.get("lastmod") 

82 

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

84 return t.cast(str, item.get("changefreq", self.config.change_freq)) 

85 

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

87 return t.cast(float, item.get("priority", 0.5)) 

88 

89 async def init(self) -> None: 

90 if not self.config.domain: 

91 msg = "domain must be set in sitemap settings" 

92 raise ValueError(msg) 

93 self.sitemap = SitemapApp( 

94 self, 

95 domain=self.config.domain, 

96 cache_ttl=self.config.cache_ttl, 

97 ) 

98 debug(f"DynamicSitemap: Initialized with domain={self.config.domain}") 

99 

100 

101Sitemap = DynamicSitemap 

102 

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

104MODULE_STATUS = AdapterStatus.STABLE 

105 

106with suppress(Exception): 

107 depends.set(Sitemap)