|
@@ -1,347 +0,0 @@
|
1
|
|
-import difflib
|
2
|
|
-from django.conf import settings
|
3
|
|
-from django.utils.translation import gettext as _
|
4
|
|
-import fstools
|
5
|
|
-import json
|
6
|
|
-import logging
|
7
|
|
-from pages import messages, url_page
|
8
|
|
-import mycreole
|
9
|
|
-import os
|
10
|
|
-import shutil
|
11
|
|
-import time
|
12
|
|
-from . import timestamp_to_datetime
|
13
|
|
-
|
14
|
|
-logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
|
15
|
|
-
|
16
|
|
-
|
17
|
|
-SPLITCHAR = ":"
|
18
|
|
-HISTORY_FOLDER_NAME = 'history'
|
19
|
|
-
|
20
|
|
-
|
21
|
|
-def full_path_all_pages(expression="*"):
|
22
|
|
- system_pages = fstools.dirlist(settings.SYSTEM_PAGES_ROOT, expression=expression, rekursive=False)
|
23
|
|
- system_pages = [os.path.join(settings.PAGES_ROOT, os.path.basename(path)) for path in system_pages]
|
24
|
|
- pages = fstools.dirlist(settings.PAGES_ROOT, expression=expression, rekursive=False)
|
25
|
|
- rv = []
|
26
|
|
- for path in set(system_pages + pages):
|
27
|
|
- p = page_wrapped(None, path)
|
28
|
|
- if p.is_available():
|
29
|
|
- rv.append(path)
|
30
|
|
- return rv
|
31
|
|
-
|
32
|
|
-
|
33
|
|
-class base(dict):
|
34
|
|
- @property
|
35
|
|
- def rel_path(self):
|
36
|
|
- return os.path.basename(self._path).replace(2*SPLITCHAR, "/")
|
37
|
|
-
|
38
|
|
- def is_available(self):
|
39
|
|
- is_a = os.path.isfile(self.filename)
|
40
|
|
- if not is_a:
|
41
|
|
- logger.debug("Not available - %s", self.filename)
|
42
|
|
- return is_a
|
43
|
|
-
|
44
|
|
- def history_numbers_list(self):
|
45
|
|
- history_folder = os.path.join(self._path, HISTORY_FOLDER_NAME)
|
46
|
|
- return list(set([int(os.path.basename(filename)[:5]) for filename in fstools.filelist(history_folder)]))
|
47
|
|
-
|
48
|
|
-
|
49
|
|
-class meta_data(base):
|
50
|
|
- META_FILE_NAME = 'meta.json'
|
51
|
|
- #
|
52
|
|
- KEY_CREATION_TIME = "creation_time"
|
53
|
|
- KEY_CREATION_USER = "creation_user"
|
54
|
|
- KEY_MODIFIED_TIME = "modified_time"
|
55
|
|
- KEY_MODIFIED_USER = "modified_user"
|
56
|
|
- KEY_TAGS = "tags"
|
57
|
|
-
|
58
|
|
- def __init__(self, path, history_version=None):
|
59
|
|
- self._path = path
|
60
|
|
- self._history_version = history_version
|
61
|
|
- #
|
62
|
|
- # Load data from disk
|
63
|
|
- try:
|
64
|
|
- with open(self.filename, 'r') as fh:
|
65
|
|
- super().__init__(json.load(fh))
|
66
|
|
- except (FileNotFoundError, json.decoder.JSONDecodeError) as e:
|
67
|
|
- super().__init__()
|
68
|
|
-
|
69
|
|
- def delete(self):
|
70
|
|
- os.remove(self.filename)
|
71
|
|
-
|
72
|
|
- @property
|
73
|
|
- def filename(self):
|
74
|
|
- if not self._history_version:
|
75
|
|
- return os.path.join(self._path, self.META_FILE_NAME)
|
76
|
|
- else:
|
77
|
|
- return self.history_filename(self._history_version)
|
78
|
|
-
|
79
|
|
- def history_filename(self, history_version):
|
80
|
|
- return os.path.join(self._path, HISTORY_FOLDER_NAME, "%05d_%s" % (history_version, self.META_FILE_NAME))
|
81
|
|
-
|
82
|
|
- def update_required(self, tags):
|
83
|
|
- return tags != self.get(self.KEY_TAGS)
|
84
|
|
-
|
85
|
|
- def update(self, username, tags):
|
86
|
|
- if self._history_version:
|
87
|
|
- logger.error("A history version %05d can not be updated!", self._history_version)
|
88
|
|
- return False
|
89
|
|
- else:
|
90
|
|
- if username:
|
91
|
|
- self[self.KEY_MODIFIED_TIME] = int(time.time())
|
92
|
|
- self[self.KEY_MODIFIED_USER] = username
|
93
|
|
- #
|
94
|
|
- if self.KEY_CREATION_USER not in self:
|
95
|
|
- self[self.KEY_CREATION_USER] = self[self.KEY_MODIFIED_USER]
|
96
|
|
- if self.KEY_CREATION_TIME not in self:
|
97
|
|
- self[self.KEY_CREATION_TIME] = self[self.KEY_MODIFIED_TIME]
|
98
|
|
- if tags:
|
99
|
|
- self[self.KEY_TAGS] = tags
|
100
|
|
- #
|
101
|
|
- if username or tags:
|
102
|
|
- self.save()
|
103
|
|
- return True
|
104
|
|
-
|
105
|
|
- def save(self):
|
106
|
|
- if self._history_version:
|
107
|
|
- logger.error("A history version %05d can not be updated!", self._history_version)
|
108
|
|
- return False
|
109
|
|
- else:
|
110
|
|
- with open(self.filename, 'w') as fh:
|
111
|
|
- json.dump(self, fh, indent=4)
|
112
|
|
- return True
|
113
|
|
-
|
114
|
|
- def store_to_history(self, history_number):
|
115
|
|
- history_filename = self.history_filename(history_number)
|
116
|
|
- fstools.mkdir(os.path.dirname(history_filename))
|
117
|
|
- shutil.copy(self.filename, history_filename)
|
118
|
|
-
|
119
|
|
-
|
120
|
|
-class page_data(base):
|
121
|
|
- PAGE_FILE_NAME = 'page'
|
122
|
|
-
|
123
|
|
- def __init__(self, path, history_version=None):
|
124
|
|
- self._history_version = history_version
|
125
|
|
- self._path = path
|
126
|
|
- self._raw_page_src = None
|
127
|
|
-
|
128
|
|
- def _load_page_src(self):
|
129
|
|
- if self._raw_page_src is None:
|
130
|
|
- try:
|
131
|
|
- with open(self.filename, 'r') as fh:
|
132
|
|
- self._raw_page_src = fh.read()
|
133
|
|
- except FileNotFoundError:
|
134
|
|
- self._raw_page_src = ""
|
135
|
|
-
|
136
|
|
- def delete(self):
|
137
|
|
- os.remove(self.filename)
|
138
|
|
-
|
139
|
|
- def rename(self, page_name):
|
140
|
|
- # Change backslash to slash and remove double slashes
|
141
|
|
- page_name = page_name.replace("\\", "/")
|
142
|
|
- while "//" in page_name:
|
143
|
|
- page_name = page_name.replace("//", "/")
|
144
|
|
- # move path
|
145
|
|
- target_path = os.path.join(settings.PAGES_ROOT, page_name.replace("/", 2*SPLITCHAR))
|
146
|
|
- shutil.move(self._path, target_path)
|
147
|
|
- # set my path
|
148
|
|
- self._path = target_path
|
149
|
|
-
|
150
|
|
- def update_required(self, page_txt):
|
151
|
|
- return page_txt.replace("\r\n", "\n") != self.raw_page_src
|
152
|
|
-
|
153
|
|
- def update_page(self, page_txt):
|
154
|
|
- if self._history_version:
|
155
|
|
- logger.error("A history version %05d can not be updated!", self._history_version)
|
156
|
|
- return False
|
157
|
|
- else:
|
158
|
|
- # save the new page content
|
159
|
|
- fstools.mkdir(os.path.dirname(self.filename))
|
160
|
|
- with open(self.filename, 'w') as fh:
|
161
|
|
- fh.write(page_txt)
|
162
|
|
- self._raw_page_src = page_txt
|
163
|
|
- return True
|
164
|
|
-
|
165
|
|
- @property
|
166
|
|
- def filename(self):
|
167
|
|
- if not self._history_version:
|
168
|
|
- return os.path.join(self._path, self.PAGE_FILE_NAME)
|
169
|
|
- else:
|
170
|
|
- return self.history_filename(self._history_version)
|
171
|
|
-
|
172
|
|
- def history_filename(self, history_version):
|
173
|
|
- return os.path.join(self._path, HISTORY_FOLDER_NAME, "%05d_%s" % (history_version, self.PAGE_FILE_NAME))
|
174
|
|
-
|
175
|
|
- @property
|
176
|
|
- def rel_path(self):
|
177
|
|
- return os.path.basename(self._path).replace(2*SPLITCHAR, "/")
|
178
|
|
-
|
179
|
|
- @property
|
180
|
|
- def title(self):
|
181
|
|
- return os.path.basename(self._path).split(2*SPLITCHAR)[-1]
|
182
|
|
-
|
183
|
|
- @property
|
184
|
|
- def raw_page_src(self):
|
185
|
|
- self._load_page_src()
|
186
|
|
- return self._raw_page_src
|
187
|
|
-
|
188
|
|
- def store_to_history(self, history_number):
|
189
|
|
- history_filename = self.history_filename(history_number)
|
190
|
|
- fstools.mkdir(os.path.dirname(history_filename))
|
191
|
|
- shutil.copy(self.filename, history_filename)
|
192
|
|
-
|
193
|
|
-
|
194
|
|
-class page_wrapped(object):
|
195
|
|
- """
|
196
|
|
- This class holds different page and meta instances and decides which will be used in which case.
|
197
|
|
- """
|
198
|
|
-
|
199
|
|
- def __init__(self, request, path, history_version=None):
|
200
|
|
- """_summary_
|
201
|
|
-
|
202
|
|
- Args:
|
203
|
|
- request (_type_): The django request or None (if None, the page functionality is limited)
|
204
|
|
- path (_type_): A rel_path of the django page or the filesystem path to the page
|
205
|
|
- history_version (_type_, optional): The history version of the page to be created
|
206
|
|
- """
|
207
|
|
- self._request = request
|
208
|
|
- #
|
209
|
|
- page_path = self.__page_path__(path)
|
210
|
|
- # Page
|
211
|
|
- self._page = page_data(page_path, history_version=history_version)
|
212
|
|
- self._page_meta = meta_data(page_path, history_version=history_version)
|
213
|
|
-
|
214
|
|
- def __page_path__(self, path):
|
215
|
|
- if path.startswith(settings.PAGES_ROOT):
|
216
|
|
- # must be a filesystem path
|
217
|
|
- return path
|
218
|
|
- else:
|
219
|
|
- # must be a relative url
|
220
|
|
- return os.path.join(settings.PAGES_ROOT, path.replace("/", 2*SPLITCHAR))
|
221
|
|
-
|
222
|
|
- def __page_choose__(self):
|
223
|
|
- return self._page
|
224
|
|
-
|
225
|
|
- def __meta_choose__(self):
|
226
|
|
- return self._page_meta
|
227
|
|
-
|
228
|
|
- def __store_history__(self):
|
229
|
|
- if self._page.is_available():
|
230
|
|
- try:
|
231
|
|
- history_number = max(self._page.history_numbers_list()) + 1
|
232
|
|
- except ValueError:
|
233
|
|
- history_number = 1 # no history yet
|
234
|
|
- self._page.store_to_history(history_number)
|
235
|
|
- self._page_meta.store_to_history(history_number)
|
236
|
|
-
|
237
|
|
- #
|
238
|
|
- # meta_data
|
239
|
|
- #
|
240
|
|
- @property
|
241
|
|
- def creation_time(self):
|
242
|
|
- meta = self.__meta_choose__()
|
243
|
|
- rv = meta.get(meta.KEY_CREATION_TIME)
|
244
|
|
- return rv
|
245
|
|
-
|
246
|
|
- @property
|
247
|
|
- def creation_user(self):
|
248
|
|
- meta = self.__meta_choose__()
|
249
|
|
- rv = meta.get(meta.KEY_CREATION_USER)
|
250
|
|
- return rv
|
251
|
|
-
|
252
|
|
- def delete(self):
|
253
|
|
- self.__store_history__()
|
254
|
|
- self._page.delete()
|
255
|
|
- self._page_meta.delete()
|
256
|
|
-
|
257
|
|
- @property
|
258
|
|
- def modified_time(self):
|
259
|
|
- meta = self.__meta_choose__()
|
260
|
|
- rv = meta.get(meta.KEY_MODIFIED_TIME)
|
261
|
|
- return rv
|
262
|
|
-
|
263
|
|
- @property
|
264
|
|
- def modified_user(self):
|
265
|
|
- meta = self.__meta_choose__()
|
266
|
|
- rv = meta.get(meta.KEY_MODIFIED_USER)
|
267
|
|
- return rv
|
268
|
|
-
|
269
|
|
- def rename(self, page_name):
|
270
|
|
- self._page.rename(page_name)
|
271
|
|
-
|
272
|
|
- @property
|
273
|
|
- def tags(self):
|
274
|
|
- meta = self.__meta_choose__()
|
275
|
|
- rv = meta.get(meta.KEY_TAGS)
|
276
|
|
- return rv
|
277
|
|
-
|
278
|
|
- #
|
279
|
|
- # page
|
280
|
|
- #
|
281
|
|
- @property
|
282
|
|
- def attachment_path(self):
|
283
|
|
- page = self.__page_choose__()
|
284
|
|
- rv = page.attachment_path
|
285
|
|
- return rv
|
286
|
|
-
|
287
|
|
- def is_available(self):
|
288
|
|
- return self._page.is_available()
|
289
|
|
-
|
290
|
|
- def userpage_is_available(self):
|
291
|
|
- return self._page.is_available()
|
292
|
|
-
|
293
|
|
- @property
|
294
|
|
- def raw_page_src(self):
|
295
|
|
- page = self.__page_choose__()
|
296
|
|
- rv = page.raw_page_src
|
297
|
|
- return rv
|
298
|
|
-
|
299
|
|
- @property
|
300
|
|
- def rel_path(self):
|
301
|
|
- page = self.__page_choose__()
|
302
|
|
- rv = page.rel_path
|
303
|
|
- return rv
|
304
|
|
-
|
305
|
|
- def render_meta(self):
|
306
|
|
- page = self.__page_choose__()
|
307
|
|
- rv = page.render_meta(self.creation_time, self.modified_time, self.creation_user, self.modified_user, self.tags)
|
308
|
|
- return rv
|
309
|
|
-
|
310
|
|
- def render_to_html(self):
|
311
|
|
- page = self.__page_choose__()
|
312
|
|
- rv = page.render_to_html()
|
313
|
|
- return rv
|
314
|
|
-
|
315
|
|
- def render_text(self, request, txt):
|
316
|
|
- page = self.__page_choose__()
|
317
|
|
- rv = page.render_text(request, txt)
|
318
|
|
- return rv
|
319
|
|
-
|
320
|
|
- @property
|
321
|
|
- def title(self):
|
322
|
|
- page = self.__page_choose__()
|
323
|
|
- rv = page.title
|
324
|
|
- return rv
|
325
|
|
-
|
326
|
|
- def update_page(self, txt, tags):
|
327
|
|
- if self._page.update_required(txt) or self._page_meta.update_required(tags):
|
328
|
|
- rv = False
|
329
|
|
- # Store history
|
330
|
|
- self.__store_history__()
|
331
|
|
- username = None
|
332
|
|
- if self._page.update_required(txt):
|
333
|
|
- # Update page
|
334
|
|
- rv |= self._page.update_page(txt)
|
335
|
|
- # Identify username, to update meta
|
336
|
|
- try:
|
337
|
|
- if self._request.user.is_authenticated:
|
338
|
|
- username = self._request.user.username
|
339
|
|
- else:
|
340
|
|
- logger.warning("Page edit without having a logged in user. This is not recommended. Check your access definitions!")
|
341
|
|
- except AttributeError:
|
342
|
|
- logger.exception("Page edit without having a request object. Check programming!")
|
343
|
|
- rv |= self._page_meta.update(username, tags)
|
344
|
|
- # Update search index
|
345
|
|
- from pages.search import update_item
|
346
|
|
- update_item(self)
|
347
|
|
- return rv
|