73 lines
2.7 KiB
Python
73 lines
2.7 KiB
Python
from django import forms
|
|
from django.http import HttpResponseNotFound, HttpResponse, HttpResponseBadRequest
|
|
from django.shortcuts import render, redirect
|
|
from simplemathcaptcha.fields import MathCaptchaField
|
|
|
|
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 = []
|
|
published = Question.objects.filter(is_published=True)
|
|
form = SearchForm(data=request.GET)
|
|
if term:
|
|
if form.is_valid():
|
|
items = Question.get_by_tearchterm(term, published)
|
|
form.term = term
|
|
else:
|
|
return redirect('/')
|
|
else:
|
|
items = published.all()[:10]
|
|
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'],
|
|
)
|
|
messages.info(request, f"Die Frage wurde eingereicht")
|
|
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})
|
|
|
|
|
|
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')
|