def get_mugshot_url(self): """ Returns the image containing the mugshot for the user. The mugshot can be a uploaded image or a Gravatar. Gravatar functionality will only be used when ``ACCOUNTS_MUGSHOT_GRAVATAR`` is set to ``True``. :return: ``None`` when Gravatar is not used and no default image is supplied by ``ACCOUNTS_MUGSHOT_DEFAULT``. """ # First check for a mugshot and if any return that. if self.mugshot: return self.mugshot.url # Use Gravatar if the user wants to. if accounts_settings.ACCOUNTS_MUGSHOT_GRAVATAR: return get_gravatar(self.user.email, accounts_settings.ACCOUNTS_MUGSHOT_SIZE, accounts_settings.ACCOUNTS_MUGSHOT_DEFAULT) # Gravatar not used, check for a default image. else: if accounts_settings.ACCOUNTS_MUGSHOT_DEFAULT not in ['404', 'mm', 'identicon', 'monsterid', 'wavatar']: return accounts_settings.ACCOUNTS_MUGSHOT_DEFAULT else: return None
def test_get_gravatar(self): template = 'http://www.gravatar.com/avatar/%(hash)s?s=%(size)s&d=%(type)s' # The hash for [email protected] hash = hashlib.md5('*****@*****.**').hexdigest() # Check the defaults. self.failUnlessEqual(get_gravatar('*****@*****.**'), template % {'hash': hash, 'size': 80, 'type': 'identicon'}) # Check different size self.failUnlessEqual(get_gravatar('*****@*****.**', size=200), template % {'hash': hash, 'size': 200, 'type': 'identicon'}) # Check different default http_404 = get_gravatar('*****@*****.**', default='404') self.failUnlessEqual(http_404, template % {'hash': hash, 'size': 80, 'type': '404'}) # Is it really a 404? response = self.client.get(http_404) self.failUnlessEqual(response.status_code, 404) # Test the switch to HTTPS accounts_settings.ACCOUNTS_MUGSHOT_GRAVATAR_SECURE = True template = 'https://secure.gravatar.com/avatar/%(hash)s?s=%(size)s&d=%(type)s' self.failUnlessEqual(get_gravatar('*****@*****.**'), template % {'hash': hash, 'size': 80, 'type': 'identicon'}) # And set back to default accounts_settings.ACCOUNTS_MUGSHOT_GRAVATAR_SECURE = False