# HG changeset patch # User Marcin Kuzminski # Date 2012-09-13 18:07:43 # Node ID 819eb7f8a5558d75eab814b5a54d1e0b078a5603 # Parent faffec4abbdad6566b17e4df6aae422d956f86d6 Implemented patch from andrewsh ref #565 Add support for {netloc} and {scheme} to alternative_gravatar_url diff --git a/development.ini b/development.ini --- a/development.ini +++ b/development.ini @@ -69,6 +69,8 @@ use_gravatar = true ## {email} user email ## {md5email} md5 hash of the user email (like at gravatar.com) ## {size} size of the image that is expected from the server application +## {scheme} http/https from RhodeCode server +## {netloc} network location from RhodeCode server #alternative_gravatar_url = http://myavatarserver.com/getbyemail/{email}/{size} #alternative_gravatar_url = http://myavatarserver.com/getbymd5/{md5email}?s={size} diff --git a/production.ini b/production.ini --- a/production.ini +++ b/production.ini @@ -69,6 +69,8 @@ use_gravatar = true ## {email} user email ## {md5email} md5 hash of the user email (like at gravatar.com) ## {size} size of the image that is expected from the server application +## {scheme} http/https from RhodeCode server +## {netloc} network location from RhodeCode server #alternative_gravatar_url = http://myavatarserver.com/getbyemail/{email}/{size} #alternative_gravatar_url = http://myavatarserver.com/getbymd5/{md5email}?s={size} diff --git a/rhodecode/config/deployment.ini_tmpl b/rhodecode/config/deployment.ini_tmpl --- a/rhodecode/config/deployment.ini_tmpl +++ b/rhodecode/config/deployment.ini_tmpl @@ -69,6 +69,8 @@ use_gravatar = true ## {email} user email ## {md5email} md5 hash of the user email (like at gravatar.com) ## {size} size of the image that is expected from the server application +## {scheme} http/https from RhodeCode server +## {netloc} network location from RhodeCode server #alternative_gravatar_url = http://myavatarserver.com/getbyemail/{email}/{size} #alternative_gravatar_url = http://myavatarserver.com/getbymd5/{md5email}?s={size} diff --git a/rhodecode/lib/helpers.py b/rhodecode/lib/helpers.py --- a/rhodecode/lib/helpers.py +++ b/rhodecode/lib/helpers.py @@ -10,6 +10,7 @@ import urllib import math import logging import re +import urlparse from datetime import datetime from pygments.formatters.html import HtmlFormatter @@ -711,11 +712,15 @@ HasRepoPermissionAny, HasRepoPermissionA #============================================================================== def gravatar_url(email_address, size=30): + from pylons import url ## doh, we need to re-import url to mock it later if(str2bool(config['app_conf'].get('use_gravatar')) and config['app_conf'].get('alternative_gravatar_url')): tmpl = config['app_conf'].get('alternative_gravatar_url', '') + parsed_url = urlparse.urlparse(url.current(qualified=True)) tmpl = tmpl.replace('{email}', email_address)\ - .replace('{md5email}', hashlib.md5(email_address.lower()).hexdigest())\ + .replace('{md5email}', hashlib.md5(email_address.lower()).hexdigest()) \ + .replace('{netloc}', parsed_url.netloc)\ + .replace('{scheme}', parsed_url.scheme)\ .replace('{size}', str(size)) return tmpl diff --git a/rhodecode/tests/test_libs.py b/rhodecode/tests/test_libs.py --- a/rhodecode/tests/test_libs.py +++ b/rhodecode/tests/test_libs.py @@ -159,19 +159,40 @@ class TestLibs(unittest.TestCase): config['app_conf']['use_gravatar'] = True config['app_conf'].update(kwargs) return config - fake = fake_conf(alternative_gravatar_url='http://test.com/{email}') - with mock.patch('pylons.config', fake): - grav = gravatar_url(email_address='test@foo.com', size=24) - assert grav == 'http://test.com/test@foo.com' + + class fake_url(): + @classmethod + def current(cls, *args, **kwargs): + return 'https://server.com' + + with mock.patch('pylons.url', fake_url): + fake = fake_conf(alternative_gravatar_url='http://test.com/{email}') + with mock.patch('pylons.config', fake): + from pylons import url + assert url.current() == 'http://server.com' + grav = gravatar_url(email_address='test@foo.com', size=24) + assert grav == 'http://test.com/test@foo.com' + + fake = fake_conf(alternative_gravatar_url='http://test.com/{email}') + with mock.patch('pylons.config', fake): + grav = gravatar_url(email_address='test@foo.com', size=24) + assert grav == 'http://test.com/test@foo.com' - fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}') - with mock.patch('pylons.config', fake): - em = 'test@foo.com' - grav = gravatar_url(email_address=em, size=24) - assert grav == 'http://test.com/%s' % (_md5(em)) + fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}') + with mock.patch('pylons.config', fake): + em = 'test@foo.com' + grav = gravatar_url(email_address=em, size=24) + assert grav == 'http://test.com/%s' % (_md5(em)) - fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}/{size}') - with mock.patch('pylons.config', fake): - em = 'test@foo.com' - grav = gravatar_url(email_address=em, size=24) - assert grav == 'http://test.com/%s/%s' % (_md5(em), 24) + fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}/{size}') + with mock.patch('pylons.config', fake): + em = 'test@foo.com' + grav = gravatar_url(email_address=em, size=24) + assert grav == 'http://test.com/%s/%s' % (_md5(em), 24) + + fake = fake_conf(alternative_gravatar_url='{scheme}://{netloc}/{md5email}/{size}') + with mock.patch('pylons.config', fake): + em = 'test@foo.com' + grav = gravatar_url(email_address=em, size=24) + assert grav == 'https://server.com/%s/%s' % (_md5(em), 24) +