Commit ba1aa79f authored by Thom Wiggers's avatar Thom Wiggers 📐
Browse files

Merge branch 'feature/thumbnails-api' into 'master'

Add separate private thumbnails view for API

See merge request !745
parents 7e5ae3c2 ae8c3339
...@@ -10,11 +10,11 @@ def create_image_thumbnail_dict(request, file, placeholder='', ...@@ -10,11 +10,11 @@ def create_image_thumbnail_dict(request, file, placeholder='',
'full': request.build_absolute_uri('{}{}'.format( 'full': request.build_absolute_uri('{}{}'.format(
settings.MEDIA_URL, file)), settings.MEDIA_URL, file)),
'small': request.build_absolute_uri(thumbnail( 'small': request.build_absolute_uri(thumbnail(
file, size_small, 1)), file, size_small, 1, True)),
'medium': request.build_absolute_uri(thumbnail( 'medium': request.build_absolute_uri(thumbnail(
file, size_medium, 1)), file, size_medium, 1, True)),
'large': request.build_absolute_uri(thumbnail( 'large': request.build_absolute_uri(thumbnail(
file, size_large, 1)) file, size_large, 1, True))
} }
return { return {
'full': placeholder, 'full': placeholder,
......
...@@ -48,7 +48,8 @@ from members.views import ObtainAuthToken, ObtainThaliaAuthToken ...@@ -48,7 +48,8 @@ from members.views import ObtainAuthToken, ObtainThaliaAuthToken
from partners.sitemaps import sitemap as partners_sitemap from partners.sitemaps import sitemap as partners_sitemap
from thabloid.sitemaps import sitemap as thabloid_sitemap from thabloid.sitemaps import sitemap as thabloid_sitemap
from thaliawebsite.forms import AuthenticationForm from thaliawebsite.forms import AuthenticationForm
from utils.views import private_thumbnails, generate_thumbnail from utils.views import private_thumbnails, generate_thumbnail, \
private_thumbnails_api
from . import views from . import views
from .sitemaps import StaticViewSitemap from .sitemaps import StaticViewSitemap
...@@ -107,6 +108,8 @@ urlpatterns = [ ...@@ -107,6 +108,8 @@ urlpatterns = [
url(r'^', include('pizzas.api.urls')), url(r'^', include('pizzas.api.urls')),
url(r'^', include('photos.api.urls')), url(r'^', include('photos.api.urls')),
url(r'^', include('pushnotifications.api.urls')), url(r'^', include('pushnotifications.api.urls')),
url(r'^generate-thumbnail/(?P<size_fit>\d+x\d+_[01])/(?P<path>[^/]+)/(?P<thumbpath>[^/]+)', generate_thumbnail, {'api': True}, name='generate-thumbnail-api'),
url(r'^private-thumbnails/(?P<size_fit>\d+x\d+_[01])/(?P<path>.*)', private_thumbnails_api, name='private-thumbnails-api'),
])), ])),
])), ])),
url(r'^education/', include('education.urls')), url(r'^education/', include('education.urls')),
......
...@@ -11,7 +11,7 @@ register = template.Library() # pylint: disable=invalid-name ...@@ -11,7 +11,7 @@ register = template.Library() # pylint: disable=invalid-name
@register.simple_tag @register.simple_tag
def thumbnail(path, size, fit=True): def thumbnail(path, size, fit=True, api=False):
""" """
Get the thumbnail path for the specified image path. Get the thumbnail path for the specified image path.
...@@ -43,6 +43,9 @@ def thumbnail(path, size, fit=True): ...@@ -43,6 +43,9 @@ def thumbnail(path, size, fit=True):
# We provide a URL instead of calling it as a function, so that using # We provide a URL instead of calling it as a function, so that using
# it means kicking off a new GET request. If we would generate all # it means kicking off a new GET request. If we would generate all
# thumbnails inline, loading an album overview would have high latency. # thumbnails inline, loading an album overview would have high latency.
if api:
return reverse('generate-thumbnail-api',
args=[size_fit, pathuri, thumburi])
return reverse('generate-thumbnail', return reverse('generate-thumbnail',
args=[size_fit, pathuri, thumburi]) args=[size_fit, pathuri, thumburi])
...@@ -56,4 +59,6 @@ def thumbnail(path, size, fit=True): ...@@ -56,4 +59,6 @@ def thumbnail(path, size, fit=True):
# We provide a URL instead of calling it as a function, so that using # We provide a URL instead of calling it as a function, so that using
# it means kicking off a new GET request. If we would generate all # it means kicking off a new GET request. If we would generate all
# thumbnails inline, loading an album overview would have high latency. # thumbnails inline, loading an album overview would have high latency.
if api:
return reverse('private-thumbnails-api', args=[size_fit, path])
return reverse('private-thumbnails', args=[size_fit, path]) return reverse('private-thumbnails', args=[size_fit, path])
...@@ -9,6 +9,8 @@ from django.http import Http404 ...@@ -9,6 +9,8 @@ from django.http import Http404
from django.shortcuts import redirect from django.shortcuts import redirect
from django.urls import reverse from django.urls import reverse
from django.utils.http import urlunquote from django.utils.http import urlunquote
from rest_framework import permissions
from rest_framework.decorators import api_view, permission_classes
from sendfile import sendfile from sendfile import sendfile
...@@ -43,7 +45,15 @@ def private_thumbnails(request, size_fit, path): ...@@ -43,7 +45,15 @@ def private_thumbnails(request, size_fit, path):
return _private_thumbnails_unauthed(request, size_fit, path) return _private_thumbnails_unauthed(request, size_fit, path)
def generate_thumbnail(_request, size_fit, path, thumbpath): @api_view(['GET'])
@permission_classes([permissions.IsAuthenticated])
def private_thumbnails_api(request, size_fit, path):
"""Get thumbnails that we need to be logged in to see
using token authentication."""
return _private_thumbnails_unauthed(request, size_fit, path)
def generate_thumbnail(_request, size_fit, path, thumbpath, api=False):
""" """
Generate thumbnail and redirect user to new location Generate thumbnail and redirect user to new location
...@@ -98,6 +108,10 @@ def generate_thumbnail(_request, size_fit, path, thumbpath): ...@@ -98,6 +108,10 @@ def generate_thumbnail(_request, size_fit, path, thumbpath):
return redirect(settings.MEDIA_URL + thumbpath, permanent=True) return redirect(settings.MEDIA_URL + thumbpath, permanent=True)
# Otherwise redirect to the route with an auth check # Otherwise redirect to the route with an auth check
if api:
return redirect(
reverse('private-thumbnails-api', args=[size_fit, path]),
permanent=True)
return redirect( return redirect(
reverse('private-thumbnails', args=[size_fit, path]), reverse('private-thumbnails', args=[size_fit, path]),
permanent=True) permanent=True)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment