|
@@ -1,69 +1,87 @@
|
1
|
1
|
from django.conf import settings
|
2
|
2
|
|
3
|
3
|
import fstools
|
|
4
|
+import logging
|
4
|
5
|
from pages import messages, url_page
|
5
|
6
|
import mycreole
|
6
|
7
|
import os
|
7
|
8
|
|
|
9
|
+logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
|
8
|
10
|
|
9
|
|
-class creol_page(object):
|
10
|
|
- SPLITCHAR = ":"
|
11
|
|
- FOLDER_ATTACHMENTS = "attachments"
|
|
11
|
+
|
|
12
|
+class base_page(object):
|
12
|
13
|
FOLDER_CONTENT = 'content'
|
13
|
14
|
FILE_NAME = 'page'
|
|
15
|
+ SPLITCHAR = ":"
|
14
|
16
|
|
15
|
|
- def __init__(self, request, rel_path) -> None:
|
16
|
|
- self._rel_path = rel_path
|
17
|
|
- self._request = request
|
|
17
|
+ def __init__(self, path):
|
|
18
|
+ if path.startswith(settings.PAGES_ROOT):
|
|
19
|
+ self._path = path
|
|
20
|
+ else:
|
|
21
|
+ self._path = os.path.join(settings.PAGES_ROOT, path.replace("/", 2*self.SPLITCHAR))
|
|
22
|
+ self._raw_page_src = None
|
18
|
23
|
|
19
|
|
- def rel_path_is_valid(self):
|
20
|
|
- return not self.SPLITCHAR in self._rel_path
|
|
24
|
+ def _load_page_src(self):
|
|
25
|
+ if self._raw_page_src is None:
|
|
26
|
+ try:
|
|
27
|
+ with open(self.filename, 'r') as fh:
|
|
28
|
+ self._raw_page_src = fh.read()
|
|
29
|
+ except FileNotFoundError:
|
|
30
|
+ self._raw_page_src = ""
|
21
|
31
|
|
22
|
|
- def is_available(self):
|
23
|
|
- return os.path.isfile(self.content_file_name)
|
|
32
|
+ def update_page(self, page_txt):
|
|
33
|
+ from .search import update_item
|
|
34
|
+ #
|
|
35
|
+ folder = os.path.dirname(self.filename)
|
|
36
|
+ if not os.path.exists(folder):
|
|
37
|
+ fstools.mkdir(folder)
|
|
38
|
+ with open(self.filename, 'w') as fh:
|
|
39
|
+ fh.write(page_txt)
|
|
40
|
+ update_item(self)
|
24
|
41
|
|
25
|
42
|
@property
|
26
|
|
- def title(self):
|
27
|
|
- return os.path.basename(self._rel_path)
|
|
43
|
+ def filename(self):
|
|
44
|
+ return os.path.join(self._path, self.FOLDER_CONTENT, self.FILE_NAME)
|
28
|
45
|
|
29
|
46
|
@property
|
30
|
|
- def attachment_path(self):
|
31
|
|
- return os.path.join(self.content_folder_name, self.FOLDER_ATTACHMENTS)
|
32
|
|
-
|
33
|
|
- def __content_folder_filter__(self, folder):
|
34
|
|
- return folder.replace('/', '::')
|
|
47
|
+ def rel_path(self):
|
|
48
|
+ return os.path.basename(self._path).replace(2*self.SPLITCHAR, "/")
|
35
|
49
|
|
36
|
|
- def __folder_content_filter__(self, folder):
|
37
|
|
- return folder.replace('::', '/')
|
|
50
|
+ def rel_path_is_valid(self):
|
|
51
|
+ return not self.SPLITCHAR in self.rel_path
|
38
|
52
|
|
39
|
|
- @property
|
40
|
|
- def content_folder_name(self):
|
41
|
|
- return self.__content_folder_filter__(self._rel_path)
|
|
53
|
+ def is_available(self):
|
|
54
|
+ is_a = os.path.isfile(self.filename)
|
|
55
|
+ if not is_a:
|
|
56
|
+ logger.info("page.is_available: Not available - %s", self.filename)
|
|
57
|
+ return is_a
|
42
|
58
|
|
43
|
59
|
@property
|
44
|
|
- def content_file_name(self):
|
45
|
|
- return os.path.join(settings.PAGES_ROOT, self.content_folder_name, self.FOLDER_CONTENT, self.FILE_NAME)
|
|
60
|
+ def title(self):
|
|
61
|
+ return os.path.basename(self._path).split("::")[-1]
|
46
|
62
|
|
47
|
63
|
@property
|
48
|
64
|
def raw_page_src(self):
|
49
|
|
- try:
|
50
|
|
- with open(self.content_file_name, 'r') as fh:
|
51
|
|
- return fh.read()
|
52
|
|
- except FileNotFoundError:
|
53
|
|
- return ""
|
|
65
|
+ self._load_page_src()
|
|
66
|
+ return self._raw_page_src
|
54
|
67
|
|
55
|
|
- def update_page(self, page_txt):
|
56
|
|
- folder = os.path.dirname(self.content_file_name)
|
57
|
|
- if not os.path.exists(folder):
|
58
|
|
- fstools.mkdir(folder)
|
59
|
|
- with open(self.content_file_name, 'w') as fh:
|
60
|
|
- fh.write(page_txt)
|
|
68
|
+
|
|
69
|
+class creole_page(base_page):
|
|
70
|
+ FOLDER_ATTACHMENTS = "attachments"
|
|
71
|
+
|
|
72
|
+ def __init__(self, request, path) -> None:
|
|
73
|
+ self._request = request
|
|
74
|
+ super().__init__(path)
|
|
75
|
+
|
|
76
|
+ @property
|
|
77
|
+ def attachment_path(self):
|
|
78
|
+ return os.path.join(os.path.basename(self._path), self.FOLDER_ATTACHMENTS)
|
61
|
79
|
|
62
|
80
|
def render_to_html(self):
|
63
|
81
|
if self.is_available():
|
64
|
82
|
return self.render_text(self._request, self.raw_page_src)
|
65
|
83
|
else:
|
66
|
|
- messages.unavailable_msg_page(self._request, self._rel_path)
|
|
84
|
+ messages.unavailable_msg_page(self._request, self.rel_path)
|
67
|
85
|
return ""
|
68
|
86
|
|
69
|
87
|
def render_text(self, request, txt):
|
|
@@ -102,18 +120,18 @@ class creol_page(object):
|
102
|
120
|
#
|
103
|
121
|
rv = ""
|
104
|
122
|
# create a rel_path list
|
105
|
|
- pathlist = [self.__folder_content_filter__(os.path.basename(path)) for path in fstools.dirlist(settings.PAGES_ROOT, rekursive=False)]
|
|
123
|
+ pathlist = [base_page(path).rel_path for path in fstools.dirlist(settings.PAGES_ROOT, rekursive=False)]
|
106
|
124
|
# sort basename
|
107
|
125
|
pathlist.sort(key=os.path.basename)
|
108
|
126
|
|
109
|
127
|
last_char = None
|
110
|
128
|
for contentname in pathlist:
|
111
|
129
|
#
|
112
|
|
- if (contentname.startswith(self._rel_path) or allpages) and contentname != self._rel_path:
|
|
130
|
+ if (contentname.startswith(self.rel_path) or allpages) and contentname != self.rel_path:
|
113
|
131
|
if allpages:
|
114
|
132
|
name = contentname
|
115
|
133
|
else:
|
116
|
|
- name = contentname[len(self._rel_path)+1:]
|
|
134
|
+ name = contentname[len(self.rel_path)+1:]
|
117
|
135
|
if name.count('/') < depth and name.startswith(startname):
|
118
|
136
|
if last_char != os.path.basename(name)[0].upper():
|
119
|
137
|
last_char = os.path.basename(name)[0].upper()
|