django-commons / drf-excel

An XLSX spreadsheet renderer for Django REST Framework.
BSD 3-Clause "New" or "Revised" License
217 stars 40 forks source link

Crash on empty list #5

Closed askoretskiy closed 5 years ago

askoretskiy commented 5 years ago

When you use given renderer on empty sequence, it raises an exception:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 217, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 215, in _get_response
    response = response.render()
  File "/usr/local/lib/python2.7/site-packages/django/template/response.py", line 107, in render
    self.content = self.rendered_content
  File "/usr/local/lib/python2.7/site-packages/rest_framework/response.py", line 72, in rendered_content
    ret = renderer.render(self.data, accepted_media_type, context)
  File "/usr/local/lib/python2.7/site-packages/drf_renderer_xlsx/renderers.py", line 120, in render
    for column_name in results[0].keys():
IndexError: list index out of range

Please add proper handling there. It should work fine with empty results (then it should show only header).

FlipperPA commented 5 years ago

Fixed here; will be released in about 10 minutes to PyPI: https://github.com/wharton/drf-renderer-xlsx/commit/dde822b1241a0c4166d2b9d0cd0d7aff49af3f0f#diff-643b3dcb780e20c4ead30d169b1ff991R112