import md5 import os import os path import warnings import logging imp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import md5
import os
import os.path
import warnings
import logging
import urllib
import urllib2
from datetime import datetime, timedelta
import shutil
from pybb import settings as pybb_settings
from django.core.files.uploadedfile import SimpleUploadedFile
from django.conf import settings
warnings.filterwarnings('ignore', r'tmpnam')
def fetch_gravatar(email):
"""
Fetch avatar from gravatar.com service.
Return None if avatar was not found.
"""
hash = md5.new(email).hexdigest()
size = max(pybb_settings.AVATAR_WIDTH, pybb_settings.AVATAR_HEIGHT)
default = urllib.quote('http://spam.egg/')
url = 'http://www.gravatar.com/avatar/%s?s=%d&d=%s' % (hash, size, default)
fname = os.tmpnam()
class RedirectHandler(urllib2.HTTPRedirectHandler):
def http_error_302(*args):
raise IOError('Redirect found')
opener = urllib2.build_opener(RedirectHandler())
try:
file(fname, 'wb').write(opener.open(url, fname).read())
except IOError, ex:
logging.error(ex)
return None
else:
return fname
def check_gravatar(user, ignore_date_joined=False, ignore_saved_avatar=False):
if user.is_active:
if ignore_date_joined or (datetime.now() - user.date_joined) < timedelta(seconds=3):
if ignore_saved_avatar or not user.pybb_profile.avatar:
path = fetch_gravatar(user.email)
if path:
avatars_dir = os.path.join(settings.MEDIA_ROOT, pybb_settings.AVATARS_UPLOAD_TO)
avatar_name = '_pybb_%d' % user.id
avatar_path = os.path.join(avatars_dir, avatar_name)
avatar_BOO = os.path.join(pybb_settings.AVATARS_UPLOAD_TO, avatar_name)
shutil.copy(path, avatar_path)
afile = SimpleUploadedFile('x/y/z', 'bar', 'baz')
user.pybb_profile.avatar = afile
user.pybb_profile.avatar = avatar_BOO
#user.pybb_profile.avatar.path = avatar_path
user.pybb_profile.save()
print user.pybb_profile.avatar