Allow specifying default as get request parameter (as specified in the standard); Do not serve picture directly, but redirect to /static

This commit is contained in:
Oliver Falk
2018-07-10 11:06:27 +02:00
parent 37197162bc
commit 2ace37e981

View File

@@ -5,7 +5,7 @@ from io import BytesIO
from os import path from os import path
from PIL import Image from PIL import Image
from django.views.generic.base import TemplateView from django.views.generic.base import TemplateView
from django.http import HttpResponse from django.http import HttpResponse, HttpResponseRedirect
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from ivatar.settings import AVATAR_MAX_SIZE, JPEG_QUALITY from ivatar.settings import AVATAR_MAX_SIZE, JPEG_QUALITY
from . ivataraccount.models import ConfirmedEmail, ConfirmedOpenId from . ivataraccount.models import ConfirmedEmail, ConfirmedOpenId
@@ -26,6 +26,11 @@ class AvatarImageView(TemplateView):
size = 80 size = 80
imgformat = 'png' imgformat = 'png'
obj = None obj = None
default = None
if 'd' in request.GET:
default = request.GET['d']
if 's' in request.GET: if 's' in request.GET:
size = request.GET['s'] size = request.GET['s']
size = int(size) size = int(size)
@@ -51,13 +56,19 @@ class AvatarImageView(TemplateView):
except ObjectDoesNotExist: except ObjectDoesNotExist:
pass pass
# If that mail/openid doesn't exist, or has no photo linked to it
if not obj or not obj.photo: if not obj or not obj.photo:
static_img = path.join('static', 'img', 'mm', '%s%s' % (str(size), '.png')) # Return the default URL, as specified
if path.isfile(static_img): if default:
photodata = Image.open(static_img) return HttpResponseRedirect(default)
# Return our default URl
else: else:
# TODO: Resize it!? static_img = path.join('static', 'img', 'mm', '%s%s' % (str(size), '.png'))
static_img = path.join('static', 'img', 'mm', '512.png') if not path.isfile(static_img):
# We trust this exists!!!
static_img = path.join('static', 'img', 'mm', '512.png')
# We trust static/ is mapped to /static/
return HttpResponseRedirect('/' + static_img)
else: else:
imgformat = obj.photo.format imgformat = obj.photo.format
photodata = Image.open(BytesIO(obj.photo.data)) photodata = Image.open(BytesIO(obj.photo.data))
@@ -70,5 +81,3 @@ class AvatarImageView(TemplateView):
return HttpResponse( return HttpResponse(
data, data,
content_type='image/%s' % imgformat) content_type='image/%s' % imgformat)
# One eventually also wants to check if the DATA is correct,
# not only the size