~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to wlprofile/gravatar.py

  • Committer: franku
  • Date: 2016-12-13 18:28:51 UTC
  • mto: This revision was merged to the branch mainline in revision 443.
  • Revision ID: somal@arcor.de-20161213182851-bo5ebf8pdvw5beua
run the script

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
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()
33
34
    fname = os.tmpnam()
34
35
 
35
36
    class RedirectHandler(urllib2.HTTPRedirectHandler):
 
37
 
36
38
        def http_error_302(*args):
37
39
            raise IOError('Redirect found')
38
40
 
44
46
    try:
45
47
        file(fname, 'wb').write(opener.open(url, fname).read())
46
48
    except IOError, ex:
47
 
        #logging.error(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