gravatar.py 774 B

1234567891011121314151617181920212223242526272829303132
  1. from io import BytesIO
  2. import requests
  3. from PIL import Image
  4. from misago.conf import settings
  5. from . import store
  6. GRAVATAR_URL = 'http://www.gravatar.com/avatar/%s?s=%s&d=404'
  7. class GravatarError(RuntimeError):
  8. pass
  9. class NoGravatarAvailable(RuntimeError):
  10. pass
  11. def set_avatar(user):
  12. url_formats = (user.email_hash, max(settings.MISAGO_AVATARS_SIZES))
  13. try:
  14. r = requests.get(GRAVATAR_URL % url_formats, timeout=5)
  15. if r.status_code != 200:
  16. raise NoGravatarAvailable("gravatar is not available for this e-mail")
  17. image = Image.open(BytesIO(r.content))
  18. store.store_new_avatar(user, image)
  19. except requests.exceptions.RequestException:
  20. raise GravatarError("failed to connect to gravatar servers")