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.

41 lines
1.3 KiB

9 years ago
9 years ago
9 years ago
  1. # -*- coding: utf-8 -*-
  2. # © 2015 Endika Iglesias
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import base64
  5. import hashlib
  6. import urllib2
  7. from openerp import api, models
  8. from openerp.exceptions import Warning as UserError
  9. from openerp.tools.translate import _
  10. class ResUsers(models.Model):
  11. _inherit = 'res.users'
  12. def _get_gravatar_base64(self, email=''):
  13. url = 'http://www.gravatar.com/avatar/{}?s=200'
  14. _hash = hashlib.md5(email).hexdigest()
  15. try:
  16. res = urllib2.urlopen(url.format(_hash))
  17. raw_image = res.read()
  18. return base64.encodestring(raw_image)
  19. except urllib2.HTTPError:
  20. raise UserError(_('Sorry Gravatar not found.'))
  21. @api.multi
  22. def get_gravatar_image(self):
  23. for rec_id in self:
  24. email = str(rec_id.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. rec_id.write({'image': user_gravatar})
  29. else:
  30. raise UserError(_(
  31. "There is no Gravatar image for this email (%s)" % (
  32. email
  33. )
  34. ))
  35. return True