aschn / drf-tracking

Utils to track requests to Django Rest Framework API views
http://drf-tracking.readthedocs.org/
ISC License
367 stars 95 forks source link

AttributeError: 'FileResponse' object has no attribute 'rendered_content' #62

Open simone-chiorazzo opened 7 years ago

simone-chiorazzo commented 7 years ago

Hi,

drf-tracking 1.2.0 crash if you use a DRF APIView that returns a FileResponse. Could you fix this behaviour?

Thanks, Simone


Crash report

Environment: development

Request Method: GET Request URL: http://prm.mydevelopmentenv.com/api/export/files

Django Version: 1.8.18 Python Version: 3.3.5 Installed Applications: ('django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'debug_toolbar', 'django_q', 'rest_framework', 'rest_framework.authtoken', 'rest_framework_tracking', 'maintenancemode', 'prm') Installed Middleware: ('django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'prm.middleware.SetRemoteAddrMiddleware', 'debug_toolbar.middleware.DebugToolbarMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'maintenancemode.middleware.MaintenanceModeMiddleware')

Traceback: File "/home/prm/virtualenv/lib/python3.3/site-packages/django/core/handlers/base.py" in get_response

  1. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/prm/virtualenv/lib/python3.3/site-packages/django/views/decorators/csrf.py" in wrapped_view
  2. return view_func(*args, **kwargs) File "/home/prm/virtualenv/lib/python3.3/site-packages/django/views/generic/base.py" in view
  3. return self.dispatch(request, *args, **kwargs) File "/home/prm/virtualenv/lib/python3.3/site-packages/rest_framework/views.py" in dispatch
  4. self.response = self.finalize_response(request, response, *args, **kwargs) File "/home/prm/virtualenv/lib/python3.3/site-packages/rest_framework_tracking/mixins.py" in finalize_response
  5. self.request.log.response = response.rendered_content

Exception Type: AttributeError at /api/export/files Exception Value: 'FileResponse' object has no attribute 'rendered_content'

avelis commented 7 years ago

@simone-chiorazzo This could solved be changing the call from response.rendered_content to response.getvalue() . This covers any concrete Response class inheriting from django.http.HttpResponse. I would ideally like to have unit tests for this though.

YYJeffrey commented 6 years ago

I also encountered the same problem, when i changed response.rendered_content to response.getvalue(), the mistakes still exist, so i set response field to be '', i hope this will help you.