Django Library Mycreole
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. from .context import context_adaption
  2. from django.conf import settings
  3. from django.shortcuts import render, redirect, HttpResponse
  4. from django.http import HttpResponseNotFound, HttpResponseForbidden
  5. import fstools
  6. import importlib
  7. import mimetypes
  8. from mycreole import url_upload, url_attachment, url_delete
  9. import logging
  10. import mycreole
  11. import os
  12. from mycreole import parameter
  13. import stringtools
  14. import themes
  15. from django.contrib import messages
  16. logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
  17. def get_next(request):
  18. if not request.POST:
  19. return request.GET.get('next', '/')
  20. else:
  21. return request.POST.get('next', '/')
  22. def access(access_type, request, rel_path):
  23. access = parameter.get(parameter.MYCREOLE_ATTACHMENT_ACCESS)
  24. method = access[access_type]
  25. return method(request, rel_path)
  26. def mycreole_attachment(request, rel_path):
  27. full_path = mycreole.get_full_path(rel_path)
  28. if access('read', request, rel_path):
  29. if os.path.isfile(full_path):
  30. mimetypes.init()
  31. mime_type = mimetypes.types_map.get(os.path.splitext(full_path)[1])
  32. data = open(full_path, 'rb').read()
  33. return HttpResponse(data, content_type=mime_type)
  34. else:
  35. return HttpResponseNotFound(rel_path)
  36. else:
  37. return HttpResponseForbidden(rel_path)
  38. def mycreole_upload(request, rel_path):
  39. if access('modify', request, rel_path):
  40. if not request.POST:
  41. context = themes.Context(request)
  42. context_adaption(
  43. context,
  44. request,
  45. 'Upload %s' % rel_path,
  46. os.path.dirname(rel_path),
  47. rel_path_is_directory=os.path.isdir(mycreole.get_full_path(rel_path)),
  48. next=request.GET.get('next', '/')
  49. )
  50. return render(request, 'mycreole/upload.html', context=context)
  51. else:
  52. filename = request.POST.get('filename')
  53. full_path = mycreole.get_full_path(rel_path)
  54. if filename is not None:
  55. full_path = os.path.join(full_path, filename)
  56. try:
  57. os.makedirs(os.path.dirname(full_path), exist_ok=True)
  58. except PermissionError:
  59. raise PermissionError("Ensure that we have access to MYCREOLE_ROOT=%s" % repr(parameter.get(parameter.MYCREOLE_ROOT)))
  60. else:
  61. with open(full_path, 'wb') as fh:
  62. fh.write(request.FILES['file'].read())
  63. messages.info(request, 'File %s successfully uploaded.' % os.path.basename(full_path))
  64. return redirect(request.POST.get('next', '/'))
  65. else:
  66. messages.error(request, "Upload: Access denied!")
  67. return redirect(request.GET.get('next', '/'))
  68. def mycreole_delete(request, rel_path):
  69. context = themes.Context(request) # needs to be executed first because of time mesurement
  70. nxt = get_next(request)
  71. modify_access = access('modify', request, rel_path)
  72. full_path = mycreole.get_full_path(rel_path)
  73. if not modify_access:
  74. messages.error(request, 'Detele Attachment: Access denied!')
  75. else:
  76. if not request.POST:
  77. context_adaption(
  78. context,
  79. request,
  80. 'Delete %s' % os.path.basename(rel_path),
  81. os.path.dirname(rel_path),
  82. next=nxt,
  83. filename=os.path.basename(rel_path)
  84. )
  85. return render(request, 'mycreole/delete.html', context=context)
  86. else:
  87. if request.POST.get('yes') is not None:
  88. try:
  89. os.remove(full_path)
  90. except Exception:
  91. messages.error(request, 'Error while deleting file from filesystem.')
  92. logger.exception('Fatal error while deleting Attachment!')
  93. else:
  94. messages.info(request, "Attachment %s deleted..." % os.path.basename(full_path))
  95. else:
  96. messages.info(request, 'Delete request aborded.')
  97. return redirect(nxt)
  98. def mycreole_manageuploads(request, rel_path):
  99. context = themes.Context(request) # needs to be executed first because of time mesurement
  100. nxt = get_next(request)
  101. read_access = access('read', request, rel_path)
  102. modify_access = access('modify', request, rel_path)
  103. #
  104. if not read_access:
  105. messages.error(request, "Manageupload: Access denied!")
  106. return redirect(nxt)
  107. basepath = mycreole.get_full_path(rel_path)
  108. if not os.path.exists(basepath):
  109. logger.info('Creating path for attachments: %s', basepath)
  110. fstools.mkdir(basepath)
  111. logger.debug("Searching for files in %s", basepath)
  112. attachments = {}
  113. for file in fstools.filelist(basepath):
  114. filename = os.path.basename(file)
  115. actions = []
  116. if modify_access:
  117. actions.append((themes.gray_icon_url(request, 'delete.png'), url_delete(request, os.path.join(rel_path, filename)), 'delete'))
  118. actions.append((themes.gray_icon_url(request, 'shuffle.png'), url_upload(request, os.path.join(rel_path, filename)), 'replace'))
  119. actions.append((themes.gray_icon_url(request, 'view.png'), url_attachment(os.path.join(rel_path, filename)), 'view'))
  120. attachments[filename] = {
  121. 'size': stringtools.physical_value_repr(os.path.getsize(file), 'B'),
  122. 'modify_access': modify_access,
  123. 'actions': actions,
  124. }
  125. logger.debug("%d attachments detected: %s", len(attachments), ', '.join(attachments.keys()))
  126. #
  127. mycreole.context.context_adaption(
  128. context,
  129. request,
  130. 'Manageuploads for %s' % rel_path,
  131. rel_path,
  132. next=nxt,
  133. attachments=attachments
  134. )
  135. return render(request, 'mycreole/manageuploads.html', context=context)