Piki is a minimal wiki
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

page.py 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from django.conf import settings
  2. import fstools
  3. from pages import messages
  4. import mycreole
  5. import os
  6. class creol_page(object):
  7. SPLITCHAR = ":"
  8. FOLDER_ATTACHMENTS = "attachments"
  9. FOLDER_CONTENT = 'content'
  10. FILE_NAME = 'page'
  11. def __init__(self, rel_path) -> None:
  12. self._rel_path = rel_path
  13. def rel_path_is_valid(self):
  14. return not self.SPLITCHAR in self._rel_path
  15. def is_available(self):
  16. return os.path.isfile(self.content_file_name)
  17. @property
  18. def title(self):
  19. return os.path.basename(self._rel_path)
  20. @property
  21. def attachment_path(self):
  22. return os.path.join(self.content_folder_name, self.FOLDER_ATTACHMENTS)
  23. @property
  24. def content_folder_name(self):
  25. return self._rel_path.replace('/', '::')
  26. @property
  27. def content_file_name(self):
  28. return os.path.join(settings.PAGES_ROOT, self.content_folder_name, self.FOLDER_CONTENT, self.FILE_NAME)
  29. @property
  30. def raw_page_src(self):
  31. try:
  32. with open(self.content_file_name, 'r') as fh:
  33. return fh.read()
  34. except FileNotFoundError:
  35. return ""
  36. def update_page(self, page_txt):
  37. folder = os.path.dirname(self.content_file_name)
  38. if not os.path.exists(folder):
  39. fstools.mkdir(folder)
  40. with open(self.content_file_name, 'w') as fh:
  41. fh.write(page_txt)
  42. def render_to_html(self, request):
  43. if self.is_available():
  44. return mycreole.render(request, self.raw_page_src, self.attachment_path, "next_anchor")
  45. else:
  46. messages.unavailable_msg_page(request, self._rel_path)
  47. return ""