feat: added comment function and new home page

This commit is contained in:
2025-05-03 12:06:04 +02:00
parent 828b39a449
commit 15f980d0b5
5 changed files with 156 additions and 24 deletions

View File

@@ -26,3 +26,12 @@ class Ticket(models.Model):
def __str__(self):
return f"[{self.get_priority_display()}] {self.title} ({self.get_status_display()})"
class Comment(models.Model):
ticket = models.ForeignKey("Ticket", on_delete=models.CASCADE, related_name="comments")
author = models.ForeignKey(User, on_delete=models.CASCADE)
text = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f"Kommentar von {self.author} zu Ticket #{self.ticket.id}"

View File

@@ -31,13 +31,39 @@
font-size: 0.9rem;
color: #666;
}
.comment {
margin: 1rem 0;
padding: 1rem;
border-left: 4px solid #007bff;
background: #f0f4ff;
border-radius: 4px;
}
.comment-form textarea {
width: 100%;
padding: 0.5rem;
border-radius: 4px;
border: 1px solid #ccc;
}
.comment-form button {
margin-top: 0.5rem;
background: #007bff;
color: white;
padding: 0.5rem 1rem;
border: none;
border-radius: 4px;
}
</style>
<div class="ticket-container">
<h1>🎫 Ticket #{{ ticket.id }} {{ ticket.title }}</h1>
<p style="text-align: right;">
<a href="{% url 'modify' ticket.pk %}" style="text-decoration: none; font-weight: bold;">✏️ Dieses Ticket bearbeiten</a>
<a href="{% url 'modify' ticket.pk %}" style="text-decoration: none; font-weight: bold;">✏️ Dieses Ticket
bearbeiten</a>
</p>
<div class="ticket-attribute">
@@ -58,7 +84,7 @@
</div>
<div class="ticket-attribute">
<strong>Bearbeitet von:</strong>
<strong>Zugewiesen an:</strong>
{% if ticket.assigned_to %}
{{ ticket.assigned_to.username }}
{% else %}
@@ -71,4 +97,27 @@
🔄 Aktualisiert: {{ ticket.updated_at|date:"d.m.Y H:i" }}
</div>
</div>
<div class="ticket-container" style="margin-top: 2rem;">
<h2>💬 Kommentare</h2>
{% if ticket.comments.exists %}
{% for comment in ticket.comments.all %}
<div class="comment">
<p><strong>{{ comment.author.username }}</strong> am {{ comment.created_at|date:"d.m.Y H:i" }}</p>
<p>{{ comment.text }}</p>
</div>
{% endfor %}
{% else %}
<p>Keine Kommentare vorhanden.</p>
{% endif %}
{% if user.is_authenticated %}
<h3>📝 Neuen Kommentar schreiben</h3>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Absenden</button>
</form>
{% endif %}
</div>
{% endblock %}

View File

@@ -0,0 +1,44 @@
{% block content %}
<style>
.home-container {
max-width: 800px;
margin: 3rem auto;
text-align: center;
font-family: sans-serif;
}
.home-container h1 {
font-size: 2rem;
margin-bottom: 1rem;
}
.actions {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 1rem;
margin-top: 2rem;
}
.actions a {
padding: 1rem 2rem;
background: #007bff;
color: white;
border-radius: 8px;
text-decoration: none;
font-weight: bold;
transition: background 0.2s ease;
}
.actions a:hover {
background: #0056b3;
}
</style>
<div class="home-container">
<h1>Willkommen im Korrekturmanagementsystem 🎫</h1>
<p>Was möchten Sie tun?</p>
<div class="actions">
<a href="{% url 'ticket-list' %}">📄 Alle Tickets anzeigen</a>
<a href="{% url 'create' %}"> Neues Ticket erstellen</a>
<a href="{% url 'ticket-list' %}?status=open">📂 Offene Tickets</a>
</div>
</div>
{% endblock %}

View File

@@ -1,13 +1,15 @@
from django.urls import path
from . import views
from .views import TicketListView, TicketCreateView, TicketUpdateView
from .views import TicketListView, TicketCreateView, TicketUpdateView, HomeView, TicketDetailView
urlpatterns = [
# /ticketsystem/
path("", TicketListView.as_view(), name="index"),
path("", HomeView.as_view(), name="home"),
# /ticketsystem/tickets
path("tickets", TicketListView.as_view(), name="ticket-list"),
# /ticketsystem/detail/
path("<int:pk>/", views.detail, name="detail"),
path("<int:pk>/", TicketDetailView.as_view(), name="detail"),
# /ticketsystem/new/
path("new/", TicketCreateView.as_view(), name="create"),
path("<int:pk>/modify/", TicketUpdateView.as_view(), name="modify"),

View File

@@ -1,11 +1,19 @@
from django.shortcuts import get_object_or_404, render
from django.views.generic import ListView
from django.views.generic import ListView, TemplateView
from django.views.generic.edit import CreateView, UpdateView
from django.urls import reverse_lazy
from django.views.generic.detail import DetailView
from django.views.generic.edit import FormMixin
from .forms import CommentForm
from django.urls import reverse
from .models import Ticket
class HomeView(TemplateView):
template_name = "ticketsystem/home.html"
class TicketListView(ListView):
model = Ticket
template_name = "ticketsystem/index.html"
@@ -27,10 +35,30 @@ class TicketListView(ListView):
return context
def detail(request, pk):
ticket = get_object_or_404(Ticket, pk=pk)
return render(request, "ticketsystem/detail.html", {"ticket": ticket})
class TicketDetailView(FormMixin, DetailView):
model = Ticket # <- das ist wichtig!
template_name = "ticketsystem/detail.html"
context_object_name = "ticket"
form_class = CommentForm
def get_success_url(self):
return reverse("detail", kwargs={"pk": self.object.pk})
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
comment = form.save(commit=False)
comment.ticket = self.object
comment.author = request.user
comment.save()
return super().form_valid(form)
return self.form_invalid(form)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["form"] = self.get_form()
return context
class TicketCreateView(CreateView):
model = Ticket