You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.3 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. # For copyright and license notices, see __openerp__.py file in root directory
  4. ##############################################################################
  5. from openerp import models, api
  6. from openerp.exceptions import Warning
  7. from openerp.tools.translate import _
  8. import base64
  9. import hashlib
  10. import urllib2
  11. class ResUsers(models.Model):
  12. _inherit = 'res.users'
  13. def _get_gravatar_base64(self, email=''):
  14. url = 'http://www.gravatar.com/avatar/{}?s=200'
  15. _hash = hashlib.md5(email).hexdigest()
  16. try:
  17. res = urllib2.urlopen(url.format(_hash))
  18. raw_image = res.read()
  19. return base64.encodestring(raw_image)
  20. except urllib2.HTTPError:
  21. raise Warning(_('Sorry Gravatar not found.'))
  22. @api.one
  23. def get_gravatar_image(self):
  24. email = str(self.email) or ''
  25. fail_example = self._get_gravatar_base64('fail@email.gravatar')
  26. user_gravatar = self._get_gravatar_base64(email)
  27. if fail_example != user_gravatar:
  28. self.write({'image': user_gravatar})
  29. else:
  30. raise Warning(
  31. _("You don't have Gravatar image to this %s email." % (
  32. email)))
  33. return True