notification system deployment - v2
This commit is contained in:
@@ -29,7 +29,7 @@ class Notification(BaseModel):
|
||||
delivered = models.BooleanField(default=False)
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.organization.name} - {self.title}'
|
||||
return f'{self.organization.name if self.organization else self.id} - {self.title}'
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
return super(Notification, self).save(*args, **kwargs)
|
||||
|
||||
@@ -1,9 +1,54 @@
|
||||
from apps.notification.pos.api.v1.serializers import NotificationSerializer
|
||||
from apps.pos_device.mixins.pos_device_mixin import POSDeviceMixin
|
||||
from apps.core.mixins.soft_delete_mixin import SoftDeleteMixin
|
||||
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||
from apps.notification.models import Notification
|
||||
from rest_framework.permissions import AllowAny
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import viewsets
|
||||
from rest_framework import status
|
||||
from django.db import transaction
|
||||
|
||||
|
||||
class NotificationViewSet(viewsets.ModelViewSet):
|
||||
pass
|
||||
class NotificationViewSet(SoftDeleteMixin, POSDeviceMixin, DynamicSearchMixin, viewsets.ModelViewSet):
|
||||
queryset = Notification.objects.all().select_related('organization')
|
||||
serializer_class = NotificationSerializer
|
||||
permission_classes = [AllowAny]
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
""" show all notification of device """
|
||||
|
||||
organization = self.get_device_organization()
|
||||
queryset = self.queryset.filter(
|
||||
organization=organization,
|
||||
delivered=False,
|
||||
is_read=False
|
||||
)
|
||||
|
||||
# set notifications delivered status to true
|
||||
# queryset.update(delivered=True)
|
||||
|
||||
# paginate & response
|
||||
page = self.paginate_queryset(queryset)
|
||||
if page is not None:
|
||||
serializer = self.get_serializer(page, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
|
||||
@action(
|
||||
methods=['put'],
|
||||
detail=True,
|
||||
url_path='read_notification',
|
||||
url_name='read_notification',
|
||||
name='read_notification',
|
||||
)
|
||||
@transaction.atomic
|
||||
def read_notification(self, request, pk=None):
|
||||
""" read notification by pos organization """
|
||||
|
||||
query = self.get_object()
|
||||
|
||||
query.is_read = True
|
||||
query.save()
|
||||
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
from rest_framework import serializers
|
||||
from apps.notification.models import Notification
|
||||
|
||||
|
||||
class NotificationSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Notification
|
||||
fields = '__all__'
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
from django.urls import path, include
|
||||
from rest_framework.routers import DefaultRouter
|
||||
from .api import NotificationViewSet
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register(r'')
|
||||
router.register(r'notification', NotificationViewSet, basename='notification')
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
|
||||
@@ -2,5 +2,5 @@ from django.urls import path, include
|
||||
|
||||
urlpatterns = [
|
||||
# path('web/', include('apps.notification.web.api.v1.urls')),
|
||||
# path('pos/', include('apps.notification.pos.api.v1.urls'))
|
||||
path('pos/', include('apps.notification.pos.api.v1.urls'))
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user