Эх сурвалжийг харах

Event log initial changes

mex3 2 долоо хоног өмнө
parent
commit
dc33a69577

+ 8 - 1
admin.py

@@ -1,7 +1,7 @@
 from django.contrib import admin
 from django.utils.safestring import mark_safe
 
-from tickets.models import Attachment, Comment, TicketList, Ticket, TicketArchive
+from tickets.models import Attachment, Comment, Event, TicketList, Ticket, TicketArchive
 
 
 class TicketListAdmin(admin.ModelAdmin):
@@ -84,6 +84,12 @@ class CommentAdmin(admin.ModelAdmin):
         "snippet"
     )
 
+class EventAdmin(admin.ModelAdmin):
+    list_display = (
+        "author",
+        "date",
+        "snippet"
+    )
 
 class AttachmentAdmin(admin.ModelAdmin):
     list_display = (
@@ -102,4 +108,5 @@ admin.site.register(TicketList, TicketListAdmin)
 admin.site.register(Ticket, TicketAdmin)
 admin.site.register(TicketArchive, TicketArchiveAdmin)
 admin.site.register(Comment, CommentAdmin)
+admin.site.register(Event, EventAdmin)
 admin.site.register(Attachment, AttachmentAdmin)

+ 1 - 0
models/__init__.py

@@ -1,4 +1,5 @@
 from tickets.models.attachment import *
 from tickets.models.comment import *
+from tickets.models.event import *
 from tickets.models.ticket import *
 from tickets.models.ticket_list import *

+ 30 - 0
models/event.py

@@ -0,0 +1,30 @@
+import textwrap
+
+from django.contrib.auth import get_user_model
+from django.db import models
+
+from tickets.models.ticket import Ticket
+
+
+class Event(models.Model):
+    author = models.ForeignKey(
+        get_user_model(), on_delete=models.CASCADE, blank=True, null=True,
+        related_name="tickets_events"
+    )
+    ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE)
+    date = models.DateTimeField(auto_now_add=True)
+
+    body = models.TextField(blank=True)
+
+    def author_text(self):
+        if self.author is not None:
+            return str(self.author)
+
+    @property
+    def snippet(self):
+        body_snippet = textwrap.shorten(self.body, width=35, placeholder="...")
+        # Define here rather than in __str__ so we can use it in the admin list_display
+        return "{author} - {snippet}...".format(author=self.author_text, snippet=body_snippet)
+
+    def __str__(self):
+        return self.snippet

+ 9 - 1
views/ticket_detail.py

@@ -8,7 +8,7 @@ from django.shortcuts import redirect
 from django.views.generic import DetailView
 
 from tickets.forms import TicketForm
-from tickets.models import Attachment, Comment, Ticket
+from tickets.models import Attachment, Comment, Event, Ticket
 from tickets.utils import UserCanReadTicketMixin
 
 
@@ -27,6 +27,9 @@ class TicketDetailView(LoginRequiredMixin, UserCanReadTicketMixin, DetailView):
         context['comment_list'] = Comment.objects.filter(ticket=self.object.pk).order_by("-date").annotate(
             author_username=F("author__username"), author_email=F("author__email")
         )
+        context['event_list'] = Event.objects.filter(ticket=self.object.pk).order_by("-date").annotate(
+            author_username=F("author__username")
+        )
         context['attachments'] = Attachment.objects.filter(ticket=self.object.pk).select_related("added_by")
 
         # List with statuses codes & their names
@@ -50,6 +53,11 @@ class TicketDetailView(LoginRequiredMixin, UserCanReadTicketMixin, DetailView):
                 ticket.status = int(status)
                 ticket.save()
                 messages.success(request, f"Status successfully changed.")
+                Event.objects.create(
+                    author=request.user,
+                    ticket=ticket,
+                    body="Ticket status was changed"
+                )
             else:
                 messages.error(request, f"Status change error.")