fixed add junior multiple device notification issue, changed send multiple user notification method, modified add fcm token method

This commit is contained in:
abutalib-kiwi
2023-08-28 15:36:24 +05:30
parent f424c99478
commit 5e17edcf3f
5 changed files with 28 additions and 31 deletions

Binary file not shown.

View File

@ -19,14 +19,13 @@ from junior.models import Junior
from notifications.constants import NOTIFICATION_DICT
from notifications.models import Notification
User = get_user_model()
def register_fcm_token(user_id, registration_id, device_id, device_type):
""" used to register the fcm device token"""
device, _ = FCMDevice.objects.update_or_create(device_id=device_id,
defaults={'user_id': user_id, 'type': device_type,
device, _ = FCMDevice.objects.update_or_create(user_id=user_id,
defaults={'device_id': device_id, 'type': device_type,
'active': True,
'registration_id': registration_id})
return device
@ -93,7 +92,7 @@ def get_notification_data(notification_type, from_user_id, from_user_type, to_us
notification_data['from_user_image'] = from_user_image
notification_data.update(extra_data)
to_user = User.objects.get(id=to_user_id)
to_user = User.objects.filter(id=to_user_id).first()
return notification_data, push_data, from_user, to_user
@ -135,21 +134,18 @@ def send_notification_multiple_user(notification_type, from_user_id, from_user_t
to_user_list = User.objects.filter(junior_profile__is_verified=True, is_superuser=False
).exclude(junior_profile__isnull=True, guardian_profile__isnull=True)
push_data = NOTIFICATION_DICT[notification_type].copy()
notification_data = push_data.copy()
points = extra_data.get('points', None)
from_user_name, from_user_image, from_user = get_from_user_details(from_user_id, from_user_type)
push_data['body'] = push_data['body'].format(from_user=from_user_name, points=points)
notification_data['body'] = notification_data['body'].format(from_user=from_user_name,
points=points)
notification_data['from_user'] = from_user_name
notification_data['from_user_image'] = from_user_image
notification_data, push_data, from_user, _ = get_notification_data(notification_type, from_user_id,
from_user_type, None, extra_data)
notification_list = []
for user in to_user_list:
notification_copy_data = notification_data.copy()
notification_copy_data.update(
{'badge': Notification.objects.filter(notification_to=user, is_read=False).count()})
notification_list.append(Notification(notification_type=notification_type,
notification_to=user,
notification_from=from_user,
data=notification_data))
data=notification_copy_data))
Notification.objects.bulk_create(notification_list)
to_user_list = to_user_list.filter(user_notification__push_notification=True)
send_multiple_push(to_user_list, push_data)
@ -183,5 +179,3 @@ def send_notification_to_junior(notification_type, from_user_id, to_user_id, ext
from_user = Guardian.objects.filter(user_id=from_user_id).first()
extra_data['from_user_image'] = from_user.image
send_notification(notification_type, from_user_id, to_user_id, extra_data)

View File

@ -52,9 +52,11 @@ class NotificationViewSet(viewsets.GenericViewSet):
@action(methods=['get'], detail=False, url_path='test', url_name='test')
def send_test_notification(self, request):
"""
to send test notification
to test send notification, task expiry, top junior
:return:
"""
notify_task_expiry()
notify_top_junior()
send_notification(TEST_NOTIFICATION, None, None, request.auth.payload['user_id'],
{})
return custom_response(SUCCESS_CODE["3000"])
@ -67,12 +69,3 @@ class NotificationViewSet(viewsets.GenericViewSet):
"""
Notification.objects.filter(id__in=request.data.get('id')).update(is_read=True)
return custom_response(SUCCESS_CODE['3039'], response_status=status.HTTP_200_OK)
@action(methods=['get'], url_path='task', url_name='task', detail=False,
permission_classes=[AllowAny])
def task(self, request, *args, **kwargs):
"""
notification list
"""
notify_top_junior()
return custom_response(SUCCESS_CODE['3039'], response_status=status.HTTP_200_OK)

View File

@ -9,7 +9,7 @@ from django.contrib.auth import get_user_model
from account.utils import get_user_full_name
# local imports
from base.constants import USER_TYPE
from base.constants import USER_TYPE, JUNIOR
from junior.models import JuniorPoints, Junior
@ -37,7 +37,7 @@ class JuniorLeaderboardSerializer(serializers.ModelSerializer):
:param obj: junior object
:return: full name
"""
return f"{obj.auth.first_name} {obj.auth.last_name}" if obj.auth.last_name else obj.auth.first_name
return get_user_full_name(obj.auth)
@staticmethod
def get_first_name(obj):
@ -60,6 +60,8 @@ class LeaderboardSerializer(serializers.ModelSerializer):
"""
leaderboard serializer
"""
user_id = serializers.SerializerMethodField()
user_type = serializers.SerializerMethodField()
junior = JuniorLeaderboardSerializer()
rank = serializers.IntegerField()
@ -68,7 +70,15 @@ class LeaderboardSerializer(serializers.ModelSerializer):
meta class
"""
model = JuniorPoints
fields = ('total_points', 'rank', 'junior')
fields = ('user_id', 'user_type', 'total_points', 'rank', 'junior')
@staticmethod
def get_user_id(obj):
return obj.junior.auth.id
@staticmethod
def get_user_type(obj):
return JUNIOR
class UserCSVReportSerializer(serializers.ModelSerializer):

View File

@ -128,7 +128,7 @@ class AnalyticsViewSet(GenericViewSet):
:param request:
:return:
"""
queryset = JuniorPoints.objects.prefetch_related('junior', 'junior__auth').annotate(rank=Window(
queryset = JuniorPoints.objects.select_related('junior', 'junior__auth').annotate(rank=Window(
expression=Rank(),
order_by=[F('total_points').desc(), 'junior__created_at']
)).order_by('-total_points', 'junior__created_at')
@ -199,7 +199,7 @@ class AnalyticsViewSet(GenericViewSet):
# sheet 3 for Juniors Leaderboard and rank
elif sheet_name == 'Juniors Leaderboard':
queryset = JuniorPoints.objects.prefetch_related('junior', 'junior__auth').annotate(rank=Window(
queryset = JuniorPoints.objects.select_related('junior', 'junior__auth').annotate(rank=Window(
expression=Rank(),
order_by=[F('total_points').desc(), 'junior__created_at']
)).order_by('-total_points', 'junior__created_at')[:15]