Add tool for migrating users from Gravatar to user avatars

(imported from commit e3fbeb74489952f00f9063303f4026ee6a7148fc)
This commit is contained in:
Zev Benjamin 2013-07-25 22:58:09 -04:00 committed by Tim Abbott
parent 91b05ffe88
commit f7b7f074e9
2 changed files with 49 additions and 3 deletions

View File

@ -52,9 +52,8 @@ def upload_image_to_s3(
else: else:
headers = None headers = None
key.set_contents_from_filename( contents = user_file.read()
user_file.temporary_file_path(), key.set_contents_from_string(contents, headers=headers)
headers=headers)
def get_file_info(request, user_file): def get_file_info(request, user_file):
uploaded_file_name = user_file.name uploaded_file_name = user_file.name

View File

@ -0,0 +1,47 @@
from __future__ import absolute_import
import sys
import requests
from zephyr.models import get_user_profile_by_email, UserProfile
from zephyr.lib.avatar import gravatar_hash, user_avatar_hash
from zephyr.lib.upload import upload_avatar_image
from django.core.management.base import BaseCommand, CommandError
from django.core.files.uploadedfile import SimpleUploadedFile
class Command(BaseCommand):
help = """Migrate the specified user's Gravatar over to an avatar that we serve. If two
email addresses are specified, use the Gravatar for the first and upload the image
for both email addresses."""
def handle(self, *args, **kwargs):
if len(args) == 0:
raise CommandError("You must specify a user")
if len(args) > 2:
raise CommandError("Too many positional arguments")
old_email = args[0]
if len(args) == 2:
new_email = args[1]
elif len(args) == 1:
new_email = old_email
gravatar_url = "https://secure.gravatar.com/avatar/%s?d=identicon" % (gravatar_hash(old_email),)
gravatar_data = requests.get(gravatar_url).content
gravatar_file = SimpleUploadedFile('gravatar.jpg', gravatar_data, 'image/jpeg')
try:
user_profile = get_user_profile_by_email(old_email)
except UserProfile.DoesNotExist:
try:
user_profile = get_user_profile_by_email(new_email)
except UserProfile.DoesNotExist:
raise CommandError("Could not find specified user")
upload_avatar_image(gravatar_file, user_profile, old_email)
if old_email != new_email:
gravatar_file.seek(0)
upload_avatar_image(gravatar_file, user_profile, new_email)
user_profile.avatar_source = UserProfile.AVATAR_FROM_USER
user_profile.save(update_fields=['avatar_source'])