Piki is a minimal wiki
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

page.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. from django.conf import settings
  2. from django.utils.translation import gettext as _
  3. import fstools
  4. import json
  5. import logging
  6. from pages import messages, url_page
  7. import mycreole
  8. import os
  9. import shutil
  10. import time
  11. from . import timestamp_to_datetime
  12. logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
  13. class meta_data(dict):
  14. KEY_CREATION_TIME = "creation_time"
  15. KEY_MODIFIED_TIME = "modified_time"
  16. KEY_MODIFIED_USER = "modified_user"
  17. KEY_TAGS = "tags"
  18. def __init__(self, meta_filename, page_exists):
  19. self._meta_filename = meta_filename
  20. # Load data from disk
  21. try:
  22. with open(meta_filename, 'r') as fh:
  23. super().__init__(json.load(fh))
  24. except (FileNotFoundError, json.decoder.JSONDecodeError) as e:
  25. super().__init__()
  26. # Add missing information to meta_data
  27. missing_keys = False
  28. if self.KEY_CREATION_TIME not in self:
  29. missing_keys = True
  30. self[self.KEY_CREATION_TIME] = int(time.time())
  31. if self.KEY_MODIFIED_TIME not in self:
  32. self[self.KEY_MODIFIED_TIME] = self[self.KEY_CREATION_TIME]
  33. if missing_keys and page_exists:
  34. self.save()
  35. def update(self, username, tags):
  36. if username:
  37. self[self.KEY_MODIFIED_TIME] = int(time.time())
  38. self[self.KEY_MODIFIED_USER] = username
  39. if tags:
  40. self[self.KEY_TAGS] = tags
  41. #
  42. if username or tags:
  43. self.save()
  44. def save(self):
  45. with open(self._meta_filename, 'w') as fh:
  46. json.dump(self, fh, indent=4)
  47. class base_page(object):
  48. PAGE_FILE_NAME = 'page'
  49. META_FILE_NAME = 'meta.json'
  50. HISTORY_FOLDER_NAME = 'history'
  51. SPLITCHAR = ":"
  52. def __init__(self, path, history_version=None):
  53. self._history_version = history_version
  54. #
  55. if path.startswith(settings.PAGES_ROOT):
  56. self._path = path
  57. else:
  58. self._path = os.path.join(settings.PAGES_ROOT, path.replace("/", 2*self.SPLITCHAR))
  59. self._raw_page_src = None
  60. #
  61. self._meta_data = meta_data(self._meta_filename, self.is_available())
  62. @property
  63. def modified_time(self):
  64. return self._meta_data.get(self._meta_data.KEY_MODIFIED_TIME)
  65. def _load_page_src(self):
  66. if self._raw_page_src is None:
  67. try:
  68. with open(self.filename, 'r') as fh:
  69. self._raw_page_src = fh.read()
  70. except FileNotFoundError:
  71. self._raw_page_src = ""
  72. def history_numbers_list(self):
  73. history_folder = os.path.join(self._path, self.HISTORY_FOLDER_NAME)
  74. fstools.mkdir(history_folder)
  75. # identify last_history number
  76. return list(set([int(os.path.basename(filename)[:5]) for filename in fstools.filelist(history_folder)]))
  77. def _store_history(self):
  78. try:
  79. hist_number = max(self.history_numbers_list()) + 1
  80. except ValueError:
  81. hist_number = 1 # no history yet
  82. # copy file to history folder
  83. shutil.copy(self.filename, self.history_filename(hist_number))
  84. shutil.copy(self._meta_filename, self._history_meta_filename(hist_number))
  85. def update_page(self, page_txt, 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. from .search import update_item
  91. if page_txt.replace("\r\n", "\n") != self.raw_page_src:
  92. # Store page history
  93. if self.raw_page_src:
  94. self._store_history()
  95. # save the new page content
  96. fstools.mkdir(os.path.dirname(self.filename))
  97. with open(self.filename, 'w') as fh:
  98. fh.write(page_txt)
  99. # update metadata
  100. page_changed = True
  101. else:
  102. page_changed = False
  103. self._update_metadata(tags)
  104. # update search index
  105. update_item(self)
  106. return page_changed
  107. @property
  108. def filename(self):
  109. if not self._history_version:
  110. return os.path.join(self._path, self.PAGE_FILE_NAME)
  111. else:
  112. return self.history_filename(self._history_version)
  113. def history_filename(self, history_version):
  114. return os.path.join(self._path, self.HISTORY_FOLDER_NAME, "%05d_%s" % (history_version, self.PAGE_FILE_NAME))
  115. @property
  116. def _meta_filename(self):
  117. if not self._history_version:
  118. return os.path.join(self._path, self.META_FILE_NAME)
  119. else:
  120. return self._history_meta_filename(self._history_version)
  121. def _history_meta_filename(self, history_version):
  122. return os.path.join(self._path, self.HISTORY_FOLDER_NAME, "%05d_%s" % (history_version, self.META_FILE_NAME))
  123. @property
  124. def rel_path(self):
  125. return os.path.basename(self._path).replace(2*self.SPLITCHAR, "/")
  126. def rel_path_is_valid(self):
  127. return not self.SPLITCHAR in self.rel_path
  128. def is_available(self):
  129. is_a = os.path.isfile(self.filename)
  130. if not is_a:
  131. logger.info("page.is_available: Not available - %s", self.filename)
  132. return is_a
  133. @property
  134. def title(self):
  135. return os.path.basename(self._path).split("::")[-1]
  136. @property
  137. def raw_page_src(self):
  138. self._load_page_src()
  139. return self._raw_page_src
  140. def _update_metadata(self, tags):
  141. username = None
  142. try:
  143. if self._request.user.is_authenticated:
  144. username = self._request.user.username
  145. else:
  146. logger.warning("Page edit without having a logged in user. This is not recommended. Check your access definitions!")
  147. except AttributeError:
  148. logger.exception("Page edit without having a request object. Check programming!")
  149. self._meta_data.update(username, tags)
  150. @property
  151. def page_tags(self):
  152. return self._meta_data.get(self._meta_data.KEY_TAGS)
  153. class creole_page(base_page):
  154. FOLDER_ATTACHMENTS = "attachments"
  155. def __init__(self, request, path, history_version=None) -> None:
  156. self._request = request
  157. super().__init__(path, history_version=history_version)
  158. @property
  159. def attachment_path(self):
  160. return os.path.join(os.path.basename(self._path), self.FOLDER_ATTACHMENTS)
  161. def render_to_html(self):
  162. if self.is_available():
  163. return self.render_text(self._request, self.raw_page_src)
  164. else:
  165. messages.unavailable_msg_page(self._request, self.rel_path)
  166. return ""
  167. def render_meta(self):
  168. 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')
  170. user = self._meta_data.get(self._meta_data.KEY_MODIFIED_USER)
  171. tags = self._meta_data.get(self._meta_data.KEY_TAGS, "-")
  172. #
  173. meta = f'=== {_("Meta data")}\n'
  174. meta += f'|{_("Created")}:|{ctime}|\n'
  175. meta += f'|{_("Modified")}:|{mtime}|\n'
  176. meta += f'|{_("Editor")}|{user}|\n'
  177. meta += f'|{_("Tags")}|{tags}|\n'
  178. #
  179. hnl = self.history_numbers_list()
  180. if hnl:
  181. meta += f'=== {_("History")}\n'
  182. meta += f'| ={_("Version")} | ={_("Date")} | ={_("Page")} | ={_("Meta data")} | \n'
  183. # Current
  184. name = _("Current")
  185. meta += f"| {name} \
  186. | {timestamp_to_datetime(self._request, self.modified_time)} \
  187. | [[{url_page(self._request, self.rel_path)} | Page]] \
  188. | [[{url_page(self._request, self.rel_path, meta=None)} | Meta]]\n"
  189. # History
  190. for num in reversed(hnl):
  191. p = creole_page(self._request, self._path, history_version=num)
  192. meta += f"| {num} \
  193. | {timestamp_to_datetime(self._request, p.modified_time)} \
  194. | [[{url_page(self._request, p.rel_path, history=num)} | Page]] \
  195. | [[{url_page(self._request, p.rel_path, meta=None, history=num)} | Meta]]\n"
  196. #
  197. meta += f'=== {_("Page content")}\n'
  198. if not self._history_version:
  199. meta += '{{{\n%s\n}}}\n' % self.raw_page_src
  200. else:
  201. c = creole_page(self._request, self.rel_path)
  202. meta += "| =Current | =This |\n"
  203. left_lines = c.raw_page_src.splitlines()
  204. right_lines = self.raw_page_src.splitlines()
  205. while len(left_lines) + len(right_lines) > 0:
  206. try:
  207. left = left_lines.pop(0)
  208. except IndexError:
  209. left = ""
  210. try:
  211. right = right_lines.pop(0)
  212. except IndexError:
  213. right = ""
  214. if left == right:
  215. meta += "| {{{ %s }}} | {{{ %s }}} |\n" % (left, right)
  216. else:
  217. meta += "| **{{{ %s }}}** | **{{{ %s }}}** |\n" % (left, right)
  218. #
  219. return mycreole.render_simple(meta)
  220. def render_text(self, request, txt):
  221. macros = {
  222. "subpages": self.macro_subpages,
  223. "allpages": self.macro_allpages,
  224. }
  225. return mycreole.render(request, txt, self.attachment_path, macros=macros)
  226. def macro_allpages(self, *args, **kwargs):
  227. kwargs["allpages"] = True
  228. return self.macro_subpages(*args, **kwargs)
  229. def macro_subpages(self, *args, **kwargs):
  230. allpages = kwargs.pop("allpages", False)
  231. #
  232. def parse_depth(s: str):
  233. try:
  234. return int(s)
  235. except ValueError:
  236. pass
  237. params = kwargs.get('', '')
  238. filter_str = ''
  239. depth = parse_depth(params)
  240. if depth is None:
  241. params = params.split(",")
  242. depth = parse_depth(params[0])
  243. if len(params) == 2:
  244. filter_str = params[1]
  245. elif depth is None:
  246. filter_str = params[0]
  247. #
  248. rv = ""
  249. # create a page_list
  250. if allpages:
  251. expression = "*"
  252. parent_rel_path = ""
  253. else:
  254. expression = os.path.basename(self._path) + 2 * self.SPLITCHAR + "*"
  255. parent_rel_path = self.rel_path
  256. pl = page_list(
  257. self._request,
  258. [creole_page(self._request, path) for path in fstools.dirlist(settings.PAGES_ROOT, expression=expression, rekursive=False)]
  259. )
  260. return pl.html_list(depth=depth, filter_str=filter_str, parent_rel_path=parent_rel_path)
  261. class page_list(list):
  262. def __init__(self, request, *args, **kwargs):
  263. self._request = request
  264. return super().__init__(*args, **kwargs)
  265. def sort_basename(self):
  266. return list.sort(self, key=lambda x: os.path.basename(x.rel_path))
  267. def creole_list(self, depth=None, filter_str='', parent_rel_path=''):
  268. self.sort_basename()
  269. depth = depth or 9999 # set a random high value if None
  270. #
  271. parent_rel_path = parent_rel_path + "/" if len(parent_rel_path) > 0 else ""
  272. #
  273. rv = ""
  274. last_char = None
  275. for page in self:
  276. name = page.rel_path[len(parent_rel_path):]
  277. if name.startswith(filter_str) and name != filter_str:
  278. if name.count('/') < depth:
  279. first_char = os.path.basename(name)[0].upper()
  280. if last_char != first_char:
  281. last_char = first_char
  282. rv += f"=== {first_char}\n"
  283. rv += f"* [[{url_page(self._request, page.rel_path)} | {name} ]]\n"
  284. return rv
  285. def html_list(self, depth=9999, filter_str='', parent_rel_path=''):
  286. return mycreole.render_simple(self.creole_list(depth, filter_str, parent_rel_path))