Fetch image via hash for OpenID and email

This commit is contained in:
Oliver Falk
2018-05-22 14:41:42 +02:00
parent 4646c38f8a
commit 7f4b82abe3
2 changed files with 37 additions and 0 deletions

34
ivatar/views.py Normal file
View File

@@ -0,0 +1,34 @@
'''
views under /
'''
import io
from django.views.generic.base import TemplateView
from django.http import HttpResponse
from . ivataraccount.models import ConfirmedEmail, ConfirmedOpenId
class AvatarImageView(TemplateView):
'''
View to return (binary) image, based for OpenID/Email (both by digest)
'''
def get(self, *args, **kwargs):
'''
Override get from parent class
'''
model = ConfirmedEmail
if len(kwargs['digest']) == 32:
# Fetch by digest from mail
pass
elif len(kwargs['digest']) == 64:
# Fetch by digest from OpenID
model = ConfirmedOpenId
else:
raise Exception('Digest provided is wrong: %s' % kwargs['digest'])
email = model.objects.get(digest=kwargs['digest'])
if not email.photo:
raise Exception('No photo assigned to "%s"' % email.email)
return HttpResponse(
io.BytesIO(email.photo.data), content_type='image/%s' % email.photo.format)