Piki is a minimal wiki
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

views.py 4.3KB

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