This commit is contained in:
Holger Sielaff
2024-09-10 12:32:53 +02:00
parent 1531b0f6d3
commit be447f4862
9 changed files with 179 additions and 7 deletions

View File

@@ -1,18 +1,64 @@
from django import forms
from django.http import HttpResponseNotFound, HttpResponse, HttpResponseBadRequest
from django.shortcuts import render
from django.shortcuts import render, redirect
from simplemathcaptcha.fields import MathCaptchaField
from content.models import Question
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:
items = Question.get_by_tearchterm(term, published)
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', {'items': items})
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"Angelegt mit ID {subq.id}")
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):