diff options
author | Chris Church <chris@ninemoreminutes.com> | 2013-11-20 06:25:09 +0100 |
---|---|---|
committer | Chris Church <chris@ninemoreminutes.com> | 2013-11-20 06:25:09 +0100 |
commit | b73e792b2357f97b26fc724d36d32b0eb38bff8c (patch) | |
tree | 3d92e3462ecd6d2ae5c2f88e18476391f8c4264b | |
parent | AC-654 Add API support for token auth using X-Auth-Token header in addition t... (diff) | |
download | awx-b73e792b2357f97b26fc724d36d32b0eb38bff8c.tar.xz awx-b73e792b2357f97b26fc724d36d32b0eb38bff8c.zip |
Example code for capturing current user from rest framework.
-rw-r--r-- | awx/api/generics.py | 9 | ||||
-rw-r--r-- | awx/main/middleware.py | 3 |
2 files changed, 12 insertions, 0 deletions
diff --git a/awx/api/generics.py b/awx/api/generics.py index 54bbdea2cc..e48c6aa6fa 100644 --- a/awx/api/generics.py +++ b/awx/api/generics.py @@ -70,6 +70,15 @@ def get_view_description(cls, html=False): class APIView(views.APIView): + def initialize_request(self, request, *args, **kwargs): + ''' + Store the Django REST Framework Request object as an attribute on the + normal Django request. + ''' + drf_request = super(APIView, self).initialize_request(request, *args, **kwargs) + request.drf_request = drf_request + return drf_request + def get_authenticate_header(self, request): """ Determine the WWW-Authenticate header to use for 401 responses. Try to diff --git a/awx/main/middleware.py b/awx/main/middleware.py index 858f68d60a..9306213ba0 100644 --- a/awx/main/middleware.py +++ b/awx/main/middleware.py @@ -24,6 +24,9 @@ class ActivityStreamMiddleware(object): post_save.connect(set_actor, sender=ActivityStream, dispatch_uid=self.disp_uid, weak=False) def process_response(self, request, response): + drf_request = getattr(request, 'drf_request', None) + drf_user = getattr(drf_request, 'user', None) + # FIXME: Associate the user above from Django REST framework with instances. post_save.disconnect(dispatch_uid=self.disp_uid) self.finished = True if self.isActivityStreamEvent: |