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

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