Add api_key_only_webhook_view decorator.

This is for webhook API endpoints that only get passed in an api_key,
not an email.  An example would be api_jira_webhook, and some of
the code is borrowed from there.  The rest of the code is from
authenticated_api_view().

(imported from commit b5b2a4ea52f9b317f00357ef3142c76534fabf20)
This commit is contained in:
Steve Howell 2013-10-02 19:12:57 -04:00
parent f6794ce849
commit 8a4a069703
1 changed files with 21 additions and 0 deletions

View File

@ -85,6 +85,27 @@ def validate_api_key(email, api_key):
raise JsonableError("User account is not active")
return user_profile
# Use this for webhook views that don't get an email passed in.
def api_key_only_webhook_view(view_func):
@csrf_exempt
@has_request_variables
@wraps(view_func)
def _wrapped_view_func(request, api_key=REQ,
*args, **kwargs):
try:
user_profile = UserProfile.objects.get(api_key=api_key, is_active=True)
except UserProfile.DoesNotExist:
raise JsonableError("Failed to find user with API key: %s" % (api_key,))
request.user = user_profile
request._email = user_profile.email
process_client(request, user_profile, "API")
rate_limit_user(request, user_profile, domain='all')
return view_func(request, user_profile, *args, **kwargs)
return _wrapped_view_func
# authenticated_api_view will add the authenticated user's user_profile to
# the view function's arguments list, since we have to look it up
# anyway.