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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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
  14. from .help import help_pages
  15. import mycreole
  16. from .page import creole_page
  17. from .search import whoosh_search
  18. from themes import Context
  19. logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
  20. def root(request):
  21. return HttpResponseRedirect(url_page(request, 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. #
  27. p = creole_page(request, rel_path)
  28. if access.read_page(request, rel_path):
  29. if meta:
  30. page_content = p.render_meta()
  31. else:
  32. page_content = p.render_to_html()
  33. else:
  34. messages.permission_denied_msg_page(request, rel_path)
  35. page_content = ""
  36. #
  37. context_adaption(
  38. context,
  39. request,
  40. rel_path=rel_path,
  41. title=p.title,
  42. upload_path=p.attachment_path,
  43. page_content=page_content
  44. )
  45. return render(request, 'pages/page.html', context=context)
  46. def edit(request, rel_path):
  47. if access.write_page(request, rel_path):
  48. context = Context(request) # needs to be executed first because of time mesurement
  49. #
  50. p = creole_page(request, rel_path)
  51. #
  52. if not request.POST:
  53. form = EditForm(page_data=p.raw_page_src)
  54. #
  55. context_adaption(
  56. context,
  57. request,
  58. form=form,
  59. # TODO: Add translation
  60. title=_("Edit page %s") % repr(p.title),
  61. upload_path=p.attachment_path,
  62. )
  63. return render(request, 'pages/page_form.html', context=context)
  64. else:
  65. save = request.POST.get("save")
  66. page_txt = request.POST.get("page_txt")
  67. preview = request.POST.get("preview")
  68. #
  69. if save is not None:
  70. if p.update_page(page_txt):
  71. messages.edit_success(request)
  72. else:
  73. messages.edit_no_change(request)
  74. return HttpResponseRedirect(url_page(request, rel_path))
  75. elif preview is not None:
  76. form = EditForm(page_data=page_txt)
  77. #
  78. context_adaption(
  79. context,
  80. request,
  81. form=form,
  82. # TODO: Add translation
  83. title=_("Edit page %s") % repr(p.title),
  84. upload_path=p.attachment_path,
  85. page_content=p.render_text(request, page_txt)
  86. )
  87. return render(request, 'pages/page_form.html', context=context)
  88. else:
  89. return HttpResponseRedirect(url_page(request, rel_path))
  90. else:
  91. messages.permission_denied_msg_page(request, rel_path)
  92. return HttpResponseRedirect(url_page(request, rel_path))
  93. def search(request):
  94. context = Context(request) # needs to be executed first because of time mesurement
  95. #
  96. search_txt = get_search_query(request)
  97. sr = whoosh_search(search_txt)
  98. if sr is None:
  99. django_messages.error(request, _('Invalid search pattern: %s') % repr(search_txt))
  100. sr = []
  101. page_content = "= Searchresults\n"
  102. for rel_path in sr:
  103. p = creole_page(request, rel_path)
  104. page_content += f"[[/page/{rel_path}|{p.title}]]\n"
  105. #
  106. context_adaption(
  107. context,
  108. request,
  109. page_content=mycreole.render_simple(page_content)
  110. )
  111. return render(request, 'pages/page.html', context=context)
  112. def helpview(request, page='main'):
  113. context = Context(request) # needs to be executed first because of time mesurement
  114. page_content = help_pages[page]
  115. context_adaption(
  116. context, # the base context
  117. request, # the request object to be used in context_adaption
  118. current_help_page=page, # the current help_page to identify which taskbar entry has to be highlighted
  119. page_content=page_content, # the help content itself (template)
  120. title=_('Help') # the title for the page (template)
  121. )
  122. return render(request, 'pages/page.html', context=context)