Browse Source

Added first metadata and page location changed

master
Dirk Alders 2 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,17 +1,55 @@
1 1
 from django.conf import settings
2 2
 
3 3
 import fstools
4
+import json
4 5
 import logging
5 6
 from pages import messages, url_page
6 7
 import mycreole
7 8
 import os
9
+import time
8 10
 
9 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 50
 class base_page(object):
13
-    FOLDER_CONTENT = 'content'
14
-    FILE_NAME = 'page'
51
+    PAGE_FILE_NAME = 'page'
52
+    META_FILE_NAME = 'meta.json'
15 53
     SPLITCHAR = ":"
16 54
 
17 55
     def __init__(self, path):
@@ -20,6 +58,8 @@ class base_page(object):
20 58
         else:
21 59
             self._path = os.path.join(settings.PAGES_ROOT, path.replace("/", 2*self.SPLITCHAR))
22 60
         self._raw_page_src = None
61
+        #
62
+        self._meta_data = meta_data(self._meta_filename, self.is_available())
23 63
 
24 64
     def _load_page_src(self):
25 65
         if self._raw_page_src is None:
@@ -38,10 +78,16 @@ class base_page(object):
38 78
         with open(self.filename, 'w') as fh:
39 79
             fh.write(page_txt)
40 80
         update_item(self)
81
+        #
82
+        self._update_metadata()
41 83
 
42 84
     @property
43 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 92
     @property
47 93
     def rel_path(self):
@@ -65,6 +111,17 @@ class base_page(object):
65 111
         self._load_page_src()
66 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 126
 class creole_page(base_page):
70 127
     FOLDER_ATTACHMENTS = "attachments"

Loading…
Cancel
Save