Fix some lint warnings, add Robohash (First shot, Issue #13) and make OpenId work again

This commit is contained in:
Oliver Falk
2018-12-03 16:01:20 +01:00
parent bc9730450c
commit 64f804b876
2 changed files with 31 additions and 14 deletions

View File

@@ -4,19 +4,20 @@ views under /
from io import BytesIO
from os import path
import hashlib
from PIL import Image
from urllib.request import urlopen
from urllib.error import HTTPError, URLError
from ssl import SSLError
from django.views.generic.base import TemplateView, View
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound
from django.core.exceptions import ObjectDoesNotExist
from django.utils.translation import ugettext_lazy as _
from django.urls import reverse_lazy
from urllib.request import urlopen
from urllib.error import HTTPError, URLError
from ssl import SSLError
from PIL import Image
from monsterid.id import build_monster as BuildMonster
from pydenticon import Generator as IdenticonGenerator
from robohash import Robohash
from ivatar.settings import AVATAR_MAX_SIZE, JPEG_QUALITY, DEFAULT_AVATAR_SIZE
from . ivataraccount.models import ConfirmedEmail, ConfirmedOpenId
@@ -26,6 +27,9 @@ URL_TIMEOUT = 5 # in seconds
def get_size(request, size=DEFAULT_AVATAR_SIZE):
'''
Get size from the URL arguments
'''
sizetemp = None
if 's' in request.GET:
sizetemp = request.GET['s']
@@ -52,7 +56,7 @@ class AvatarImageView(TemplateView):
'''
# TODO: Do cache resize images!! Memcached?
def get(self, request, *args, **kwargs): # pylint: disable=too-many-branches,too-many-statements,too-many-locals
def get(self, request, *args, **kwargs): # pylint: disable=too-many-branches,too-many-statements,too-many-locals,too-many-return-statements
'''
Override get from parent class
'''
@@ -91,8 +95,13 @@ class AvatarImageView(TemplateView):
try:
obj = model.objects.get(digest_sha256=kwargs['digest'])
except ObjectDoesNotExist:
model = ConfirmedOpenId
try:
obj = model.objects.get(digest=kwargs['digest'])
except:
pass
# If that mail/openid doesn't exist, or has no photo linked to it
if not obj or not obj.photo or forcedefault:
gravatar_url = 'https://secure.gravatar.com/avatar/' + kwargs['digest'] \
@@ -123,6 +132,16 @@ class AvatarImageView(TemplateView):
data,
content_type='image/png')
if str(default) == 'robohash':
robohash = Robohash(kwargs['digest'])
robohash.assemble(roboset='any', sizex=size, sizey=size)
data = BytesIO()
robohash.img.save(data, format='png')
data.seek(0)
return HttpResponse(
data,
content_type='image/png')
if str(default) == 'identicon' or str(default) == 'retro':
# Taken from example code
foreground = [
@@ -157,7 +176,6 @@ class AvatarImageView(TemplateView):
static_img = path.join('static', 'img', 'mm', '512.png')
# We trust static/ is mapped to /static/
return HttpResponseRedirect('/' + static_img)
else:
return HttpResponseRedirect(default)
static_img = path.join('static', 'img', 'nobody', '%s%s' % (str(size), '.png'))
@@ -188,7 +206,7 @@ class GravatarProxyView(View):
'''
# TODO: Do cache images!! Memcached?
def get(self, request, *args, **kwargs): # pylint: disable=too-many-branches,too-many-statements,too-many-locals
def get(self, request, *args, **kwargs): # pylint: disable=too-many-branches,too-many-statements,too-many-locals,no-self-use,unused-argument
'''
Override get from parent class
'''
@@ -205,17 +223,14 @@ class GravatarProxyView(View):
print(
'Gravatar fetch failed with an unexpected %s HTTP error' %
exc.code)
pass
except URLError as exc:
print(
'Gravatar fetch failed with URL error: %s' %
exc.reason)
pass
except SSLError as exc:
print(
'Gravatar fetch failed with SSL error: %s' %
exc.reason)
pass
try:
data = BytesIO(gravatarimagedata.read())
img = Image.open(data)
@@ -226,8 +241,9 @@ class GravatarProxyView(View):
except ValueError as exc:
print('Value error: %s' % exc)
pass
# TODO: In case anything strange happens, we need to redirect to the default
url = reverse_lazy('avatar_view', args=[kwargs['digest']]) + '?s=%i' % size + '&forcedefault=y'
url = reverse_lazy(
'avatar_view',
args=[kwargs['digest']]) + '?s=%i' % size + '&forcedefault=y'
return HttpResponseRedirect(url)

View File

@@ -34,3 +34,4 @@ psycopg2
notsetuptools
git+https://github.com/ofalk/monsterid.git
git+https://github.com/azaghal/pydenticon.git
robohash