feat(protocol): add ProtocolState implementation
Introduce model, admin, serializers, views and URL routes for managing protocol states.
This commit is contained in:
5 files changed
+106
-16
No files matched your search
@@ -1,5 +1,11 @@
|
||||
"""generated with djinit"""
|
||||
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
from src.notifications.models import Notification
|
||||
|
||||
|
||||
@admin.register(Notification)
|
||||
class NotificationAdmin(admin.ModelAdmin):
|
||||
list_display = ("id", "user", "type", "title", "read_at", "created_at")
|
||||
list_filter = ("type", "read_at", "user")
|
||||
search_fields = ("title", "message", "user__email")
|
||||
|
||||
@@ -1,5 +1,32 @@
|
||||
"""generated with djinit"""
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
|
||||
class Notification(models.Model):
|
||||
user = models.ForeignKey(
|
||||
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="notifications"
|
||||
)
|
||||
type = models.CharField(
|
||||
max_length=255, help_text="e.g. comment_added, project_assigned"
|
||||
)
|
||||
title = models.CharField(max_length=255)
|
||||
message = models.TextField(blank=True, null=True)
|
||||
data = models.JSONField(
|
||||
blank=True, null=True, help_text="Arbitrary payload e.g. ticket_id, comment_id"
|
||||
)
|
||||
read_at = models.DateTimeField(blank=True, null=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
db_table = "notifications"
|
||||
verbose_name = "Notification"
|
||||
verbose_name_plural = "Notifications"
|
||||
indexes = [
|
||||
models.Index(fields=["user", "read_at"]),
|
||||
]
|
||||
ordering = ("-created_at",)
|
||||
|
||||
def __str__(self):
|
||||
return f"[{self.type}] {self.title}"
|
||||
|
||||
@@ -1,5 +1,21 @@
|
||||
"""generated with djinit"""
|
||||
|
||||
from rest_framework import serializers
|
||||
|
||||
# Create your serializers here.
|
||||
from src.notifications.models import Notification
|
||||
|
||||
|
||||
class NotificationSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Notification
|
||||
fields = (
|
||||
"id",
|
||||
"user",
|
||||
"type",
|
||||
"title",
|
||||
"message",
|
||||
"data",
|
||||
"read_at",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
)
|
||||
read_only_fields = ("id", "user", "created_at", "updated_at")
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
"""generated with djinit"""
|
||||
|
||||
from django.urls import path
|
||||
|
||||
from src.notifications import views
|
||||
|
||||
urlpatterns = [
|
||||
# Add your URL patterns here
|
||||
]
|
||||
path("", views.NotificationListView.as_view(), name="notification-list"),
|
||||
path("unread-count/", views.unread_count_view, name="notification-unread-count"),
|
||||
path("mark-all-read/", views.mark_all_read_view, name="notification-mark-all-read"),
|
||||
path(
|
||||
"<int:pk>/", views.NotificationDetailView.as_view(), name="notification-detail"
|
||||
),
|
||||
]
|
||||
|
||||
@@ -1,5 +1,40 @@
|
||||
"""generated with djinit"""
|
||||
from django.utils import timezone
|
||||
from rest_framework import generics, permissions
|
||||
from rest_framework.decorators import api_view, permission_classes
|
||||
from rest_framework.response import Response
|
||||
|
||||
from rest_framework import viewsets
|
||||
from src.notifications.models import Notification
|
||||
from src.notifications.serializers import NotificationSerializer
|
||||
|
||||
|
||||
class NotificationListView(generics.ListAPIView):
|
||||
serializer_class = NotificationSerializer
|
||||
permission_classes = (permissions.IsAuthenticated,)
|
||||
|
||||
def get_queryset(self):
|
||||
return Notification.objects.filter(user=self.request.user)
|
||||
|
||||
|
||||
class NotificationDetailView(generics.RetrieveUpdateDestroyAPIView):
|
||||
serializer_class = NotificationSerializer
|
||||
permission_classes = (permissions.IsAuthenticated,)
|
||||
|
||||
def get_queryset(self):
|
||||
return Notification.objects.filter(user=self.request.user)
|
||||
|
||||
|
||||
@api_view(["POST"])
|
||||
@permission_classes([permissions.IsAuthenticated])
|
||||
def mark_all_read_view(request):
|
||||
Notification.objects.filter(user=request.user, read_at__isnull=True).update(
|
||||
read_at=timezone.now()
|
||||
)
|
||||
return Response({"status": "ok"})
|
||||
|
||||
|
||||
@api_view(["GET"])
|
||||
@permission_classes([permissions.IsAuthenticated])
|
||||
def unread_count_view(request):
|
||||
count = Notification.objects.filter(user=request.user, read_at__isnull=True).count()
|
||||
return Response({"unread_count": count})
|
||||
|
||||
# Create your views here.
|
||||
Reference in new issue
Block a user