Browse Source

Added first metadata and page location changed

master
Dirk Alders 3 months ago
parent
commit
ec36afe683
1 changed files with 60 additions and 3 deletions
  1. 60
    3
      pages/page.py

+ 60
- 3
pages/page.py View File

1
 from django.conf import settings
1
 from django.conf import settings
2
 
2
 
3
 import fstools
3
 import fstools
4
+import json
4
 import logging
5
 import logging
5
 from pages import messages, url_page
6
 from pages import messages, url_page
6
 import mycreole
7
 import mycreole
7
 import os
8
 import os
9
+import time
8
 
10
 
9
 logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
11
 logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
10
 
12
 
11
 
13
 
14
+class meta_data(dict):
15
+    KEY_CREATION_TIME = "creation_time"
16
+    KEY_MODIFIED_TIME = "modified_time"
17
+    KEY_MODIFIED_USER = "modified_user"
18
+
19
+    def __init__(self, meta_filename, page_exists):
20
+        self._meta_filename = meta_filename
21
+
22
+        # Load data from disk
23
+        try:
24
+            with open(meta_filename, 'r') as fh:
25
+                super().__init__(json.load(fh))
26
+        except (FileNotFoundError, json.decoder.JSONDecodeError) as e:
27
+            super().__init__()
28
+
29
+        # Add missing information to meta_data
30
+        missing_keys = False
31
+        if self.KEY_CREATION_TIME not in self:
32
+            missing_keys = True
33
+            self[self.KEY_CREATION_TIME] = int(time.time())
34
+        if self.KEY_MODIFIED_TIME not in self:
35
+            self[self.KEY_MODIFIED_TIME] = self[self.KEY_CREATION_TIME]
36
+        if missing_keys and page_exists:
37
+            self.save()
38
+
39
+    def update(self, username):
40
+        self[self.KEY_MODIFIED_TIME] = int(time.time())
41
+        self[self.KEY_MODIFIED_USER] = username
42
+        #
43
+        self.save()
44
+
45
+    def save(self):
46
+        with open(self._meta_filename, 'w') as fh:
47
+            json.dump(self, fh, indent=4)
48
+
49
+
12
 class base_page(object):
50
 class base_page(object):
13
-    FOLDER_CONTENT = 'content'
51
+    PAGE_FILE_NAME = 'page'
14
-    FILE_NAME = 'page'
52
+    META_FILE_NAME = 'meta.json'
15
     SPLITCHAR = ":"
53
     SPLITCHAR = ":"
16
 
54
 
17
     def __init__(self, path):
55
     def __init__(self, path):
20
         else:
58
         else:
21
             self._path = os.path.join(settings.PAGES_ROOT, path.replace("/", 2*self.SPLITCHAR))
59
             self._path = os.path.join(settings.PAGES_ROOT, path.replace("/", 2*self.SPLITCHAR))
22
         self._raw_page_src = None
60
         self._raw_page_src = None
61
+        #
62
+        self._meta_data = meta_data(self._meta_filename, self.is_available())
23
 
63
 
24
     def _load_page_src(self):
64
     def _load_page_src(self):
25
         if self._raw_page_src is None:
65
         if self._raw_page_src is None:
38
         with open(self.filename, 'w') as fh:
78
         with open(self.filename, 'w') as fh:
39
             fh.write(page_txt)
79
             fh.write(page_txt)
40
         update_item(self)
80
         update_item(self)
81
+        #
82
+        self._update_metadata()
41
 
83
 
42
     @property
84
     @property
43
     def filename(self):
85
     def filename(self):
44
-        return os.path.join(self._path, self.FOLDER_CONTENT, self.FILE_NAME)
86
+        return os.path.join(self._path, self.PAGE_FILE_NAME)
87
+
88
+    @property
89
+    def _meta_filename(self):
90
+        return os.path.join(self._path, self.META_FILE_NAME)
45
 
91
 
46
     @property
92
     @property
47
     def rel_path(self):
93
     def rel_path(self):
65
         self._load_page_src()
111
         self._load_page_src()
66
         return self._raw_page_src
112
         return self._raw_page_src
67
 
113
 
114
+    def _update_metadata(self):
115
+        username = None
116
+        try:
117
+            if self._request.user.is_authenticated:
118
+                username = self._request.user.username
119
+            else:
120
+                logger.warning("Page edit without having a logged in user. This is not recommended. Check your access definitions!")
121
+        except AttributeError:
122
+            logger.exception("Page edit without having a request object. Check programming!")
123
+        self._meta_data.update(username)
124
+
68
 
125
 
69
 class creole_page(base_page):
126
 class creole_page(base_page):
70
     FOLDER_ATTACHMENTS = "attachments"
127
     FOLDER_ATTACHMENTS = "attachments"

Loading…
Cancel
Save