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

views.py 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 importlib
  6. import mimetypes
  7. import logging
  8. import mycreole
  9. import os
  10. import themes
  11. from django.contrib import messages
  12. try:
  13. from config import APP_NAME as ROOT_LOGGER_NAME
  14. except ImportError:
  15. ROOT_LOGGER_NAME = 'root'
  16. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  17. def get_method(import_string):
  18. class_data = import_string.split(".")
  19. module_path = ".".join(class_data[:-1])
  20. class_str = class_data[-1]
  21. #
  22. module = importlib.import_module(module_path)
  23. return getattr(module, class_str)
  24. def access(access_type, request, rel_path):
  25. def log_warning(access_type, e):
  26. logger.warning('Could not import %s_access method for checking access rights: %s - %s', access_type, type(e).__name__, e)
  27. try:
  28. method_name = settings.MYCREOLE_ATTACHMENT_ACCESS[access_type]
  29. except (AttributeError, KeyError) as e:
  30. log_warning(access_type, e)
  31. return False
  32. else:
  33. if method_name in [True, None]:
  34. return True
  35. elif method_name is False:
  36. return False
  37. else:
  38. try:
  39. return get_method(method_name)(request, rel_path)
  40. except AttributeError as e:
  41. log_warning(access_type, e)
  42. return False
  43. def mycreole_attachment(request, rel_path):
  44. full_path = mycreole.get_full_path(rel_path)
  45. if access('read', request, rel_path):
  46. if os.path.isfile(full_path):
  47. mimetypes.init()
  48. mime_type = mimetypes.types_map.get(os.path.splitext(full_path)[1])
  49. data = open(full_path, 'rb').read()
  50. return HttpResponse(data, content_type=mime_type)
  51. else:
  52. return HttpResponseNotFound(rel_path)
  53. else:
  54. return HttpResponseForbidden(rel_path)
  55. def mycreole_upload(request, rel_path):
  56. if access('modify', request, rel_path):
  57. if not request.POST:
  58. context = themes.Context(request)
  59. context_adaption(context, request, 'Upload %s' % rel_path, next=request.GET.get('next', '/'))
  60. return render(request, 'mycreole/upload.html', context=context)
  61. else:
  62. full_path = mycreole.get_full_path(rel_path)
  63. try:
  64. os.makedirs(os.path.dirname(full_path), exist_ok=True)
  65. except PermissionError:
  66. raise PermissionError("Ensure that we have access to MYCREOLE_ROOT=%s" % repr(settings.MYCREOLE_ROOT))
  67. else:
  68. with open(full_path, 'wb') as fh:
  69. fh.write(request.FILES['file'].read())
  70. return redirect(request.POST.get('next', '/'))
  71. else:
  72. messages.error(request, "Upload: Access denied!")
  73. return redirect(request.GET.get('next', '/'))
  74. def mycreole_manageuploads(request, rel_path):
  75. messages.error(request, 'Manage Uploads: Not yet implemented!')
  76. return redirect(request.GET.get('next', '/'))