Skip to content

Commit

Permalink
Merge pull request #13 from hacsoc/gravatar
Browse files Browse the repository at this point in the history
Implement Gravatar support behind config variable.
  • Loading branch information
sjaensch authored Feb 16, 2017
2 parents 4cc26c8 + bd72602 commit d375ace
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
6 changes: 6 additions & 0 deletions config-example.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@
# Name of the S3 bucket used to import employee data from a file named employees.json
# Check out /import/employees.json.example to see how this file should look like.
S3_BUCKET = 'employees'

# When do we use Gravatar? Options are:
# * 'always' - prefers Gravatar over the Employee.photo_url
# * 'backup' - use Gravatar when photo_url is empty
# * anything else - disabled
GRAVATAR = 'backup'
20 changes: 20 additions & 0 deletions models/employee.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
import base64
import hashlib
import functools

from google.appengine.ext import ndb
Expand Down Expand Up @@ -78,6 +80,24 @@ def update_from_dict(self, d):
self.department = d.get('department')
self.meta_department = get_meta_department(self.department)

def get_gravatar(self):
"""Creates gravatar URL from email address."""
m = hashlib.md5()
m.update(self.user.email())
encoded_hash = base64.b16encode(m.digest()).lower()
return '//gravatar.com/avatar/{}?s=200'.format(encoded_hash)

def get_photo_url(self):
"""Return an avatar photo URL (depending on Gravatar config). This still could
be empty, in which case the theme needs to provide an alternate photo.
"""
if config.GRAVATAR == 'always':
return self.get_gravatar()
elif config.GRAVATAR == 'backup' and not self.photo_url:
return self.get_gravatar()
else:
return self.photo_url

@property
def full_name(self):
"""Return user's full name (first name + ' ' + last name)."""
Expand Down
6 changes: 5 additions & 1 deletion testing/factories/employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ def create_employee(
department='Engineering',
first_name='John',
last_name='Doe',
photo_url=None,
):

if photo_url is None:
photo_url = 'http://example.com/photos/{0}.jpg'.format(username)

return Employee.create_from_dict({
'username': username,
'department': department,
'first_name': first_name,
'last_name': last_name,
'photo_url': 'http://exmaple.com/photos/{0}.jpg'.format(username),
'photo_url': photo_url,
})
24 changes: 24 additions & 0 deletions tests/models/employee_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,27 @@ def test_get_current_employee_raises(self, mock_get_current_user):
def test_full_name(self):
employee = create_employee(first_name='Foo', last_name='Bar')
self.assertEqual('Foo Bar', employee.full_name)

@mock.patch('models.employee.config')
def test_gravatar_backup(self, mock_config):
mock_config.GRAVATAR = 'backup'
employee = create_employee(photo_url='')
self.assertEqual(employee.get_gravatar(), employee.get_photo_url())
employee = create_employee(photo_url='http://example.com/example.jpg')
self.assertEqual(employee.photo_url, employee.get_photo_url())

@mock.patch('models.employee.config')
def test_gravatar_always(self, mock_config):
mock_config.GRAVATAR = 'always'
employee = create_employee(photo_url='')
self.assertEqual(employee.get_gravatar(), employee.get_photo_url())
employee = create_employee(photo_url='http://example.com/example.jpg')
self.assertEqual(employee.get_gravatar(), employee.get_photo_url())

@mock.patch('models.employee.config')
def test_gravatar_disabled(self, mock_config):
mock_config.GRAVATAR = 'disabled'
employee = create_employee(photo_url='')
self.assertEqual(employee.photo_url, employee.get_photo_url())
employee = create_employee(photo_url='http://example.com/example.jpg')
self.assertEqual(employee.photo_url, employee.get_photo_url())
4 changes: 2 additions & 2 deletions themes/default/templates/parts/avatar.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% macro avatar_url_for(employee) -%}
{% if employee.photo_url -%}
{{ employee.photo_url }}
{% if employee.get_photo_url() -%}
{{ employee.get_photo_url() }}
{%- else -%}
{{ theme_static('img/user_medium_square.png', external=True) }}
{%- endif %}
Expand Down

0 comments on commit d375ace

Please sign in to comment.