Piki is a minimal wiki
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

views.py 4.6KB

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