Piki is a minimal wiki
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

views.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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, page_list
  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. history = request.GET.get("history")
  27. if history:
  28. history = int(history)
  29. #
  30. p = creole_page(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. )
  50. return render(request, 'pages/page.html', context=context)
  51. def edit(request, rel_path):
  52. if access.write_page(request, rel_path):
  53. context = Context(request) # needs to be executed first because of time mesurement
  54. #
  55. if not request.POST:
  56. history = request.GET.get("history")
  57. if history:
  58. history = int(history)
  59. #
  60. p = creole_page(request, rel_path, history_version=history)
  61. #
  62. form = EditForm(page_data=p.raw_page_src, page_tags=p.page_tags)
  63. #
  64. context_adaption(
  65. context,
  66. request,
  67. form=form,
  68. # TODO: Add translation
  69. title=_("Edit page %s") % repr(p.title),
  70. upload_path=p.attachment_path,
  71. )
  72. return render(request, 'pages/page_form.html', context=context)
  73. else:
  74. p = creole_page(request, rel_path)
  75. #
  76. save = request.POST.get("save")
  77. page_txt = request.POST.get("page_txt")
  78. tags = request.POST.get("page_tags")
  79. preview = request.POST.get("preview")
  80. #
  81. if save is not None:
  82. if p.update_page(page_txt, tags):
  83. messages.edit_success(request)
  84. else:
  85. messages.edit_no_change(request)
  86. return HttpResponseRedirect(url_page(request, rel_path))
  87. elif preview is not None:
  88. form = EditForm(page_data=page_txt)
  89. #
  90. context_adaption(
  91. context,
  92. request,
  93. form=form,
  94. # TODO: Add translation
  95. title=_("Edit page %s") % repr(p.title),
  96. upload_path=p.attachment_path,
  97. page_content=p.render_text(request, page_txt)
  98. )
  99. return render(request, 'pages/page_form.html', context=context)
  100. else:
  101. return HttpResponseRedirect(url_page(request, rel_path))
  102. else:
  103. messages.permission_denied_msg_page(request, rel_path)
  104. return HttpResponseRedirect(url_page(request, rel_path))
  105. def search(request):
  106. context = Context(request) # needs to be executed first because of time mesurement
  107. #
  108. search_txt = get_search_query(request)
  109. sr = whoosh_search(search_txt)
  110. if sr is None:
  111. django_messages.error(request, _('Invalid search pattern: %s') % repr(search_txt))
  112. sr = []
  113. pl = page_list(request, [creole_page(request, rel_path) for rel_path in set(sr)])
  114. #
  115. context_adaption(
  116. context,
  117. request,
  118. title=_("Searchresults"),
  119. page_content=mycreole.render_simple(pl.creole_list())
  120. )
  121. return render(request, 'pages/page.html', context=context)
  122. def helpview(request, page='main'):
  123. context = Context(request) # needs to be executed first because of time mesurement
  124. page_content = help_pages[page]
  125. context_adaption(
  126. context, # the base context
  127. request, # the request object to be used in context_adaption
  128. current_help_page=page, # the current help_page to identify which taskbar entry has to be highlighted
  129. page_content=page_content, # the help content itself (template)
  130. title=_('Help') # the title for the page (template)
  131. )
  132. return render(request, 'pages/page.html', context=context)