Piki is a minimal wiki
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

views.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #
  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, page_tags=p.page_tags)
  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. tags = request.POST.get("page_tags")
  68. preview = request.POST.get("preview")
  69. #
  70. if save is not None:
  71. if p.update_page(page_txt, tags):
  72. messages.edit_success(request)
  73. else:
  74. messages.edit_no_change(request)
  75. return HttpResponseRedirect(url_page(request, rel_path))
  76. elif preview is not None:
  77. form = EditForm(page_data=page_txt)
  78. #
  79. context_adaption(
  80. context,
  81. request,
  82. form=form,
  83. # TODO: Add translation
  84. title=_("Edit page %s") % repr(p.title),
  85. upload_path=p.attachment_path,
  86. page_content=p.render_text(request, page_txt)
  87. )
  88. return render(request, 'pages/page_form.html', context=context)
  89. else:
  90. return HttpResponseRedirect(url_page(request, rel_path))
  91. else:
  92. messages.permission_denied_msg_page(request, rel_path)
  93. return HttpResponseRedirect(url_page(request, rel_path))
  94. def search(request):
  95. context = Context(request) # needs to be executed first because of time mesurement
  96. #
  97. search_txt = get_search_query(request)
  98. sr = whoosh_search(search_txt)
  99. if sr is None:
  100. django_messages.error(request, _('Invalid search pattern: %s') % repr(search_txt))
  101. sr = []
  102. pl = page_list(request, [creole_page(request, rel_path) for rel_path in set(sr)])
  103. #
  104. context_adaption(
  105. context,
  106. request,
  107. title=_("Searchresults"),
  108. page_content=mycreole.render_simple(pl.creole_list())
  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)