Skip to content

Commit d375ace

Browse files
authored
Merge pull request #13 from hacsoc/gravatar
Implement Gravatar support behind config variable.
2 parents 4cc26c8 + bd72602 commit d375ace

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

config-example.py

+6
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@
2222
# Name of the S3 bucket used to import employee data from a file named employees.json
2323
# Check out /import/employees.json.example to see how this file should look like.
2424
S3_BUCKET = 'employees'
25+
26+
# When do we use Gravatar? Options are:
27+
# * 'always' - prefers Gravatar over the Employee.photo_url
28+
# * 'backup' - use Gravatar when photo_url is empty
29+
# * anything else - disabled
30+
GRAVATAR = 'backup'

models/employee.py

+20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
import base64
3+
import hashlib
24
import functools
35

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

83+
def get_gravatar(self):
84+
"""Creates gravatar URL from email address."""
85+
m = hashlib.md5()
86+
m.update(self.user.email())
87+
encoded_hash = base64.b16encode(m.digest()).lower()
88+
return '//gravatar.com/avatar/{}?s=200'.format(encoded_hash)
89+
90+
def get_photo_url(self):
91+
"""Return an avatar photo URL (depending on Gravatar config). This still could
92+
be empty, in which case the theme needs to provide an alternate photo.
93+
"""
94+
if config.GRAVATAR == 'always':
95+
return self.get_gravatar()
96+
elif config.GRAVATAR == 'backup' and not self.photo_url:
97+
return self.get_gravatar()
98+
else:
99+
return self.photo_url
100+
81101
@property
82102
def full_name(self):
83103
"""Return user's full name (first name + ' ' + last name)."""

testing/factories/employee.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ def create_employee(
77
department='Engineering',
88
first_name='John',
99
last_name='Doe',
10+
photo_url=None,
1011
):
1112

13+
if photo_url is None:
14+
photo_url = 'http://example.com/photos/{0}.jpg'.format(username)
15+
1216
return Employee.create_from_dict({
1317
'username': username,
1418
'department': department,
1519
'first_name': first_name,
1620
'last_name': last_name,
17-
'photo_url': 'http://exmaple.com/photos/{0}.jpg'.format(username),
21+
'photo_url': photo_url,
1822
})

tests/models/employee_test.py

+24
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,27 @@ def test_get_current_employee_raises(self, mock_get_current_user):
4949
def test_full_name(self):
5050
employee = create_employee(first_name='Foo', last_name='Bar')
5151
self.assertEqual('Foo Bar', employee.full_name)
52+
53+
@mock.patch('models.employee.config')
54+
def test_gravatar_backup(self, mock_config):
55+
mock_config.GRAVATAR = 'backup'
56+
employee = create_employee(photo_url='')
57+
self.assertEqual(employee.get_gravatar(), employee.get_photo_url())
58+
employee = create_employee(photo_url='http://example.com/example.jpg')
59+
self.assertEqual(employee.photo_url, employee.get_photo_url())
60+
61+
@mock.patch('models.employee.config')
62+
def test_gravatar_always(self, mock_config):
63+
mock_config.GRAVATAR = 'always'
64+
employee = create_employee(photo_url='')
65+
self.assertEqual(employee.get_gravatar(), employee.get_photo_url())
66+
employee = create_employee(photo_url='http://example.com/example.jpg')
67+
self.assertEqual(employee.get_gravatar(), employee.get_photo_url())
68+
69+
@mock.patch('models.employee.config')
70+
def test_gravatar_disabled(self, mock_config):
71+
mock_config.GRAVATAR = 'disabled'
72+
employee = create_employee(photo_url='')
73+
self.assertEqual(employee.photo_url, employee.get_photo_url())
74+
employee = create_employee(photo_url='http://example.com/example.jpg')
75+
self.assertEqual(employee.photo_url, employee.get_photo_url())

themes/default/templates/parts/avatar.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{% macro avatar_url_for(employee) -%}
2-
{% if employee.photo_url -%}
3-
{{ employee.photo_url }}
2+
{% if employee.get_photo_url() -%}
3+
{{ employee.get_photo_url() }}
44
{%- else -%}
55
{{ theme_static('img/user_medium_square.png', external=True) }}
66
{%- endif %}

0 commit comments

Comments
 (0)