From 464899f7d380d504ed82c96d0ef6fa1ab454a424 Mon Sep 17 00:00:00 2001 From: abutalib-kiwi Date: Fri, 25 Aug 2023 12:26:38 +0530 Subject: [PATCH] added notification for top leaderboard junior, added related celery task, created method to send notification to multiple users --- base/tasks.py | 31 +++++++++++++++++++++++++++++-- celerybeat-schedule | Bin 16384 -> 16384 bytes notifications/constants.py | 30 ++++++++++++++++++++++-------- notifications/utils.py | 33 +++++++++++++++++++++++++++++++++ notifications/views.py | 4 ++-- zod_bank/celery.py | 6 +++++- 6 files changed, 91 insertions(+), 13 deletions(-) diff --git a/base/tasks.py b/base/tasks.py index 2056493..98d0df0 100644 --- a/base/tasks.py +++ b/base/tasks.py @@ -9,12 +9,16 @@ from templated_email import send_templated_mail # django imports from django.conf import settings +from django.db.models import F, Window +from django.db.models.functions.window import Rank # local imports from base.constants import PENDING, IN_PROGRESS, JUNIOR from guardian.models import JuniorTask -from notifications.constants import PENDING_TASK_EXPIRING, IN_PROGRESS_TASK_EXPIRING -from notifications.utils import send_notification +from junior.models import JuniorPoints +from notifications.constants import PENDING_TASK_EXPIRING, IN_PROGRESS_TASK_EXPIRING, NOTIFICATION_DICT, TOP_JUNIOR +from notifications.models import Notification +from notifications.utils import send_notification, get_from_user_details, send_notification_multiple_user @shared_task @@ -56,3 +60,26 @@ def notify_task_expiry(): send_notification(IN_PROGRESS_TASK_EXPIRING, task.junior.auth.id, JUNIOR, task.guardian.user.id, {'task_id': task.id}) return True + + +@shared_task() +def notify_top_junior(): + """ + task to send notification for top leaderboard junior to all junior's + :return: + """ + junior_points_qs = JuniorPoints.objects.prefetch_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') + + prev_top_position = junior_points_qs.filter(position=1).first() + new_top_position = junior_points_qs.filter(rank=1).first() + if prev_top_position != new_top_position: + to_user_list = [junior_point.junior.auth for junior_point in junior_points_qs] + send_notification_multiple_user(TOP_JUNIOR, new_top_position.junior.auth.id, JUNIOR, + to_user_list, {'points': new_top_position.total_points}) + for junior_point in junior_points_qs: + junior_point.position = junior_point.rank + junior_point.save() + return True diff --git a/celerybeat-schedule b/celerybeat-schedule index 49e872897d5b39cf5fa0b1af41e7e7fe192562d5..4ebacd4aa0047e3c5989eee717063f096d44e18e 100644 GIT binary patch delta 586 zcmZo@U~Fh$oS?(VHc?l5GKYeK)J#SOFc33>&+4a!_@emJog?lwWQO;a_Ki(3+N;lN8qR^0`|>`0PMRlWDV@-fND@>y5dX_?RZ| zHf~{;WK&gVSgSaBtx+H&^W^_VA(P*j*a(0{ALvcV5N(?hG{u`^@)I-V=pMnm{F2PH z%J`D}g7~b`yv+QfDH$vp8Ad%aNr}a&dL@a)*~NNT)NnX+u|MYko4N2a%uI$1DUBXx zEq%i&J&XpEKbT2^UByGHtNbZ;m6j>@{ADm#F;4C=)@70-)fI_2T!BrP*w8XzU^wy! Q<}PlayOcL`ns+h*0OKUD6#xJL delta 120 zcmZo@U~Fh$oS?(VJW*GhYbGND7>F5dT$sQ=afAJ4Mgew#i4h8uITRFBzjHujIn5z- xAP