mirror of
https://git.linux-kernel.at/oliver/ivatar.git
synced 2025-11-17 13:38:03 +00:00
Fix some lint warnings, add Robohash (First shot, Issue #13) and make OpenId work again
This commit is contained in:
@@ -4,19 +4,20 @@ views under /
|
|||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from os import path
|
from os import path
|
||||||
import hashlib
|
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.views.generic.base import TemplateView, View
|
||||||
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound
|
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
from django.urls import reverse_lazy
|
from django.urls import reverse_lazy
|
||||||
|
|
||||||
from urllib.request import urlopen
|
from PIL import Image
|
||||||
from urllib.error import HTTPError, URLError
|
|
||||||
from ssl import SSLError
|
|
||||||
|
|
||||||
from monsterid.id import build_monster as BuildMonster
|
from monsterid.id import build_monster as BuildMonster
|
||||||
from pydenticon import Generator as IdenticonGenerator
|
from pydenticon import Generator as IdenticonGenerator
|
||||||
|
from robohash import Robohash
|
||||||
|
|
||||||
from ivatar.settings import AVATAR_MAX_SIZE, JPEG_QUALITY, DEFAULT_AVATAR_SIZE
|
from ivatar.settings import AVATAR_MAX_SIZE, JPEG_QUALITY, DEFAULT_AVATAR_SIZE
|
||||||
from . ivataraccount.models import ConfirmedEmail, ConfirmedOpenId
|
from . ivataraccount.models import ConfirmedEmail, ConfirmedOpenId
|
||||||
@@ -26,6 +27,9 @@ URL_TIMEOUT = 5 # in seconds
|
|||||||
|
|
||||||
|
|
||||||
def get_size(request, size=DEFAULT_AVATAR_SIZE):
|
def get_size(request, size=DEFAULT_AVATAR_SIZE):
|
||||||
|
'''
|
||||||
|
Get size from the URL arguments
|
||||||
|
'''
|
||||||
sizetemp = None
|
sizetemp = None
|
||||||
if 's' in request.GET:
|
if 's' in request.GET:
|
||||||
sizetemp = request.GET['s']
|
sizetemp = request.GET['s']
|
||||||
@@ -52,7 +56,7 @@ class AvatarImageView(TemplateView):
|
|||||||
'''
|
'''
|
||||||
# TODO: Do cache resize images!! Memcached?
|
# 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
|
Override get from parent class
|
||||||
'''
|
'''
|
||||||
@@ -91,8 +95,13 @@ class AvatarImageView(TemplateView):
|
|||||||
try:
|
try:
|
||||||
obj = model.objects.get(digest_sha256=kwargs['digest'])
|
obj = model.objects.get(digest_sha256=kwargs['digest'])
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
|
model = ConfirmedOpenId
|
||||||
|
try:
|
||||||
|
obj = model.objects.get(digest=kwargs['digest'])
|
||||||
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
# If that mail/openid doesn't exist, or has no photo linked to it
|
# If that mail/openid doesn't exist, or has no photo linked to it
|
||||||
if not obj or not obj.photo or forcedefault:
|
if not obj or not obj.photo or forcedefault:
|
||||||
gravatar_url = 'https://secure.gravatar.com/avatar/' + kwargs['digest'] \
|
gravatar_url = 'https://secure.gravatar.com/avatar/' + kwargs['digest'] \
|
||||||
@@ -123,6 +132,16 @@ class AvatarImageView(TemplateView):
|
|||||||
data,
|
data,
|
||||||
content_type='image/png')
|
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':
|
if str(default) == 'identicon' or str(default) == 'retro':
|
||||||
# Taken from example code
|
# Taken from example code
|
||||||
foreground = [
|
foreground = [
|
||||||
@@ -157,7 +176,6 @@ class AvatarImageView(TemplateView):
|
|||||||
static_img = path.join('static', 'img', 'mm', '512.png')
|
static_img = path.join('static', 'img', 'mm', '512.png')
|
||||||
# We trust static/ is mapped to /static/
|
# We trust static/ is mapped to /static/
|
||||||
return HttpResponseRedirect('/' + static_img)
|
return HttpResponseRedirect('/' + static_img)
|
||||||
else:
|
|
||||||
return HttpResponseRedirect(default)
|
return HttpResponseRedirect(default)
|
||||||
|
|
||||||
static_img = path.join('static', 'img', 'nobody', '%s%s' % (str(size), '.png'))
|
static_img = path.join('static', 'img', 'nobody', '%s%s' % (str(size), '.png'))
|
||||||
@@ -188,7 +206,7 @@ class GravatarProxyView(View):
|
|||||||
'''
|
'''
|
||||||
# TODO: Do cache images!! Memcached?
|
# 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
|
Override get from parent class
|
||||||
'''
|
'''
|
||||||
@@ -205,17 +223,14 @@ class GravatarProxyView(View):
|
|||||||
print(
|
print(
|
||||||
'Gravatar fetch failed with an unexpected %s HTTP error' %
|
'Gravatar fetch failed with an unexpected %s HTTP error' %
|
||||||
exc.code)
|
exc.code)
|
||||||
pass
|
|
||||||
except URLError as exc:
|
except URLError as exc:
|
||||||
print(
|
print(
|
||||||
'Gravatar fetch failed with URL error: %s' %
|
'Gravatar fetch failed with URL error: %s' %
|
||||||
exc.reason)
|
exc.reason)
|
||||||
pass
|
|
||||||
except SSLError as exc:
|
except SSLError as exc:
|
||||||
print(
|
print(
|
||||||
'Gravatar fetch failed with SSL error: %s' %
|
'Gravatar fetch failed with SSL error: %s' %
|
||||||
exc.reason)
|
exc.reason)
|
||||||
pass
|
|
||||||
try:
|
try:
|
||||||
data = BytesIO(gravatarimagedata.read())
|
data = BytesIO(gravatarimagedata.read())
|
||||||
img = Image.open(data)
|
img = Image.open(data)
|
||||||
@@ -226,8 +241,9 @@ class GravatarProxyView(View):
|
|||||||
|
|
||||||
except ValueError as exc:
|
except ValueError as exc:
|
||||||
print('Value error: %s' % exc)
|
print('Value error: %s' % exc)
|
||||||
pass
|
|
||||||
|
|
||||||
# TODO: In case anything strange happens, we need to redirect to the default
|
# 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)
|
return HttpResponseRedirect(url)
|
||||||
|
|||||||
@@ -34,3 +34,4 @@ psycopg2
|
|||||||
notsetuptools
|
notsetuptools
|
||||||
git+https://github.com/ofalk/monsterid.git
|
git+https://github.com/ofalk/monsterid.git
|
||||||
git+https://github.com/azaghal/pydenticon.git
|
git+https://github.com/azaghal/pydenticon.git
|
||||||
|
robohash
|
||||||
|
|||||||
Reference in New Issue
Block a user