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.2 KiB

9 years ago
9 years ago
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.one
  22. def get_gravatar_image(self):
  23. email = str(self.email) or ''
  24. fail_example = self._get_gravatar_base64('fail@email.gravatar')
  25. user_gravatar = self._get_gravatar_base64(email)
  26. if fail_example != user_gravatar:
  27. self.write({'image': user_gravatar})
  28. else:
  29. raise UserError(
  30. _("There is no Gravatar image for this email (%s)" % (
  31. email)))
  32. return True