~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to wlprofile/gravatar.py

  • Committer: kaputtnik
  • Date: 2019-05-30 18:20:02 UTC
  • mto: This revision was merged to the branch mainline in revision 540.
  • Revision ID: kaputtnik-20190530182002-g7l91m1xo28clghv
adjusted README; first commit on the new server

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
try:
2
 
        from hashlib import md5
 
2
    from hashlib import md5
3
3
except ImportError:
4
 
        from md5 import md5
 
4
    from md5 import md5
5
5
import os
6
6
import os.path
7
7
import warnings
8
8
import logging
9
 
import urllib
10
 
import urllib2
 
9
import urllib.request, urllib.parse, urllib.error
 
10
import urllib.request, urllib.error, urllib.parse
11
11
from datetime import datetime, timedelta
12
12
import shutil
13
13
import socket
18
18
 
19
19
warnings.filterwarnings('ignore', r'tmpnam')
20
20
 
 
21
 
21
22
def fetch_gravatar(email):
22
 
    """
23
 
    Fetch avatar from gravatar.com service.
 
23
    """Fetch avatar from gravatar.com service.
24
24
 
25
25
    Return None if avatar was not found.
 
26
 
26
27
    """
27
28
 
28
29
    hash = md5(email).hexdigest()
29
30
    size = max(pybb_settings.AVATAR_WIDTH, pybb_settings.AVATAR_HEIGHT)
30
 
    default = urllib.quote('http://spam.egg/')
 
31
    default = urllib.parse.quote('http://spam.egg/')
31
32
 
32
33
    url = 'http://www.gravatar.com/avatar/%s?s=%d&d=%s' % (hash, size, default)
33
34
    fname = os.tmpnam()
34
35
 
35
 
    class RedirectHandler(urllib2.HTTPRedirectHandler):
 
36
    class RedirectHandler(urllib.request.HTTPRedirectHandler):
 
37
 
36
38
        def http_error_302(*args):
37
39
            raise IOError('Redirect found')
38
40
 
39
41
    timeout = socket.getdefaulttimeout()
40
42
    socket.setdefaulttimeout(10)
41
 
    opener = urllib2.build_opener(RedirectHandler())
 
43
    opener = urllib.request.build_opener(RedirectHandler())
42
44
    socket.setdefaulttimeout(timeout)
43
45
 
44
46
    try:
45
47
        file(fname, 'wb').write(opener.open(url, fname).read())
46
 
    except IOError, ex:
47
 
        #logging.error(ex)
 
48
    except IOError as ex:
 
49
        # logging.error(ex)
48
50
        return None
49
51
    else:
50
52
        return fname
56
58
            if ignore_saved_avatar or not user.pybb_profile.avatar:
57
59
                path = fetch_gravatar(user.email)
58
60
                if path:
59
 
                    avatars_dir = os.path.join(settings.MEDIA_ROOT, pybb_settings.AVATARS_UPLOAD_TO)
 
61
                    avatars_dir = os.path.join(
 
62
                        settings.MEDIA_ROOT, pybb_settings.AVATARS_UPLOAD_TO)
60
63
                    avatar_name = '_pybb_%d' % user.id
61
64
 
62
65
                    avatar_path = os.path.join(avatars_dir, avatar_name)
63
66
                    shutil.copy(path, avatar_path)
64
67
 
65
 
                    relpath = os.path.join(pybb_settings.AVATARS_UPLOAD_TO, avatar_name)
 
68
                    relpath = os.path.join(
 
69
                        pybb_settings.AVATARS_UPLOAD_TO, avatar_name)
66
70
                    user.pybb_profile.avatar = relpath
67
71
                    user.pybb_profile.save()
68
72
                    return True