decorators: Pass the parsed client to view functions as request._client.

(imported from commit e297cfc9c2c1c4d07f083a28ab3d85d32a8bc85b)
This commit is contained in:
Tim Abbott 2012-11-27 14:44:58 -05:00
parent d908b7299c
commit a6841c7f12
2 changed files with 16 additions and 19 deletions

View File

@ -66,8 +66,8 @@ def authenticated_api_view(view_func):
return json_error("Invalid user: %s" % (email,)) return json_error("Invalid user: %s" % (email,))
if api_key != user_profile.api_key: if api_key != user_profile.api_key:
return json_error("Invalid API key for user '%s'" % (email,)) return json_error("Invalid API key for user '%s'" % (email,))
update_user_activity(request, user_profile, request._client = parse_client(request, "API")
parse_client(request, "API")) update_user_activity(request, user_profile, request._client)
return view_func(request, user_profile, *args, **kwargs) return view_func(request, user_profile, *args, **kwargs)
return _wrapped_view_func return _wrapped_view_func
@ -80,9 +80,10 @@ def authenticated_json_view(view_func):
def _wrapped_view_func(request, *args, **kwargs): def _wrapped_view_func(request, *args, **kwargs):
if not request.user.is_authenticated(): if not request.user.is_authenticated():
return json_error("Not logged in", status=401) return json_error("Not logged in", status=401)
update_user_activity(request, request.user.userprofile, request._client = parse_client(request, "website")
parse_client(request, "website")) user_profile = request.user.userprofile
return view_func(request, request.user.userprofile, *args, **kwargs) update_user_activity(request, user_profile, request._client)
return view_func(request, user_profile, *args, **kwargs)
return _wrapped_view_func return _wrapped_view_func
# These views are used by the main Django server to notify the Tornado server # These views are used by the main Django server to notify the Tornado server

View File

@ -469,16 +469,12 @@ def api_get_profile(request, user_profile):
return json_success(result) return json_success(result)
@authenticated_api_view @authenticated_api_view
@has_request_variables def api_send_message(request, user_profile):
def api_send_message(request, user_profile, return send_message_backend(request, user_profile, request._client)
client_name=POST("client", default="API")):
return send_message_backend(request, user_profile, client_name)
@authenticated_json_view @authenticated_json_view
@has_request_variables def json_send_message(request, user_profile):
def json_send_message(request, user_profile, return send_message_backend(request, user_profile, request._client)
client_name=POST("client", default="website")):
return send_message_backend(request, user_profile, client_name)
# Currently tabbott/extra@mit.edu is our only superuser. TODO: Make # Currently tabbott/extra@mit.edu is our only superuser. TODO: Make
# this a real superuser security check. # this a real superuser security check.
@ -554,7 +550,7 @@ def create_mirrored_message_users(request, user_profile, recipients):
# send_message_backend should either check the API key or check that # send_message_backend should either check the API key or check that
# the user is logged in. # the user is logged in.
@has_request_variables @has_request_variables
def send_message_backend(request, user_profile, client_name, def send_message_backend(request, user_profile, client,
message_type_name = POST('type'), message_type_name = POST('type'),
message_to = POST('to', converter=extract_recipients), message_to = POST('to', converter=extract_recipients),
forged = POST(default=False), forged = POST(default=False),
@ -567,7 +563,7 @@ def send_message_backend(request, user_profile, client_name,
if len(message_to) == 0: if len(message_to) == 0:
return json_error("Message must have recipients.") return json_error("Message must have recipients.")
if client_name == "zephyr_mirror": if client.name == "zephyr_mirror":
# Here's how security works for non-superuser mirroring: # Here's how security works for non-superuser mirroring:
# #
# The message must be (1) a private message (2) that # The message must be (1) a private message (2) that
@ -626,7 +622,7 @@ def send_message_backend(request, user_profile, client_name,
except UserProfile.DoesNotExist: except UserProfile.DoesNotExist:
return json_error("Invalid email '%s'" % (email,)) return json_error("Invalid email '%s'" % (email,))
if client_name == "zephyr_mirror": if client.name == "zephyr_mirror":
if user_profile.id not in recipient_profile_ids and not forged: if user_profile.id not in recipient_profile_ids and not forged:
return json_error("User not authorized for this query") return json_error("User not authorized for this query")
@ -658,9 +654,9 @@ def send_message_backend(request, user_profile, client_name,
message.pub_date = datetime.datetime.utcfromtimestamp(float(request.POST['time'])).replace(tzinfo=utc) message.pub_date = datetime.datetime.utcfromtimestamp(float(request.POST['time'])).replace(tzinfo=utc)
else: else:
message.pub_date = now() message.pub_date = now()
message.sending_client = get_client(client_name) message.sending_client = client
if client_name == "zephyr_mirror" and already_sent_mirrored_message(message): if client.name == "zephyr_mirror" and already_sent_mirrored_message(message):
return json_success() return json_success()
do_send_message(message) do_send_message(message)
@ -921,7 +917,7 @@ def api_github_landing(request, user_profile, event=POST,
if len(subject) > 60: if len(subject) > 60:
subject = subject[:57].rstrip() + '...' subject = subject[:57].rstrip() + '...'
return send_message_backend(request, user_profile, "github_bot", return send_message_backend(request, user_profile, get_client("github_bot"),
message_type_name="stream", message_type_name="stream",
message_to=["commits"], message_to=["commits"],
forged=False, subject_name=subject, forged=False, subject_name=subject,