Coverage for portality / models / history.py: 100%
25 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-04 09:41 +0100
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-04 09:41 +0100
1from portality.core import app
2from portality.dao import DomainObject
3import os
4import json
6from portality.lib import dates
7from portality.lib.dates import FMT_DATE_STD
10class History(DomainObject):
11 SAVE_BASE_DIRECTORY = os.path.join(app.config['ARTICLE_HISTORY_DIR'], '..') # sensible default, but always override this in children
13 def save(self): # method signature intentionally kept different from base class to indicate you can't do the usual things on .save()
14 self.__save_to_file()
16 def __save_to_file(self):
17 """
18 Override usual DomainObject.save method so instead of sending
19 to Elasticsearch, this will save objects in files on a daily
20 directory basis. A new history object should be created every time
21 when a new snapshot of an article or journal is needed - i.e.
22 they are never changed and re-saved.
24 With this implementation if a history object is changed and saved
25 a 2nd time (after the initial save to put it on disk):
26 - the file will be overwritten (if it saved 2+ times on the same
27 day)
28 OR
29 - a new file will be created in a new directory (if the 2nd+ time
30 it's saved is on a different day).
31 """
32 self.set_id(self.makeid())
33 directory_name = dates.now_str(FMT_DATE_STD)
34 full_dir = os.path.join(self.SAVE_BASE_DIRECTORY, directory_name)
35 full_path = os.path.join(full_dir, "{0}.json".format(self.id))
37 if not os.path.isdir(full_dir):
38 os.makedirs(full_dir)
40 with open(full_path, 'w') as o:
41 o.write(json.dumps(self.data, indent=4))
44class ArticleHistory(History):
45 __type__ = "article_history"
46 SAVE_BASE_DIRECTORY = app.config['ARTICLE_HISTORY_DIR']
48class JournalHistory(History):
49 __type__ = "journal_history"
50 SAVE_BASE_DIRECTORY = app.config['JOURNAL_HISTORY_DIR']