Skip to content

Commit

Permalink
Merge pull request #1243 from noirbizarre/avatar-cache
Browse files Browse the repository at this point in the history
Adds some avatar/identicon tests and cache identicon rendering
  • Loading branch information
noirbizarre committed Oct 25, 2017
2 parents a73221e + dd9dfae commit 3eeec3e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Get cached linkchecker result before hitting API [#1235](https://github.com/opendatateam/udata/pull/1235)
- Cleanup resources checksum [migration] [#1239](https://github.com/opendatateam/udata/pull/1239)
- Show check results in resource modal [#1242](https://github.com/opendatateam/udata/pull/1242)
- Cache avatar rendering [#1243](https://github.com/opendatateam/udata/pull/1243)

## 1.2.0 (2017-10-20)

Expand Down
30 changes: 19 additions & 11 deletions udata/features/identicon/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

import hashlib
import io
import sys

import pydenticon

from flask import redirect, send_file, current_app

from udata import theme, entrypoints
from udata.app import cache

ADORABLE_AVATARS_URL = 'https://api.adorable.io/avatars/{size}/{identifier}.png'
ADORABLE_AVATARS_URL = 'https://api.adorable.io/avatars/{size}/{identifier}.png' # noqa
ROBOHASH_URL = 'https://robohash.org/{identifier}.png?size={size}x{size}&set={skin}&bgset={bg}' # noqa


Expand Down Expand Up @@ -76,17 +76,17 @@ def get_identicon(identifier, size):
return get_provider()(identifier, size)


def internal(identifier, size):
@cache.memoize()
def generate_pydenticon(identifier, size):
'''
Internal provider
Use pydenticon to generate an identicon.
Use pydenticon to generate an identicon image.
All parameters are extracted from configuration.
'''
blocks_size = get_internal_config('size')
foreground = get_internal_config('foreground')
background = get_internal_config('background')
generator = pydenticon.Generator(blocks_size, blocks_size, digest=hashlib.sha1,
generator = pydenticon.Generator(blocks_size, blocks_size,
digest=hashlib.sha1,
foreground=foreground,
background=background)

Expand All @@ -95,11 +95,19 @@ def internal(identifier, size):
padding = int(round(get_internal_config('padding') * size / 100.))
size = size - 2 * padding
padding = (padding, ) * 4
identicon = generator.generate(identifier, size, size,
padding=padding,
output_format='png')
return send_file(io.BytesIO(identicon), mimetype='image/png')
return generator.generate(identifier, size, size,
padding=padding,
output_format='png')


def internal(identifier, size):
'''
Internal provider
Use pydenticon to generate an identicon.
'''
identicon = generate_pydenticon(identifier, size)
return send_file(io.BytesIO(identicon), mimetype='image/png')


def adorable(identifier, size):
Expand Down
Empty file.
24 changes: 24 additions & 0 deletions udata/features/identicon/tests/test_backends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from udata.features.identicon.backends import internal

from udata.tests import TestCase
from udata.utils import faker


class InternalBackendTests(TestCase):
def assert_stream_equal(self, response1, response2):
self.assertEqual(list(response1.iter_encoded()),
list(response2.iter_encoded()))

def test_base_rendering(self):
response = internal(faker.word(), 32)
self.assert200(response)
self.assertEqual(response.mimetype, 'image/png')
self.assertTrue(response.is_streamed)

def test_render_twice_the_same(self):
identifier = faker.word()
self.assert_stream_equal(internal(identifier, 32),
internal(identifier, 32))

0 comments on commit 3eeec3e

Please sign in to comment.