Files

73 lines
2.7 KiB
Python
Raw Permalink Normal View History

2024-09-10 12:32:53 +02:00
from django import forms
2024-07-13 14:38:42 +02:00
from django.http import HttpResponseNotFound, HttpResponse, HttpResponseBadRequest
2024-09-10 12:32:53 +02:00
from django.shortcuts import render, redirect
from simplemathcaptcha.fields import MathCaptchaField
2024-09-10 12:32:53 +02:00
from content.models import Question, SubmittedQuestion
class SearchForm(forms.Form):
term = forms.CharField(max_length=100)
# captcha = MathCaptchaField()
class SubmitQuestion(forms.Form):
name = forms.CharField(max_length=500, required=True)
buzzword = forms.CharField(max_length=150, required=False)
question = forms.CharField(max_length=100000, widget=forms.Textarea, required=True)
awnser = forms.CharField(max_length=100000, widget=forms.Textarea, required=True)
captcha = MathCaptchaField()
# level = forms.ForeignKey(Level, on_delete=models.SET_NULL, null=True)
# labels = forms.ManyToManyField(Label, blank=True)
# medias = forms.ManyToManyField(MediaFile, blank=True)
# links = forms.ManyToManyField(Link, blank=True)
# shares = forms.ManyToManyField(SharedQuestion, blank=True)
def search_question(request):
term = request.GET.get('term')
items = []
2024-07-13 10:19:49 +02:00
published = Question.objects.filter(is_published=True)
2024-09-10 12:32:53 +02:00
form = SearchForm(data=request.GET)
if term:
2024-09-10 12:32:53 +02:00
if form.is_valid():
items = Question.get_by_tearchterm(term, published)
form.term = term
else:
return redirect('/')
else:
2024-07-13 10:19:49 +02:00
items = published.all()[:10]
2024-09-10 12:32:53 +02:00
return render(request, 'questions.html', {'form': SearchForm(), 'items': items})
from django.contrib import messages
def submit_question(request):
if request.method == 'POST':
form = SubmitQuestion(data=request.POST)
if form.is_valid():
subq = SubmittedQuestion.objects.create(
name=form.cleaned_data['name'],
buzzword=form.cleaned_data['buzzword'],
question=form.cleaned_data['question'],
awnser=form.cleaned_data['awnser'],
)
2024-09-21 12:33:00 +02:00
messages.info(request, f"Die Frage wurde eingereicht")
2024-09-10 12:32:53 +02:00
return render(request, 'submit-question.html', {'form': SubmitQuestion()})
else:
[messages.error(request,f'Error in {err}') for err in form.errors]
return render(request, 'submit-question.html', {'form': form})
else:
form = SubmitQuestion()
return render(request, 'submit-question.html', {'form': form})
2024-07-13 14:38:42 +02:00
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')