From 2b6e943d8c15e23642861e200415ae3295f55d7e Mon Sep 17 00:00:00 2001 From: jain Date: Wed, 26 Jul 2023 16:38:36 +0530 Subject: [PATCH 1/2] jira-28 notification --- base/messages.py | 2 ++ celerybeat-schedule | Bin 16384 -> 16384 bytes guardian/serializers.py | 56 ++++++++++++++++++++----------------- guardian/utils.py | 6 ++++ guardian/views.py | 10 +++++-- junior/serializers.py | 13 +++++++-- junior/views.py | 5 ++++ notifications/constants.py | 43 +++++++++++++++++++++++++++- 8 files changed, 103 insertions(+), 32 deletions(-) diff --git a/base/messages.py b/base/messages.py index a6a24c9..f99f9cb 100644 --- a/base/messages.py +++ b/base/messages.py @@ -87,6 +87,8 @@ ERROR_CODE = { # task status "2060": "Task does not exist or not in pending state", "2061": "Please insert image or check the image is valid or not.", + # email not null + "2062": "Please enter email address" } """Success message code""" SUCCESS_CODE = { diff --git a/celerybeat-schedule b/celerybeat-schedule index 2fd0ce139fd0ddb41e3d63d94701251cc695247b..573b0c9bd5140b34794915695cfc78142e742fee 100644 GIT binary patch delta 88 zcmZo@U~Fh$T%f?nG?`IBoQso>0SvwyPBiq|oTIRWm)F$*BE$})R&SQo+r-2!#bwFI fU12@>nn5KqgEzzEAVWWhSY_Sh<%R(p4LSJ$N2e3m delta 423 zcmX|-%}&BV6osjvNTn*MASjCeU<|fFHZI%{NKB(K6HOX71cuQzt%VEIL`)jGk-g~) zxG>SJurn@v1vkEekKooRMCUHf$;nK2 zI55&rg771aq9Bv^K`PfD3nqk2K+r_@OYHcwvfv`=bd>1@_gOk~OX87>#3*Cji~HRG zu2(6c+703V4U8B0;5U1e_4z&pn7}nlGpJ^^`D;R}l(u*X;=6`PH1n##Uovy-8JJW; ztVYE$n;O;)0-+x82&NQ2OE1og5SFR3iiviU2|wxAbaNoBNU z1S#34MlwT&2rEN9fK`vB?lD{%!I};09&W>iO~u*kC!%uZ3H`3?o}#> Fe*yUta83XK diff --git a/guardian/serializers.py b/guardian/serializers.py index f7016f5..8ec66bf 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -25,7 +25,10 @@ from base.messages import ERROR_CODE, SUCCESS_CODE from base.constants import NUMBER, JUN, ZOD, GRD from junior.models import Junior, JuniorPoints from .utils import real_time, convert_timedelta_into_datetime, update_referral_points - +# notification's constant +from notifications.constants import TASK_POINTS, TASK_REJECTED +# send notification function +from notifications.utils import send_notification # In this serializer file @@ -61,7 +64,7 @@ class UserSerializer(serializers.ModelSerializer): try: """Create user profile""" user = User.objects.create_user(username=email, email=email, password=password) - UserNotification.objects.create(user=user) + UserNotification.objects.get_or_create(user=user) if user_type == str(NUMBER['one']): # create junior profile Junior.objects.create(auth=user, junior_code=generate_code(JUN, user.id), @@ -328,27 +331,30 @@ class ApproveTaskSerializer(serializers.ModelSerializer): def create(self, validated_data): """update task status """ - instance = self.context['task_instance'] - junior = self.context['junior'] - junior_details = Junior.objects.filter(id=junior).last() - junior_data, created = JuniorPoints.objects.get_or_create(junior=junior_details) - if self.context['action'] == str(NUMBER['one']): - # approve the task - instance.task_status = str(NUMBER['five']) - instance.is_approved = True - # update total task point - junior_data.total_points = junior_data.total_points + instance.points - # update complete time of task - instance.completed_on = real_time() - else: - # reject the task - instance.task_status = str(NUMBER['three']) - instance.is_approved = False - # update total task point - junior_data.total_points = junior_data.total_points - instance.points - # update reject time of task - instance.rejected_on = real_time() - instance.save() - junior_data.save() - return instance + with transaction.atomic(): + instance = self.context['task_instance'] + junior = self.context['junior'] + junior_details = Junior.objects.filter(id=junior).last() + junior_data, created = JuniorPoints.objects.get_or_create(junior=junior_details) + if self.context['action'] == str(NUMBER['one']): + # approve the task + instance.task_status = str(NUMBER['five']) + instance.is_approved = True + # update total task point + junior_data.total_points = junior_data.total_points + instance.points + # update complete time of task + instance.completed_on = real_time() + send_notification.delay(TASK_POINTS, None, junior_details.auth.id, {}) + else: + # reject the task + instance.task_status = str(NUMBER['three']) + instance.is_approved = False + # update total task point + junior_data.total_points = junior_data.total_points - instance.points + # update reject time of task + instance.rejected_on = real_time() + send_notification.delay(TASK_REJECTED, None, junior_details.auth.id, {}) + instance.save() + junior_data.save() + return junior_details diff --git a/guardian/utils.py b/guardian/utils.py index b1d5eda..4a25d9e 100644 --- a/guardian/utils.py +++ b/guardian/utils.py @@ -18,6 +18,10 @@ from junior.models import Junior, JuniorPoints from .models import JuniorTask # Import app from celery from zod_bank.celery import app +# notification's constant +from notifications.constants import REFERRAL_POINTS +# send notification function +from notifications.utils import send_notification # Define upload image on # ali baba cloud # firstly save image @@ -85,6 +89,8 @@ def update_referral_points(referral_code, referral_code_used): junior_query.total_points = junior_query.total_points + NUMBER['five'] junior_query.referral_points = junior_query.referral_points + NUMBER['five'] junior_query.save() + send_notification.delay(REFERRAL_POINTS, None, junior_queryset.auth.id, {}) + @app.task diff --git a/guardian/views.py b/guardian/views.py index 5fe331e..7a56656 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -35,7 +35,7 @@ from account.utils import custom_response, custom_error_response, OTP_EXPIRY, se from base.messages import ERROR_CODE, SUCCESS_CODE from base.constants import NUMBER from .utils import upload_image_to_alibaba -from notifications.constants import REGISTRATION, TASK_CREATED +from notifications.constants import REGISTRATION, TASK_CREATED, LEADERBOARD_RANKING from notifications.utils import send_notification """ Define APIs """ @@ -245,6 +245,7 @@ class TopJuniorListAPIView(viewsets.ModelViewSet): # Update the position field for each JuniorPoints object for index, junior in enumerate(junior_total_points): junior.position = index + 1 + send_notification.delay(LEADERBOARD_RANKING, None, junior.junior.auth.id, {}) junior.save() serializer = self.get_serializer(junior_total_points, many=True) @@ -309,11 +310,14 @@ class ApproveTaskAPIView(viewsets.ViewSet): "action": str(request.data['action']), "junior": self.request.data['junior_id']}, data=request.data) - if str(request.data['action']) == str(NUMBER['one']) and serializer.is_valid(): + unexpected_task_status = [str(NUMBER['five']), str(NUMBER['six'])] + if (str(request.data['action']) == str(NUMBER['one']) and serializer.is_valid() + and queryset[1] and queryset[1].task_status not in unexpected_task_status): # save serializer serializer.save() return custom_response(SUCCESS_CODE['3025'], response_status=status.HTTP_200_OK) - elif str(request.data['action']) == str(NUMBER['two']) and serializer.is_valid(): + elif (str(request.data['action']) == str(NUMBER['two']) and serializer.is_valid() + and queryset[1] and queryset[1].task_status not in unexpected_task_status): # save serializer serializer.save() return custom_response(SUCCESS_CODE['3026'], response_status=status.HTTP_200_OK) diff --git a/junior/serializers.py b/junior/serializers.py index 13b6815..27a6808 100644 --- a/junior/serializers.py +++ b/junior/serializers.py @@ -18,7 +18,8 @@ from account.models import UserEmailOtp, UserNotification from junior.utils import junior_notification_email, junior_approval_mail from guardian.utils import real_time, update_referral_points, convert_timedelta_into_datetime from notifications.utils import send_notification -from notifications.constants import INVITED_GUARDIAN, APPROVED_JUNIOR +from notifications.constants import (INVITED_GUARDIAN, APPROVED_JUNIOR, SKIPPED_PROFILE_SETUP, TASK_ACTION, + TASK_SUBMITTED) class ListCharField(serializers.ListField): @@ -284,9 +285,13 @@ class AddJuniorSerializer(serializers.ModelSerializer): expiry_time = timezone.now() + timezone.timedelta(days=1) UserEmailOtp.objects.create(email=email, otp=otp_value, user_type='1', expired_at=expiry_time, is_verified=True) + # add push notification + UserNotification.objects.get_or_create(user=user_data) """Notification email""" junior_notification_email(email, full_name, email, password) junior_approval_mail(guardian, full_name) + # push notification + send_notification.delay(SKIPPED_PROFILE_SETUP, None, junior_data.auth.id, {}) return junior_data @@ -317,6 +322,8 @@ class CompleteTaskSerializer(serializers.ModelSerializer): instance.task_status = str(NUMBER['four']) instance.is_approved = False instance.save() + send_notification.delay(TASK_SUBMITTED, None, instance.junior.auth.id, {}) + send_notification.delay(TASK_ACTION, None, instance.guardian.user.id, {}) return instance class JuniorPointsSerializer(serializers.ModelSerializer): @@ -393,7 +400,7 @@ class AddGuardianSerializer(serializers.ModelSerializer): instance.id), referral_code_used=junior_data.referral_code, is_verified=True) - UserNotification.objects.create(user=instance) + UserNotification.objects.get_or_create(user=instance) return guardian_data else: user = User.objects.create(username=email, email=email, @@ -413,7 +420,7 @@ class AddGuardianSerializer(serializers.ModelSerializer): UserEmailOtp.objects.create(email=email, otp=otp_value, user_type=str(NUMBER['two']), expired_at=expiry_time, is_verified=True) - UserNotification.objects.create(user=user) + UserNotification.objects.get_or_create(user=user) JuniorGuardianRelationship.objects.create(guardian=guardian_data, junior=junior_data, relationship=relationship) diff --git a/junior/views.py b/junior/views.py index 4ccca91..429b2a1 100644 --- a/junior/views.py +++ b/junior/views.py @@ -36,6 +36,8 @@ from base.constants import NUMBER from account.utils import custom_response, custom_error_response from guardian.utils import upload_image_to_alibaba from .utils import update_positions_based_on_points +from notifications.utils import send_notification +from notifications.constants import REMOVE_JUNIOR """ Define APIs """ # Define validate guardian code API, @@ -233,6 +235,7 @@ class RemoveJuniorAPIView(views.APIView): if serializer.is_valid(): # save serializer serializer.save() + send_notification.delay(REMOVE_JUNIOR, None, junior_queryset.auth.id, {}) return custom_response(SUCCESS_CODE['3022'], serializer.data, response_status=status.HTTP_200_OK) return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST) else: @@ -374,6 +377,8 @@ class InviteGuardianAPIView(viewsets.ModelViewSet): def create(self, request, *args, **kwargs): """ junior list""" try: + if request.data['email'] == '': + return custom_error_response(ERROR_CODE['2062'], response_status=status.HTTP_400_BAD_REQUEST) info = {'user': request.user, 'email': request.data['email'], 'first_name': request.data['first_name'], 'last_name': request.data['last_name'], 'relationship': str(request.data['relationship'])} # use AddJuniorSerializer serializer diff --git a/notifications/constants.py b/notifications/constants.py index c6f0a9f..b754ef0 100644 --- a/notifications/constants.py +++ b/notifications/constants.py @@ -1,10 +1,19 @@ """ notification constants file """ + REGISTRATION = 1 TASK_CREATED = 2 INVITED_GUARDIAN = 3 APPROVED_JUNIOR = 4 +REFERRAL_POINTS = 5 +TASK_POINTS = 6 +TASK_REJECTED = 7 +SKIPPED_PROFILE_SETUP = 8 +TASK_SUBMITTED = 9 +TASK_ACTION = 10 +LEADERBOARD_RANKING = 11 +REMOVE_JUNIOR = 12 TEST_NOTIFICATION = 99 NOTIFICATION_DICT = { @@ -22,7 +31,39 @@ NOTIFICATION_DICT = { }, APPROVED_JUNIOR: { "title": "Approve junior", - "body": "You have request for associate the junior" + "body": "You have request from junior to associate with you" + }, + REFERRAL_POINTS: { + "title": "Earn Referral points", + "body": "You earn 5 points for referral." + }, + TASK_POINTS: { + "title": "Earn Task points!", + "body": "You earn 5 points for task." + }, + TASK_REJECTED: { + "title": "Task rejected!", + "body": "Your task has been rejected." + }, + SKIPPED_PROFILE_SETUP: { + "title": "Skipped profile setup!", + "body": "Your guardian has been setup your profile." + }, + TASK_SUBMITTED: { + "title": "Task submitted!", + "body": "Your task has been submitted successfully." + }, + TASK_ACTION: { + "title": "Task approval!", + "body": "You have request for task approval." + }, + LEADERBOARD_RANKING: { + "title": "Leader board rank!", + "body": "Your rank is ." + }, + REMOVE_JUNIOR: { + "title": "Disassociate by guardian!", + "body": "Your guardian disassociate you ." }, TEST_NOTIFICATION: { "title": "Test Notification", From a323ad7df772e4d645c4cae7e071a3a6d15bc685 Mon Sep 17 00:00:00 2001 From: jain Date: Wed, 26 Jul 2023 16:42:26 +0530 Subject: [PATCH 2/2] jira-28 notification --- notifications/constants.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/notifications/constants.py b/notifications/constants.py index b754ef0..b861142 100644 --- a/notifications/constants.py +++ b/notifications/constants.py @@ -1,19 +1,19 @@ """ notification constants file """ - -REGISTRATION = 1 -TASK_CREATED = 2 -INVITED_GUARDIAN = 3 -APPROVED_JUNIOR = 4 -REFERRAL_POINTS = 5 -TASK_POINTS = 6 -TASK_REJECTED = 7 -SKIPPED_PROFILE_SETUP = 8 -TASK_SUBMITTED = 9 -TASK_ACTION = 10 -LEADERBOARD_RANKING = 11 -REMOVE_JUNIOR = 12 +from base.constants import NUMBER +REGISTRATION = NUMBER['one'] +TASK_CREATED = NUMBER['two'] +INVITED_GUARDIAN = NUMBER['three'] +APPROVED_JUNIOR = NUMBER['four'] +REFERRAL_POINTS = NUMBER['five'] +TASK_POINTS = NUMBER['six'] +TASK_REJECTED = NUMBER['seven'] +SKIPPED_PROFILE_SETUP = NUMBER['eight'] +TASK_SUBMITTED = NUMBER['nine'] +TASK_ACTION = NUMBER['ten'] +LEADERBOARD_RANKING = NUMBER['eleven'] +REMOVE_JUNIOR = NUMBER['twelve'] TEST_NOTIFICATION = 99 NOTIFICATION_DICT = {