django-orchestra-test/orchestra/views.py

21 lines
774 B
Python
Raw Permalink Normal View History

2016-04-27 13:32:38 +00:00
from django.apps import apps
from django.http import Http404
2015-10-05 12:09:11 +00:00
from django.contrib.admin.utils import unquote
from django.core.exceptions import PermissionDenied
2015-10-08 09:00:22 +00:00
from django.shortcuts import get_object_or_404
from django.views.static import serve
def serve_private_media(request, app_label, model_name, field_name, object_id, filename):
2016-04-27 13:32:38 +00:00
model = apps.get_model(app_label, model_name)
if model is None:
raise Http404('')
instance = get_object_or_404(model, pk=unquote(object_id))
if not hasattr(instance, field_name):
raise Http404('')
field = getattr(instance, field_name)
if field.condition(request, instance):
return serve(request, field.name, document_root=field.storage.location)
else:
raise PermissionDenied()