From 64f804b876d94f8456f187e6036916e6082239ed Mon Sep 17 00:00:00 2001 From: Oliver Falk Date: Mon, 3 Dec 2018 16:01:20 +0100 Subject: [PATCH 1/3] Fix some lint warnings, add Robohash (First shot, Issue #13) and make OpenId work again --- ivatar/views.py | 44 ++++++++++++++++++++++++++++++-------------- requirements.txt | 1 + 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/ivatar/views.py b/ivatar/views.py index a9e9348..9d8de33 100644 --- a/ivatar/views.py +++ b/ivatar/views.py @@ -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,7 +95,12 @@ class AvatarImageView(TemplateView): try: obj = model.objects.get(digest_sha256=kwargs['digest']) except ObjectDoesNotExist: - pass + 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: @@ -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,8 +176,7 @@ 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) + return HttpResponseRedirect(default) static_img = path.join('static', 'img', 'nobody', '%s%s' % (str(size), '.png')) if not path.isfile(static_img): @@ -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) diff --git a/requirements.txt b/requirements.txt index cfd7b9e..aec5e22 100644 --- a/requirements.txt +++ b/requirements.txt @@ -34,3 +34,4 @@ psycopg2 notsetuptools git+https://github.com/ofalk/monsterid.git git+https://github.com/azaghal/pydenticon.git +robohash From 7c1b8218201584dccc2506d06aa5dab0bf57daf3 Mon Sep 17 00:00:00 2001 From: Oliver Falk Date: Mon, 3 Dec 2018 16:17:33 +0100 Subject: [PATCH 2/3] Use latest master tree on GitHub for robohash and allow to choose the set with robohash= (set1-3) --- ivatar/views.py | 5 ++++- requirements.txt | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/ivatar/views.py b/ivatar/views.py index 9d8de33..6629e93 100644 --- a/ivatar/views.py +++ b/ivatar/views.py @@ -133,8 +133,11 @@ class AvatarImageView(TemplateView): content_type='image/png') if str(default) == 'robohash': + roboset = 'any' + if request.GET.get('robohash'): + roboset = request.GET.get('robohash') robohash = Robohash(kwargs['digest']) - robohash.assemble(roboset='any', sizex=size, sizey=size) + robohash.assemble(roboset=roboset, sizex=size, sizey=size) data = BytesIO() robohash.img.save(data, format='png') data.seek(0) diff --git a/requirements.txt b/requirements.txt index aec5e22..f23c634 100644 --- a/requirements.txt +++ b/requirements.txt @@ -34,4 +34,4 @@ psycopg2 notsetuptools git+https://github.com/ofalk/monsterid.git git+https://github.com/azaghal/pydenticon.git -robohash +git+https://github.com/e1ven/Robohash.git From 3f04e183d4ea6217e55882e607040ac795e3ac05 Mon Sep 17 00:00:00 2001 From: Oliver Falk Date: Mon, 3 Dec 2018 16:27:10 +0100 Subject: [PATCH 3/3] Regression from 7c1b8218201584dccc2506d06aa5dab0bf57daf3, switch back to latest official version, since build breaks with UnicodeDecodeError --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index f23c634..aec5e22 100644 --- a/requirements.txt +++ b/requirements.txt @@ -34,4 +34,4 @@ psycopg2 notsetuptools git+https://github.com/ofalk/monsterid.git git+https://github.com/azaghal/pydenticon.git -git+https://github.com/e1ven/Robohash.git +robohash