Files
Tablequizwiki/content/views/public.py
Holger Sielaff 1531b0f6d3 question as csv
2024-07-13 14:38:42 +02:00

27 lines
832 B
Python

from django.http import HttpResponseNotFound, HttpResponse, HttpResponseBadRequest
from django.shortcuts import render
from content.models import Question
def search_question(request):
term = request.GET.get('term')
items = []
published = Question.objects.filter(is_published=True)
if term:
items = Question.get_by_tearchterm(term, published)
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')