2013-07-26 04:58:09 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
2013-07-26 04:58:09 +02:00
|
|
|
import requests
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.models import get_user_profile_by_email, UserProfile
|
2013-11-04 17:22:58 +01:00
|
|
|
from zerver.lib.avatar import gravatar_hash
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.upload import upload_avatar_image
|
2013-07-26 04:58:09 +02:00
|
|
|
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."""
|
|
|
|
|
2015-08-21 02:10:41 +02:00
|
|
|
def add_arguments(self, parser):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (ArgumentParser) -> None
|
2015-08-21 02:10:41 +02:00
|
|
|
parser.add_argument('old_email', metavar='<old email>', type=str,
|
|
|
|
help="user whose Gravatar should be migrated")
|
|
|
|
parser.add_argument('new_email', metavar='<new email>', type=str, nargs='?', default=None,
|
|
|
|
help="user to copy the Gravatar to")
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (*Any, **str) -> None
|
2015-08-21 02:10:41 +02:00
|
|
|
old_email = options['old_email']
|
|
|
|
|
|
|
|
if options['new_email']:
|
|
|
|
new_email = options['new_email']
|
|
|
|
else:
|
2013-07-26 04:58:09 +02:00
|
|
|
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'])
|