2016-04-27 13:32:38 +00:00
|
|
|
from django.apps import apps
|
2015-06-02 12:59:49 +00:00
|
|
|
from django.http import Http404
|
2015-10-05 12:09:11 +00:00
|
|
|
from django.contrib.admin.utils import unquote
|
2015-06-02 12:59:49 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2015-10-08 09:00:22 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2015-06-02 12:59:49 +00:00
|
|
|
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)
|
2015-06-02 12:59:49 +00:00
|
|
|
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()
|