123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- from .context import context_adaption
- from django.conf import settings
- from django.shortcuts import render, redirect, HttpResponse
- from django.http import HttpResponseNotFound, HttpResponseForbidden
- import importlib
- import mimetypes
- import logging
- import mycreole
- import os
- import themes
- from django.contrib import messages
-
- logger = logging.getLogger('APP')
-
-
- def get_method(import_string):
- class_data = import_string.split(".")
- module_path = ".".join(class_data[:-1])
- class_str = class_data[-1]
- #
- module = importlib.import_module(module_path)
- return getattr(module, class_str)
-
-
- def access(access_type, request, rel_path):
- def log_warning(access_type, e):
- logger.warning('Could not import %s_access method for checking access rights: %s - %s', access_type, type(e).__name__, e)
-
- try:
- method_name = settings.MYCREOLE_ATTACHMENT_ACCESS[access_type]
- except (AttributeError, KeyError) as e:
- log_warning(access_type, e)
- return False
- else:
- if method_name in [True, None]:
- return True
- elif method_name is False:
- return False
- else:
- try:
- return get_method(method_name)(request, rel_path)
- except AttributeError as e:
- log_warning(access_type, e)
- return False
-
-
- def mycreole_attachment(request, rel_path):
- full_path = mycreole.get_full_path(rel_path)
- if access('read', request, rel_path):
- if os.path.isfile(full_path):
- mimetypes.init()
- mime_type = mimetypes.types_map.get(os.path.splitext(full_path)[1])
- data = open(full_path, 'rb').read()
- return HttpResponse(data, content_type=mime_type)
- else:
- return HttpResponseNotFound(rel_path)
- else:
- return HttpResponseForbidden(rel_path)
-
-
- def mycreole_upload(request, rel_path):
- if access('modify', request, rel_path):
- if not request.POST:
- context = themes.Context(request)
- context_adaption(context, request, 'Upload %s' % rel_path, next=request.GET.get('next', '/'))
- return render(request, 'mycreole/upload.html', context=context)
- else:
- full_path = mycreole.get_full_path(rel_path)
- try:
- os.makedirs(os.path.dirname(full_path), exist_ok=True)
- except PermissionError:
- raise PermissionError("Ensure that we have access to MYCREOLE_ROOT=%s" % repr(settings.MYCREOLE_ROOT))
- else:
- with open(full_path, 'wb') as fh:
- fh.write(request.FILES['file'].read())
- return redirect(request.POST.get('next', '/'))
- else:
- messages.error(request, "Upload: Access denied!")
- return redirect(request.GET.get('next', '/'))
-
-
- def mycreole_manageuploads(request, rel_path):
- messages.error(request, 'Manage Uploads: Not yet implemented!')
- return redirect(request.GET.get('next', '/'))
|