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