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.

views.py 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. from django.conf import settings
  2. from django.contrib import messages as django_messages
  3. from django.shortcuts import render
  4. from django.http import HttpResponse, HttpResponseRedirect
  5. from django.utils.translation import gettext as _
  6. import logging
  7. from . import access
  8. from . import messages
  9. from . import url_page
  10. from . import get_search_query
  11. import config
  12. from .context import context_adaption
  13. from .forms import EditForm, RenameForm
  14. from .help import help_pages
  15. import mycreole
  16. from .page import page_wrapped, page_list
  17. from .search import whoosh_search, load_index, delete_item, add_item
  18. from themes import Context
  19. logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
  20. def root(request):
  21. return HttpResponseRedirect(url_page(config.STARTPAGE))
  22. def page(request, rel_path):
  23. context = Context(request) # needs to be executed first because of time mesurement
  24. #
  25. meta = "meta" in request.GET
  26. history = request.GET.get("history")
  27. if history:
  28. history = int(history)
  29. #
  30. p = page_wrapped(request, rel_path, history_version=history)
  31. if access.read_page(request, rel_path):
  32. if meta:
  33. page_content = p.render_meta()
  34. else:
  35. page_content = p.render_to_html()
  36. if history:
  37. messages.history_version_display(request, rel_path, history)
  38. else:
  39. messages.permission_denied_msg_page(request, rel_path)
  40. page_content = ""
  41. #
  42. context_adaption(
  43. context,
  44. request,
  45. rel_path=rel_path,
  46. title=p.title,
  47. upload_path=p.attachment_path,
  48. page_content=page_content,
  49. is_available=p.userpage_is_available()
  50. )
  51. return render(request, 'pages/page.html', context=context)
  52. def edit(request, rel_path):
  53. if access.write_page(request, rel_path):
  54. context = Context(request) # needs to be executed first because of time mesurement
  55. #
  56. if not request.POST:
  57. history = request.GET.get("history")
  58. if history:
  59. history = int(history)
  60. #
  61. p = page_wrapped(request, rel_path, history_version=history)
  62. #
  63. form = EditForm(page_data=p.raw_page_src, page_tags=p.tags)
  64. #
  65. context_adaption(
  66. context,
  67. request,
  68. rel_path=rel_path,
  69. is_available=p.userpage_is_available(),
  70. form=form,
  71. # TODO: Add translation
  72. title=_("Edit page %s") % repr(p.title),
  73. upload_path=p.attachment_path,
  74. )
  75. return render(request, 'pages/page_edit.html', context=context)
  76. else:
  77. p = page_wrapped(request, rel_path)
  78. #
  79. save = request.POST.get("save")
  80. page_txt = request.POST.get("page_txt")
  81. tags = request.POST.get("page_tags")
  82. preview = request.POST.get("preview")
  83. #
  84. if save is not None:
  85. if p.update_page(page_txt, tags):
  86. messages.edit_success(request)
  87. else:
  88. messages.no_change(request)
  89. return HttpResponseRedirect(url_page(rel_path))
  90. elif preview is not None:
  91. form = EditForm(page_data=page_txt, page_tags=tags)
  92. #
  93. context_adaption(
  94. context,
  95. request,
  96. rel_path=rel_path,
  97. is_available=p.userpage_is_available(),
  98. form=form,
  99. # TODO: Add translation
  100. title=_("Edit page %s") % repr(p.title),
  101. upload_path=p.attachment_path,
  102. page_content=p.render_text(request, page_txt)
  103. )
  104. return render(request, 'pages/page_edit.html', context=context)
  105. else:
  106. return HttpResponseRedirect(url_page(rel_path))
  107. else:
  108. messages.permission_denied_msg_page(request, rel_path)
  109. return HttpResponseRedirect(url_page(rel_path))
  110. def delete(request, rel_path):
  111. if access.write_page(request, rel_path):
  112. context = Context(request) # needs to be executed first because of time mesurement
  113. #
  114. if not request.POST:
  115. p = page_wrapped(request, rel_path)
  116. #
  117. # form = DeleteForm(page_data=p.raw_page_src, page_tags=p.tags)
  118. #
  119. context_adaption(
  120. context,
  121. request,
  122. rel_path=rel_path,
  123. is_available=p.userpage_is_available(),
  124. # form=form,
  125. # TODO: Add translation
  126. title=_("Delete page %s") % repr(p.title),
  127. upload_path=p.attachment_path,
  128. page_content=p.render_to_html(),
  129. )
  130. else:
  131. p = page_wrapped(request, rel_path)
  132. #
  133. delete = request.POST.get("delete")
  134. #
  135. if delete:
  136. # delete page from search index
  137. ix = load_index()
  138. delete_item(ix, p)
  139. # delete move files to history
  140. p.delete()
  141. # add delete message
  142. messages.page_deleted(request, p.title)
  143. return HttpResponseRedirect("/")
  144. else:
  145. messages.operation_canceled(request)
  146. return HttpResponseRedirect(url_page(rel_path))
  147. return render(request, 'pages/page_delete.html', context=context)
  148. else:
  149. messages.permission_denied_msg_page(request, rel_path)
  150. return HttpResponseRedirect(url_page(rel_path))
  151. def rename(request, rel_path):
  152. if access.write_page(request, rel_path):
  153. context = Context(request) # needs to be executed first because of time mesurement
  154. #
  155. if not request.POST:
  156. p = page_wrapped(request, rel_path)
  157. #
  158. form = RenameForm(page_name=p.rel_path)
  159. #
  160. context_adaption(
  161. context,
  162. request,
  163. rel_path=rel_path,
  164. is_available=p.userpage_is_available(),
  165. form=form,
  166. # TODO: Add translation
  167. title=_("Delete page %s") % repr(p.title),
  168. upload_path=p.attachment_path,
  169. page_content=p.render_to_html(),
  170. )
  171. else:
  172. p = page_wrapped(request, rel_path)
  173. #
  174. rename = request.POST.get("rename")
  175. page_name = request.POST.get("page_name")
  176. if rename:
  177. if page_name == p.rel_path:
  178. messages.no_change(request)
  179. else:
  180. # delete page from search index
  181. ix = load_index()
  182. delete_item(ix, p)
  183. # rename the storage folder
  184. p.rename(page_name)
  185. # add the renamed page to the search index
  186. add_item(ix, p)
  187. # add rename message
  188. messages.page_renamed(request)
  189. else:
  190. messages.operation_canceled(request)
  191. return HttpResponseRedirect(url_page(p.rel_path))
  192. return render(request, 'pages/page_rename.html', context=context)
  193. else:
  194. messages.permission_denied_msg_page(request, rel_path)
  195. return HttpResponseRedirect(url_page(rel_path))
  196. def search(request):
  197. context = Context(request) # needs to be executed first because of time mesurement
  198. #
  199. search_txt = get_search_query(request)
  200. sr = whoosh_search(search_txt)
  201. if sr is None:
  202. django_messages.error(request, _('Invalid search pattern: %s') % repr(search_txt))
  203. sr = []
  204. pl = page_list(request, [page_wrapped(request, rel_path) for rel_path in set(sr)])
  205. #
  206. context_adaption(
  207. context,
  208. request,
  209. title=_("Searchresults"),
  210. page_content=mycreole.render_simple(pl.creole_list())
  211. )
  212. return render(request, 'pages/page.html', context=context)
  213. def helpview(request, page='main'):
  214. context = Context(request) # needs to be executed first because of time mesurement
  215. page_content = help_pages[page]
  216. context_adaption(
  217. context, # the base context
  218. request, # the request object to be used in context_adaption
  219. current_help_page=page, # the current help_page to identify which taskbar entry has to be highlighted
  220. page_content=page_content, # the help content itself (template)
  221. title=_('Help') # the title for the page (template)
  222. )
  223. return render(request, 'pages/page.html', context=context)