Commit d6c43522 authored by Gladys Forte's avatar Gladys Forte

notification v2

parent fe520cc9
from django.conf import settings from django.conf import settings
from websocket import create_connection from websocket import create_connection
import json import json
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
REALTIMESERVER_IP = settings.REALTIMESERVER_IP REALTIMESERVER_IP = settings.REALTIMESERVER_IP
...@@ -13,4 +15,24 @@ def send_broadcast_message(room_name, sender, message): ...@@ -13,4 +15,24 @@ def send_broadcast_message(room_name, sender, message):
'message': message 'message': message
} }
ws.send(json.dumps(data)) ws.send(json.dumps(data))
ws.close() ws.close()
\ No newline at end of file
class CustomPagination(PageNumberPagination):
page_size = 10
max_page_size = 50
page_query_param = 'page'
page_size_query_param = 'page_size'
def get_paginated_response(self, data):
return Response({
'page_number': self.page.number,
'size_per_page': self.page.paginator.per_page,
'total_pages': self.page.paginator.num_pages,
'total': self.page.paginator.count,
'totalunseen': data['unseen'],
'code': data['code'],
'status': data['status'],
'message': data['message'],
'results': data['results']
})
\ No newline at end of file
...@@ -14,9 +14,9 @@ from rest_framework import status ...@@ -14,9 +14,9 @@ from rest_framework import status
from django.conf import settings from django.conf import settings
from app.applicationlayer.management.notification.utils_notif import ( from app.applicationlayer.management.notification.utils_notif import (
send_broadcast_message) send_broadcast_message,
from app.applicationlayer.utils import (status_message_response, CustomPagination)
CustomPagination) from app.applicationlayer.utils import (status_message_response)
from rest_framework.exceptions import ValidationError from rest_framework.exceptions import ValidationError
from django.shortcuts import render, redirect, get_object_or_404 from django.shortcuts import render, redirect, get_object_or_404
from rest_framework.decorators import action from rest_framework.decorators import action
...@@ -32,59 +32,59 @@ class NotificationsViewset(meviewsets.ModelViewSet): ...@@ -32,59 +32,59 @@ class NotificationsViewset(meviewsets.ModelViewSet):
pagination_class = CustomPagination pagination_class = CustomPagination
def list(self, request, *args, **kwargs): def list(self, request, *args, **kwargs):
# try: try:
req = self.request req = self.request
account_no = req.query_params.get('account_no') account_no = req.query_params.get('account_no')
app = req.query_params.get('app_code') app = req.query_params.get('app')
if account_no: if account_no:
queryset = models.Notification.objects.filter(
account_no=account_no).order_by('-created')
queryset = self.filter_queryset(queryset)
unseen = models.Notification.objects.filter(
account_no=account_no, is_read=False).count()
if app:
queryset = models.Notification.objects.filter( queryset = models.Notification.objects.filter(
account_no=account_no, app=app).order_by('-created') account_no=account_no).order_by('-created')
queryset = self.filter_queryset(queryset) queryset = self.filter_queryset(queryset)
unseen = models.Notification.objects.filter( unseen = models.Notification.objects.filter(
account_no=account_no, app=app, is_read=False).count() account_no=account_no, is_read=False).count()
else: if app:
queryset = models.Notification.objects.all().order_by('-created')
queryset = self.filter_queryset(queryset) queryset = models.Notification.objects.filter(
unseen = models.Notification.objects.filter( account_no=account_no, app=app).order_by('-created')
is_read=False).count() queryset = self.filter_queryset(queryset)
unseen = models.Notification.objects.filter(
if not queryset: account_no=account_no, app=app, is_read=False).count()
else:
queryset = models.Notification.objects.all().order_by('-created')
queryset = self.filter_queryset(queryset)
unseen = models.Notification.objects.filter(
is_read=False).count()
if not queryset:
message = status_message_response(
200, 'success', 'No records found', []
)
return Response(message)
serializer = self.get_serializer(queryset, many=True)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
message = {
'unseen': unseen,
'code': 200,
'status': 'success',
'message': 'List of Notifications found',
'results': serializer.data
}
return self.get_paginated_response(message)
except Exception as e:
message = status_message_response( message = status_message_response(
200, 'success', 'No records found', [] 500, 'failed',
) 'Request was not able to process' + str(e), [])
return Response(message,
return Response(message) status=status.HTTP_500_INTERNAL_SERVER_ERROR)
serializer = self.get_serializer(queryset, many=True)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
message = {
'unseen': unseen,
'code': 200,
'status': 'success',
'message': 'List of Notifications found',
'results': serializer.data
}
return self.get_paginated_response(message)
# return Response(serializer.data)
# except Exception as e:
# message = status_message_response(
# 500, 'failed',
# 'Request was not able to process' + str(e), [])
# return Response(message,
# status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@action(methods=["PATCH"], detail=True) @action(methods=["PATCH"], detail=True)
...@@ -96,8 +96,7 @@ class NotificationsViewset(meviewsets.ModelViewSet): ...@@ -96,8 +96,7 @@ class NotificationsViewset(meviewsets.ModelViewSet):
models.Notification.objects.filter(account_no=account_no, models.Notification.objects.filter(account_no=account_no,
id__in=request.data['ids']).update( id__in=request.data['ids']).update(
is_read=True, is_read=True,
modified=datetime.now(), modified=datetime.now())
modifiedby=account_no)
message = status_message_response( message = status_message_response(
200, 'success', 200, 'success',
...@@ -124,8 +123,7 @@ class NotificationsViewset(meviewsets.ModelViewSet): ...@@ -124,8 +123,7 @@ class NotificationsViewset(meviewsets.ModelViewSet):
models.Notification.objects.filter(account_no=account_no).update( models.Notification.objects.filter(account_no=account_no).update(
is_read=True, is_read=True,
modified=datetime.now(), modified=datetime.now())
modifiedby=account_no)
message = status_message_response( message = status_message_response(
200, 'success', 200, 'success',
...@@ -147,9 +145,7 @@ class NotificationsViewset(meviewsets.ModelViewSet): ...@@ -147,9 +145,7 @@ class NotificationsViewset(meviewsets.ModelViewSet):
serializer = serializers.NotificationSerializer(data=request.data) serializer = serializers.NotificationSerializer(data=request.data)
try: try:
if serializer.is_valid(): if serializer.is_valid():
x = serializer.save() serializer.save()
x.created = datetime.now()
x.save()
message = { message = {
'code': 201, 'code': 201,
...@@ -159,7 +155,6 @@ class NotificationsViewset(meviewsets.ModelViewSet): ...@@ -159,7 +155,6 @@ class NotificationsViewset(meviewsets.ModelViewSet):
} }
ROOM = serializer.data['account_no'] ROOM = serializer.data['account_no']
SENDER = serializer.data['sender_account_no'] SENDER = serializer.data['sender_account_no']
send_broadcast_message( send_broadcast_message(
......
...@@ -27,3 +27,4 @@ SESSION_TIMEOUT = config['DEV']['SESSION_TIMEOUT'] ...@@ -27,3 +27,4 @@ SESSION_TIMEOUT = config['DEV']['SESSION_TIMEOUT']
FRONT_END_URL = config['DEV']['FRONT_END_URL'] FRONT_END_URL = config['DEV']['FRONT_END_URL']
AUTH_ACCESSS_TOKEN_TIMEOUT = config['DEV']['AUTH_ACCESSS_TOKEN_TIMEOUT'] AUTH_ACCESSS_TOKEN_TIMEOUT = config['DEV']['AUTH_ACCESSS_TOKEN_TIMEOUT']
USER_DEFAULT_PASSWORD = config['DEV']['USER_DEFAULT_PASSWORD'] USER_DEFAULT_PASSWORD = config['DEV']['USER_DEFAULT_PASSWORD']
REALTIMESERVER_IP = config['NOTIFICATION']['REALTIMESERVER_IP']
\ No newline at end of file
...@@ -25,5 +25,4 @@ SESSION_TIMEOUT = config['LOCAL']['SESSION_TIMEOUT'] ...@@ -25,5 +25,4 @@ SESSION_TIMEOUT = config['LOCAL']['SESSION_TIMEOUT']
FRONT_END_URL = config['LOCAL']['FRONT_END_URL'] FRONT_END_URL = config['LOCAL']['FRONT_END_URL']
AUTH_ACCESSS_TOKEN_TIMEOUT = config['LOCAL']['AUTH_ACCESSS_TOKEN_TIMEOUT'] AUTH_ACCESSS_TOKEN_TIMEOUT = config['LOCAL']['AUTH_ACCESSS_TOKEN_TIMEOUT']
USER_DEFAULT_PASSWORD = config['LOCAL']['USER_DEFAULT_PASSWORD'] USER_DEFAULT_PASSWORD = config['LOCAL']['USER_DEFAULT_PASSWORD']
REALTIMESERVER_IP = config['NOTIFICATION']['REALTIMESERVER_IP'] REALTIMESERVER_IP = config['NOTIFICATION']['REALTIMESERVER_IP']
\ No newline at end of file
...@@ -27,3 +27,4 @@ SESSION_TIMEOUT = config['PRODUCTION']['SESSION_TIMEOUT'] ...@@ -27,3 +27,4 @@ SESSION_TIMEOUT = config['PRODUCTION']['SESSION_TIMEOUT']
FRONT_END_URL = config['PRODUCTION']['FRONT_END_URL'] FRONT_END_URL = config['PRODUCTION']['FRONT_END_URL']
AUTH_ACCESSS_TOKEN_TIMEOUT = config['PRODUCTION']['AUTH_ACCESSS_TOKEN_TIMEOUT'] AUTH_ACCESSS_TOKEN_TIMEOUT = config['PRODUCTION']['AUTH_ACCESSS_TOKEN_TIMEOUT']
USER_DEFAULT_PASSWORD = config['PRODUCTION']['USER_DEFAULT_PASSWORD'] USER_DEFAULT_PASSWORD = config['PRODUCTION']['USER_DEFAULT_PASSWORD']
REALTIMESERVER_IP = config['NOTIFICATION']['REALTIMESERVER_IP']
\ No newline at end of file
...@@ -27,3 +27,4 @@ SESSION_TIMEOUT = config['UAT']['SESSION_TIMEOUT'] ...@@ -27,3 +27,4 @@ SESSION_TIMEOUT = config['UAT']['SESSION_TIMEOUT']
FRONT_END_URL = config['UAT']['FRONT_END_URL'] FRONT_END_URL = config['UAT']['FRONT_END_URL']
AUTH_ACCESSS_TOKEN_TIMEOUT = config['UAT']['AUTH_ACCESSS_TOKEN_TIMEOUT'] AUTH_ACCESSS_TOKEN_TIMEOUT = config['UAT']['AUTH_ACCESSS_TOKEN_TIMEOUT']
USER_DEFAULT_PASSWORD = config['UAT']['USER_DEFAULT_PASSWORD'] USER_DEFAULT_PASSWORD = config['UAT']['USER_DEFAULT_PASSWORD']
REALTIMESERVER_IP = config['NOTIFICATION']['REALTIMESERVER_IP']
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment