From 2b320f8d57e82ba9363e5856dad8ad1ddfcaae41 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Fri, 14 Jun 2013 14:03:11 -0400 Subject: [PATCH] Allow user to upload an image for their bot's avatar (back end). (imported from commit 34833b61b935f4eec2b23abbb3532aaa58e13fb6) --- zephyr/lib/actions.py | 5 +++-- zephyr/lib/create_user.py | 4 +++- zephyr/views.py | 44 +++++++++++++++++++++++++++++++++------ 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/zephyr/lib/actions.py b/zephyr/lib/actions.py index 175528898b..50b8e3416f 100644 --- a/zephyr/lib/actions.py +++ b/zephyr/lib/actions.py @@ -68,7 +68,8 @@ def log_event(event): log.write(simplejson.dumps(event) + '\n') def do_create_user(email, password, realm, full_name, short_name, - active=True, bot=False, bot_owner=None): + active=True, bot=False, bot_owner=None, + avatar_source=UserProfile.AVATAR_FROM_GRAVATAR): event = {'type': 'user_created', 'timestamp': time.time(), 'full_name': full_name, @@ -81,7 +82,7 @@ def do_create_user(email, password, realm, full_name, short_name, log_event(event) user_profile = create_user(email, password, realm, full_name, short_name, - active, bot, bot_owner) + active, bot, bot_owner, avatar_source) notice = dict(event=dict(type="realm_user", op="add", person=dict(email=user_profile.email, diff --git a/zephyr/lib/create_user.py b/zephyr/lib/create_user.py index d9366cf9d3..21713f8161 100644 --- a/zephyr/lib/create_user.py +++ b/zephyr/lib/create_user.py @@ -46,9 +46,11 @@ def create_user_profile(realm, email, password, active, bot, full_name, short_na return user_profile def create_user(email, password, realm, full_name, short_name, - active=True, bot=False, bot_owner=None): + active=True, bot=False, bot_owner=None, + avatar_source=UserProfile.AVATAR_FROM_GRAVATAR): user_profile = create_user_profile(realm, email, password, active, bot, full_name, short_name, bot_owner) + user_profile.avatar_source = avatar_source user_profile.save() recipient = Recipient.objects.create(type_id=user_profile.id, type=Recipient.PERSONAL) diff --git a/zephyr/views.py b/zephyr/views.py index 0c7d6dbfda..046fb79719 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -46,7 +46,7 @@ from zephyr.decorator import require_post, \ JsonableError, RequestVariableMissingError, get_user_profile_by_email, \ authenticated_rest_api_view, process_as_post, REQ, rate_limit, rate_limit_user from zephyr.lib.query import last_n -from zephyr.lib.avatar import avatar_url +from zephyr.lib.avatar import avatar_url, user_avatar_hash from zephyr.lib.response import json_success, json_error, json_response, json_method_not_allowed from zephyr.lib.timestamp import datetime_to_timestamp from zephyr.lib.cache import cache_with_key, cache_get_many, cache_set_many @@ -1986,14 +1986,46 @@ def json_create_bot(request, user_profile, full_name=REQ, short_name=REQ): except UserProfile.DoesNotExist: pass + if len(request.FILES) == 0: + avatar_source = UserProfile.AVATAR_FROM_GRAVATAR + elif len(request.FILES) != 1: + return json_error("You may only upload one file at a time") + else: + user_file = request.FILES.values()[0] + conn = S3Connection(settings.S3_KEY, settings.S3_SECRET_KEY) + key = Key(conn.get_bucket(settings.S3_AVATAR_BUCKET)) + + content_type = guess_type(user_file.name)[0] + key.key = user_avatar_hash(email) + key.set_metadata("user_profile_id", str(user_profile.id)) + if content_type: + headers = {'Content-Type': content_type} + else: + headers = None + key.set_contents_from_filename( + user_file.temporary_file_path(), + headers=headers) + avatar_source = UserProfile.AVATAR_FROM_USER + bot_profile = do_create_user(email, '', user_profile.realm, full_name, - short_name, True, True, user_profile) - return json_success({'api_key': bot_profile.api_key}) + short_name, True, True, + user_profile, avatar_source) + json_result = dict( + api_key=bot_profile.api_key, + avatar_url=avatar_url(bot_profile) + ) + return json_success(json_result) @authenticated_json_post_view def json_get_bots(request, user_profile): bot_profiles = UserProfile.objects.filter(is_bot=True, is_active=True, bot_owner=user_profile) - return json_success({'bots': [{'username': bp.email, 'full_name': bp.full_name, - 'api_key': bp.api_key} - for bp in bot_profiles]}) + def bot_info(bot_profile): + return dict( + username = bot_profile.email, + full_name = bot_profile.full_name, + api_key = bot_profile.api_key, + avatar_url = avatar_url(bot_profile) + ) + + return json_success({'bots': map(bot_info, bot_profiles)})