Browse Source

Page tagging implemented

master
Dirk Alders 3 months ago
parent
commit
d81c2df678
4 changed files with 35 additions and 17 deletions
  1. 4
    2
      pages/forms.py
  2. 25
    12
      pages/page.py
  3. 3
    1
      pages/search.py
  4. 3
    2
      pages/views.py

+ 4
- 2
pages/forms.py View File

6
 
6
 
7
 class EditForm(forms.Form):  # Note that it is not inheriting from forms.ModelForm
7
 class EditForm(forms.Form):  # Note that it is not inheriting from forms.ModelForm
8
     page_txt = forms.CharField(max_length=20000, label="Page source text", widget=forms.Textarea(attrs={"rows": "20"}))
8
     page_txt = forms.CharField(max_length=20000, label="Page source text", widget=forms.Textarea(attrs={"rows": "20"}))
9
+    page_tags = forms.CharField(max_length=20000, label="Tags (words separated by spaces)")
9
 
10
 
10
     def __init__(self, *args, **kwargs) -> None:
11
     def __init__(self, *args, **kwargs) -> None:
11
         page_data = kwargs.pop("page_data")
12
         page_data = kwargs.pop("page_data")
13
+        page_tags = kwargs.pop("page_tags")
12
         super().__init__(*args, **kwargs)
14
         super().__init__(*args, **kwargs)
13
-        page_txt = self.fields['page_txt']
14
-        page_txt.initial = page_data
15
+        self.fields['page_txt'].initial = page_data
16
+        self.fields['page_tags'].initial = page_tags

+ 25
- 12
pages/page.py View File

17
     KEY_CREATION_TIME = "creation_time"
17
     KEY_CREATION_TIME = "creation_time"
18
     KEY_MODIFIED_TIME = "modified_time"
18
     KEY_MODIFIED_TIME = "modified_time"
19
     KEY_MODIFIED_USER = "modified_user"
19
     KEY_MODIFIED_USER = "modified_user"
20
+    KEY_TAGS = "tags"
20
 
21
 
21
     def __init__(self, meta_filename, page_exists):
22
     def __init__(self, meta_filename, page_exists):
22
         self._meta_filename = meta_filename
23
         self._meta_filename = meta_filename
38
         if missing_keys and page_exists:
39
         if missing_keys and page_exists:
39
             self.save()
40
             self.save()
40
 
41
 
41
-    def update(self, username):
42
-        self[self.KEY_MODIFIED_TIME] = int(time.time())
43
-        self[self.KEY_MODIFIED_USER] = username
42
+    def update(self, username, tags):
43
+        if username:
44
+            self[self.KEY_MODIFIED_TIME] = int(time.time())
45
+            self[self.KEY_MODIFIED_USER] = username
46
+        if tags:
47
+            self[self.KEY_TAGS] = tags
44
         #
48
         #
45
-        self.save()
49
+        if username or tags:
50
+            self.save()
46
 
51
 
47
     def save(self):
52
     def save(self):
48
         with open(self._meta_filename, 'w') as fh:
53
         with open(self._meta_filename, 'w') as fh:
87
         shutil.copy(self.filename, os.path.join(history_folder, "%05d_%s" % (hist_number, self.PAGE_FILE_NAME)))
92
         shutil.copy(self.filename, os.path.join(history_folder, "%05d_%s" % (hist_number, self.PAGE_FILE_NAME)))
88
         shutil.copy(self._meta_filename, os.path.join(history_folder, "%05d_%s" % (hist_number, self.META_FILE_NAME)))
93
         shutil.copy(self._meta_filename, os.path.join(history_folder, "%05d_%s" % (hist_number, self.META_FILE_NAME)))
89
 
94
 
90
-    def update_page(self, page_txt):
95
+    def update_page(self, page_txt, tags):
91
         from .search import update_item
96
         from .search import update_item
92
         if page_txt.replace("\r\n", "\n") != self.raw_page_src:
97
         if page_txt.replace("\r\n", "\n") != self.raw_page_src:
93
             # Store page history
98
             # Store page history
97
             fstools.mkdir(os.path.dirname(self.filename))
102
             fstools.mkdir(os.path.dirname(self.filename))
98
             with open(self.filename, 'w') as fh:
103
             with open(self.filename, 'w') as fh:
99
                 fh.write(page_txt)
104
                 fh.write(page_txt)
100
-            # update search index
101
-            update_item(self)
102
             # update metadata
105
             # update metadata
103
-            self._update_metadata()
104
-            return True
105
-        return False
106
+            page_changed = True
107
+        else:
108
+            page_changed = False
109
+        self._update_metadata(tags)
110
+        # update search index
111
+        update_item(self)
112
+        return page_changed
106
 
113
 
107
     @property
114
     @property
108
     def filename(self):
115
     def filename(self):
134
         self._load_page_src()
141
         self._load_page_src()
135
         return self._raw_page_src
142
         return self._raw_page_src
136
 
143
 
137
-    def _update_metadata(self):
144
+    def _update_metadata(self, tags):
138
         username = None
145
         username = None
139
         try:
146
         try:
140
             if self._request.user.is_authenticated:
147
             if self._request.user.is_authenticated:
143
                 logger.warning("Page edit without having a logged in user. This is not recommended. Check your access definitions!")
150
                 logger.warning("Page edit without having a logged in user. This is not recommended. Check your access definitions!")
144
         except AttributeError:
151
         except AttributeError:
145
             logger.exception("Page edit without having a request object. Check programming!")
152
             logger.exception("Page edit without having a request object. Check programming!")
146
-        self._meta_data.update(username)
153
+        self._meta_data.update(username, tags)
154
+
155
+    @property
156
+    def page_tags(self):
157
+        return self._meta_data.get(self._meta_data.KEY_TAGS)
147
 
158
 
148
 
159
 
149
 class creole_page(base_page):
160
 class creole_page(base_page):
168
         ctime = timestamp_to_datetime(self._request, self._meta_data.get(self._meta_data.KEY_CREATION_TIME)).strftime('%Y-%m-%d %H:%M')
179
         ctime = timestamp_to_datetime(self._request, self._meta_data.get(self._meta_data.KEY_CREATION_TIME)).strftime('%Y-%m-%d %H:%M')
169
         mtime = timestamp_to_datetime(self._request, self._meta_data.get(self._meta_data.KEY_MODIFIED_TIME)).strftime('%Y-%m-%d %H:%M')
180
         mtime = timestamp_to_datetime(self._request, self._meta_data.get(self._meta_data.KEY_MODIFIED_TIME)).strftime('%Y-%m-%d %H:%M')
170
         user = self._meta_data.get(self._meta_data.KEY_MODIFIED_USER)
181
         user = self._meta_data.get(self._meta_data.KEY_MODIFIED_USER)
182
+        tags = self._meta_data.get(self._meta_data.KEY_TAGS, "-")
171
         #
183
         #
172
         meta = f'|{_("Created")}:|{ctime}|\n'
184
         meta = f'|{_("Created")}:|{ctime}|\n'
173
         meta += f'|{_("Modified")}:|{mtime}|\n'
185
         meta += f'|{_("Modified")}:|{mtime}|\n'
174
         meta += f'|{_("Editor")}|{user}|\n'
186
         meta += f'|{_("Editor")}|{user}|\n'
187
+        meta += f'|{_("Tags")}|{tags}|\n'
175
         meta += f'=== {_("Page content")}\n'
188
         meta += f'=== {_("Page content")}\n'
176
         meta += '{{{\n%s\n}}}\n' % self.raw_page_src
189
         meta += '{{{\n%s\n}}}\n' % self.raw_page_src
177
         #
190
         #

+ 3
- 1
pages/search.py View File

18
     # Page
18
     # Page
19
     title=TEXT,
19
     title=TEXT,
20
     page_src=TEXT,
20
     page_src=TEXT,
21
+    page_tags=TEXT,
21
     # metadata
22
     # metadata
22
     creation_time=DATETIME,
23
     creation_time=DATETIME,
23
     modified_time=DATETIME,
24
     modified_time=DATETIME,
63
         #
64
         #
64
         title=bp.title,
65
         title=bp.title,
65
         page_src=bp.raw_page_src,
66
         page_src=bp.raw_page_src,
67
+        page_tags=bp.page_tags,
66
         #
68
         #
67
         creation_time=datetime.fromtimestamp(bp._meta_data.get(bp._meta_data.KEY_CREATION_TIME)),
69
         creation_time=datetime.fromtimestamp(bp._meta_data.get(bp._meta_data.KEY_CREATION_TIME)),
68
         modified_time=datetime.fromtimestamp(bp._meta_data.get(bp._meta_data.KEY_MODIFIED_TIME)),
70
         modified_time=datetime.fromtimestamp(bp._meta_data.get(bp._meta_data.KEY_MODIFIED_TIME)),
77
 
79
 
78
 def whoosh_search(search_txt):
80
 def whoosh_search(search_txt):
79
     ix = load_index()
81
     ix = load_index()
80
-    qp = qparser.MultifieldParser(['title', 'page_src'], ix.schema)
82
+    qp = qparser.MultifieldParser(['title', 'page_src', 'page_tags'], ix.schema)
81
     qp.add_plugin(DateParserPlugin(free=True))
83
     qp.add_plugin(DateParserPlugin(free=True))
82
     try:
84
     try:
83
         q = qp.parse(search_txt)
85
         q = qp.parse(search_txt)

+ 3
- 2
pages/views.py View File

59
         p = creole_page(request, rel_path)
59
         p = creole_page(request, rel_path)
60
         #
60
         #
61
         if not request.POST:
61
         if not request.POST:
62
-            form = EditForm(page_data=p.raw_page_src)
62
+            form = EditForm(page_data=p.raw_page_src, page_tags=p.page_tags)
63
             #
63
             #
64
             context_adaption(
64
             context_adaption(
65
                 context,
65
                 context,
73
         else:
73
         else:
74
             save = request.POST.get("save")
74
             save = request.POST.get("save")
75
             page_txt = request.POST.get("page_txt")
75
             page_txt = request.POST.get("page_txt")
76
+            tags = request.POST.get("page_tags")
76
             preview = request.POST.get("preview")
77
             preview = request.POST.get("preview")
77
             #
78
             #
78
             if save is not None:
79
             if save is not None:
79
-                if p.update_page(page_txt):
80
+                if p.update_page(page_txt, tags):
80
                     messages.edit_success(request)
81
                     messages.edit_success(request)
81
                 else:
82
                 else:
82
                     messages.edit_no_change(request)
83
                     messages.edit_no_change(request)

Loading…
Cancel
Save