question as csv

This commit is contained in:
Holger Sielaff
2024-07-13 14:38:42 +02:00
parent e7b2dff233
commit 1531b0f6d3
6 changed files with 99 additions and 44 deletions

View File

@@ -1,3 +1,4 @@
from django.http import HttpResponseNotFound, HttpResponse, HttpResponseBadRequest
from django.shortcuts import render
from content.models import Question
@@ -12,3 +13,14 @@ def search_question(request):
else:
items = published.all()[:10]
return render(request, 'questions.html', {'items': items})
def as_csv(request, **kwargs):
try:
q = Question.objects.get(id=kwargs['id'])
response = HttpResponse(content_type='text/csv')
return q.to_csv(fh=response)
except KeyError:
return HttpResponseBadRequest('No id given')
except Question.DoesNotExist:
return HttpResponseNotFound('Question not found')