Coverage for portality / models / cache.py: 78%

58 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-05 00:09 +0100

1""" The cache contains file metadata for the sitemap, journal csv, and the public data dump. It also holds site stats 

2 for the front page """ 

3 

4from portality.dao import DomainObject 

5from portality.core import app 

6from portality.lib import dates 

7from portality.lib.dates import DEFAULT_TIMESTAMP_VAL 

8 

9 

10class Cache(DomainObject): 

11 __type__ = "cache" 

12 

13 @classmethod 

14 def get_site_statistics(cls): 

15 rec = cls.pull("site_statistics") 

16 if rec is not None: 

17 try: 

18 return { 

19 "journals": rec.data.get("journals"), 

20 "countries": rec.data.get("countries"), 

21 "abstracts": rec.data.get("abstracts"), 

22 "no_apc": rec.data.get("no_apc"), 

23 "new_journals": rec.data.get("new_journals") 

24 } 

25 except AttributeError: 

26 pass # Return None below 

27 

28 # if we get to here, then we don't return the cache 

29 return None 

30 

31 @classmethod 

32 def cache_site_statistics(cls, stats): 

33 cobj = cls(**stats) 

34 cobj.set_id("site_statistics") 

35 cobj.save() 

36 

37 @classmethod 

38 def cache_sitemap(cls, url): 

39 cobj = cls(**{ 

40 "filename": url 

41 }) 

42 cobj.set_id("sitemap") 

43 cobj.save() 

44 

45 @classmethod 

46 def cache_nth_sitemap(cls, n, url): 

47 cobj = cls(**{ 

48 "filename": url 

49 }) 

50 cobj.set_id("sitemap"+str(n)) 

51 cobj.save() 

52 

53 @classmethod 

54 def get_sitemap(cls, n): 

55 rec = cls.pull("sitemap"+str(n)) 

56 if rec is None: 

57 return None 

58 return rec.get("filename") 

59 

60 @classmethod 

61 def cache_sitemap_indexes(cls, urls): 

62 """Cache multiple sitemap index URLs""" 

63 for idx, url in enumerate(urls): 

64 cobj = cls(**{ 

65 "filename": url 

66 }) 

67 cobj.set_id(f"sitemap_index_{idx}") 

68 cobj.save() 

69 

70 @classmethod 

71 def get_sitemap_index(cls, n): 

72 """Get a specific sitemap index URL""" 

73 rec = cls.pull(f"sitemap_index_{n}") 

74 if rec is None: 

75 return None 

76 return rec.get("filename") 

77 

78 @classmethod 

79 def cache_public_data_dump(cls, article_container, article_filename, article_url, article_size, 

80 journal_container, journal_filename, journal_url, journal_size): 

81 cobj = cls(**{ 

82 "article": { 

83 "container": article_container, 

84 "filename": article_filename, 

85 "url" : article_url, 

86 "size" : article_size 

87 }, 

88 "journal": { 

89 "container": journal_container, 

90 "filename": journal_filename, 

91 "url" : journal_url, 

92 "size" : journal_size 

93 } 

94 }) 

95 cobj.set_id("public_data_dump") 

96 cobj.save() 

97 

98 @classmethod 

99 def get_public_data_dump(cls): 

100 return cls.pull("public_data_dump") 

101 

102 def marked_regen(self): 

103 return self.data.get("regen", False)