Django Library Mycreole
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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