Refactor: Change upload_avatar_image to accept two user profiles.

In this commit we just change the upload_avatar_image function to accept
two user_profiles acting_user_profile and target_user_profile. Basically
email param is dropped for a target_user_profile so that avatar's could
be moved lateron to user id based storage.
This commit is contained in:
adnrs96 2017-03-02 20:51:46 +05:30 committed by Tim Abbott
parent 9ba0db29e4
commit 9eb47f108c
4 changed files with 30 additions and 27 deletions

View File

@ -103,8 +103,8 @@ class ZulipUploadBackend(object):
# type: (Text, int, Optional[Text], binary_type, UserProfile, Optional[Realm]) -> Text # type: (Text, int, Optional[Text], binary_type, UserProfile, Optional[Realm]) -> Text
raise NotImplementedError() raise NotImplementedError()
def upload_avatar_image(self, user_file, user_profile, email): def upload_avatar_image(self, user_file, acting_user_profile, target_user_profile):
# type: (File, UserProfile, Text) -> None # type: (File, UserProfile, UserProfile) -> None
raise NotImplementedError() raise NotImplementedError()
def delete_message_image(self, path_id): def delete_message_image(self, path_id):
@ -239,18 +239,18 @@ class S3UploadBackend(ZulipUploadBackend):
logging.warning("%s does not exist. Its entry in the database will be removed." % (file_name,)) logging.warning("%s does not exist. Its entry in the database will be removed." % (file_name,))
return False return False
def upload_avatar_image(self, user_file, user_profile, email): def upload_avatar_image(self, user_file, acting_user_profile, target_user_profile):
# type: (File, UserProfile, Text) -> None # type: (File, UserProfile, UserProfile) -> None
content_type = guess_type(user_file.name)[0] content_type = guess_type(user_file.name)[0]
bucket_name = settings.S3_AVATAR_BUCKET bucket_name = settings.S3_AVATAR_BUCKET
s3_file_name = user_avatar_hash(email) s3_file_name = user_avatar_hash(target_user_profile.email)
image_data = user_file.read() image_data = user_file.read()
upload_image_to_s3( upload_image_to_s3(
bucket_name, bucket_name,
s3_file_name + ".original", s3_file_name + ".original",
content_type, content_type,
user_profile, target_user_profile,
image_data, image_data,
) )
@ -260,7 +260,7 @@ class S3UploadBackend(ZulipUploadBackend):
bucket_name, bucket_name,
s3_file_name + "-medium.png", s3_file_name + "-medium.png",
"image/png", "image/png",
user_profile, target_user_profile,
resized_medium resized_medium
) )
@ -269,7 +269,7 @@ class S3UploadBackend(ZulipUploadBackend):
bucket_name, bucket_name,
s3_file_name, s3_file_name,
'image/png', 'image/png',
user_profile, target_user_profile,
resized_data, resized_data,
) )
# See avatar_url in avatar.py for URL. (That code also handles the case # See avatar_url in avatar.py for URL. (That code also handles the case
@ -386,9 +386,9 @@ class LocalUploadBackend(ZulipUploadBackend):
logging.warning("%s does not exist. Its entry in the database will be removed." % (file_name,)) logging.warning("%s does not exist. Its entry in the database will be removed." % (file_name,))
return False return False
def upload_avatar_image(self, user_file, user_profile, email): def upload_avatar_image(self, user_file, acting_user_profile, target_user_profile):
# type: (File, UserProfile, Text) -> None # type: (File, UserProfile, UserProfile) -> None
email_hash = user_avatar_hash(email) email_hash = user_avatar_hash(target_user_profile.email)
image_data = user_file.read() image_data = user_file.read()
write_local_file('avatars', email_hash+'.original', image_data) write_local_file('avatars', email_hash+'.original', image_data)
@ -446,9 +446,9 @@ def delete_message_image(path_id):
# type: (Text) -> bool # type: (Text) -> bool
return upload_backend.delete_message_image(path_id) return upload_backend.delete_message_image(path_id)
def upload_avatar_image(user_file, user_profile, email): def upload_avatar_image(user_file, acting_user_profile, target_user_profile):
# type: (File, UserProfile, Text) -> None # type: (File, UserProfile, UserProfile) -> None
upload_backend.upload_avatar_image(user_file, user_profile, email) upload_backend.upload_avatar_image(user_file, acting_user_profile, target_user_profile)
def upload_icon_image(user_file, user_profile): def upload_icon_image(user_file, user_profile):
# type: (File, UserProfile) -> None # type: (File, UserProfile) -> None

View File

@ -37,16 +37,18 @@ for both email addresses."""
try: try:
user_profile = get_user_profile_by_email(old_email) user_profile = get_user_profile_by_email(old_email)
upload_avatar_image(gravatar_file, user_profile, user_profile)
user_profile.avatar_source = UserProfile.AVATAR_FROM_USER
user_profile.save(update_fields=['avatar_source'])
except UserProfile.DoesNotExist: except UserProfile.DoesNotExist:
try: raise CommandError("Could not find specified user for email %s" % (old_email))
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: if old_email != new_email:
gravatar_file.seek(0) gravatar_file.seek(0)
upload_avatar_image(gravatar_file, user_profile, new_email) try:
user_profile = get_user_profile_by_email(new_email)
user_profile.avatar_source = UserProfile.AVATAR_FROM_USER upload_avatar_image(gravatar_file, user_profile, user_profile)
user_profile.save(update_fields=['avatar_source']) user_profile.avatar_source = UserProfile.AVATAR_FROM_USER
user_profile.save(update_fields=['avatar_source'])
except UserProfile.DoesNotExist:
raise CommandError("Could not find specified user for email %s" % (new_email))

View File

@ -260,7 +260,7 @@ def set_avatar_backend(request, user_profile):
return json_error(_("You must upload exactly one avatar.")) return json_error(_("You must upload exactly one avatar."))
user_file = list(request.FILES.values())[0] user_file = list(request.FILES.values())[0]
upload_avatar_image(user_file, user_profile, user_profile.email) upload_avatar_image(user_file, user_profile, user_profile)
do_change_avatar_fields(user_profile, UserProfile.AVATAR_FROM_USER) do_change_avatar_fields(user_profile, UserProfile.AVATAR_FROM_USER)
user_avatar_url = avatar_url(user_profile) user_avatar_url = avatar_url(user_profile)

View File

@ -192,7 +192,7 @@ def patch_bot_backend(request, user_profile, email,
pass pass
elif len(request.FILES) == 1: elif len(request.FILES) == 1:
user_file = list(request.FILES.values())[0] user_file = list(request.FILES.values())[0]
upload_avatar_image(user_file, user_profile, bot.email) upload_avatar_image(user_file, user_profile, bot)
avatar_source = UserProfile.AVATAR_FROM_USER avatar_source = UserProfile.AVATAR_FROM_USER
do_change_avatar_fields(bot, avatar_source) do_change_avatar_fields(bot, avatar_source)
else: else:
@ -255,8 +255,6 @@ def add_bot_backend(request, user_profile, full_name_raw=REQ("full_name"), short
elif len(request.FILES) != 1: elif len(request.FILES) != 1:
return json_error(_("You may only upload one file at a time")) return json_error(_("You may only upload one file at a time"))
else: else:
user_file = list(request.FILES.values())[0]
upload_avatar_image(user_file, user_profile, email)
avatar_source = UserProfile.AVATAR_FROM_USER avatar_source = UserProfile.AVATAR_FROM_USER
default_sending_stream = None default_sending_stream = None
@ -278,6 +276,9 @@ def add_bot_backend(request, user_profile, full_name_raw=REQ("full_name"), short
default_sending_stream=default_sending_stream, default_sending_stream=default_sending_stream,
default_events_register_stream=default_events_register_stream, default_events_register_stream=default_events_register_stream,
default_all_public_streams=default_all_public_streams) default_all_public_streams=default_all_public_streams)
if len(request.FILES) == 1:
user_file = list(request.FILES.values())[0]
upload_avatar_image(user_file, user_profile, bot_profile)
json_result = dict( json_result = dict(
api_key=bot_profile.api_key, api_key=bot_profile.api_key,
avatar_url=avatar_url(bot_profile), avatar_url=avatar_url(bot_profile),