feat: added answer field

This commit is contained in:
2025-05-31 01:14:49 +02:00
parent 51bf409e3c
commit aabf84ecc0
8 changed files with 128 additions and 10 deletions

View File

@@ -1,5 +1,6 @@
from django import forms
from .models import Comment, Ticket
from django.core.exceptions import ValidationError
class CommentForm(forms.ModelForm):
@@ -16,7 +17,26 @@ class CommentForm(forms.ModelForm):
class TicketForm(forms.ModelForm):
class Meta:
model = Ticket
fields = ["title", "description", "status", "priority", "course"]
fields = ["title", "description", "status", "priority", "course", "answer"]
widgets = {
'answer': forms.Textarea(attrs={
'rows': 4,
'placeholder': 'Beschreibe die Lösung des Problems...'
})
}
def clean(self):
cleaned_data = super().clean()
status = cleaned_data.get('status')
answer = cleaned_data.get('answer')
# Wenn Status auf "gelöst" gesetzt wird, muss eine Antwort vorhanden sein
if status == 'resolved' and not answer:
raise ValidationError({
'answer': 'Eine Antwort ist erforderlich, wenn der Status auf "Gelöst" gesetzt wird.'
})
return cleaned_data
def save(self, commit=True):
ticket = super().save(commit=False)
@@ -25,6 +45,11 @@ class TicketForm(forms.ModelForm):
if ticket.course and ticket.course.tutor:
ticket.assigned_to = ticket.course.tutor
# Setze answered_at wenn eine Antwort gegeben wird
if ticket.answer and not ticket.answered_at:
from django.utils import timezone
ticket.answered_at = timezone.now()
if commit:
ticket.save()
return ticket