diff --git a/celerybeat-schedule b/celerybeat-schedule index f2510fc..ce5d8ac 100644 Binary files a/celerybeat-schedule and b/celerybeat-schedule differ diff --git a/guardian/serializers.py b/guardian/serializers.py index 1dabc8d..cbd1e4e 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -29,7 +29,7 @@ from .utils import real_time, convert_timedelta_into_datetime, update_referral_p # notification's constant from notifications.constants import TASK_POINTS, TASK_REJECTED # send notification function -from notifications.utils import send_notification +from notifications.utils import send_notification, send_notification_to_junior # In this serializer file @@ -383,7 +383,8 @@ class ApproveTaskSerializer(serializers.ModelSerializer): # update complete time of task # instance.completed_on = real_time() instance.completed_on = timezone.now().astimezone(pytz.utc) - send_notification.delay(TASK_POINTS, None, junior_details.auth.id, {}) + send_notification_to_junior.delay(TASK_POINTS, None, junior_details.auth.id, + {'task_id': instance.id}) else: # reject the task instance.task_status = str(NUMBER['three']) @@ -391,7 +392,8 @@ class ApproveTaskSerializer(serializers.ModelSerializer): # update reject time of task # instance.rejected_on = real_time() instance.rejected_on = timezone.now().astimezone(pytz.utc) - send_notification.delay(TASK_REJECTED, None, junior_details.auth.id, {}) + send_notification_to_junior.delay(TASK_REJECTED, None, junior_details.auth.id, + {'task_id': instance.id}) instance.save() junior_data.save() return junior_details diff --git a/guardian/utils.py b/guardian/utils.py index 57e8080..d5081ac 100644 --- a/guardian/utils.py +++ b/guardian/utils.py @@ -21,7 +21,9 @@ from zod_bank.celery import app # notification's constant from notifications.constants import REFERRAL_POINTS # send notification function -from notifications.utils import send_notification +from notifications.utils import send_notification, send_notification_to_junior + + # Define upload image on # ali baba cloud # firstly save image @@ -92,7 +94,7 @@ 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, {}) + send_notification_to_junior.delay(REFERRAL_POINTS, None, junior_queryset.auth.id, {}) diff --git a/guardian/views.py b/guardian/views.py index 3948e6f..0319938 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -38,8 +38,8 @@ 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, GUARDIAN_CODE_STATUS from .utils import upload_image_to_alibaba -from notifications.constants import REGISTRATION, TASK_CREATED, LEADERBOARD_RANKING -from notifications.utils import send_notification +from notifications.constants import REGISTRATION, TASK_ASSIGNED, LEADERBOARD_RANKING +from notifications.utils import send_notification_to_junior """ Define APIs """ # Define Signup API, @@ -72,8 +72,6 @@ class SignupViewset(viewsets.ModelViewSet): user_type=str(request.data['user_type']), expired_at=expiry) """Send email to the register user""" send_otp_email(request.data['email'], otp) - # send push notification for registration - send_notification.delay(REGISTRATION, None, user.id, {}) return custom_response(SUCCESS_CODE['3001'], response_status=status.HTTP_200_OK) return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST) @@ -186,9 +184,10 @@ class CreateTaskAPIView(viewsets.ModelViewSet): serializer = TaskSerializer(context={"user":request.user, "image":image_data}, data=data) if serializer.is_valid(): # save serializer - serializer.save() + task = serializer.save() junior_id = Junior.objects.filter(id=junior).last() - send_notification.delay(TASK_CREATED, None, junior_id.auth.id, {}) + send_notification_to_junior.delay(TASK_ASSIGNED, request.auth.payload['user_id'], + junior_id.auth.id, {'task_id': task.id}) return custom_response(SUCCESS_CODE['3018'], serializer.data, response_status=status.HTTP_200_OK) return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST) except Exception as e: @@ -241,7 +240,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, {}) + send_notification_to_junior.delay(LEADERBOARD_RANKING, None, junior.junior.auth.id, {}) junior.save() serializer = self.get_serializer(junior_total_points[:NUMBER['fifteen']], many=True) return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) diff --git a/junior/serializers.py b/junior/serializers.py index c3e2d72..f932a26 100644 --- a/junior/serializers.py +++ b/junior/serializers.py @@ -21,7 +21,7 @@ from guardian.models import Guardian, JuniorTask 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.utils import send_notification, send_notification_to_junior, send_notification_to_guardian from notifications.constants import (INVITED_GUARDIAN, APPROVED_JUNIOR, SKIPPED_PROFILE_SETUP, TASK_ACTION, TASK_SUBMITTED) @@ -98,7 +98,7 @@ class CreateJuniorSerializer(serializers.ModelSerializer): JuniorGuardianRelationship.objects.get_or_create(guardian=guardian_data, junior=junior) junior.guardian_code_status = str(NUMBER['three']) junior_approval_mail(user.email, user.first_name) - send_notification.delay(APPROVED_JUNIOR, None, guardian_data.user.id, {}) + send_notification_to_guardian.delay(APPROVED_JUNIOR, junior.auth.id, guardian_data.user.id, {}) junior.dob = validated_data.get('dob', junior.dob) junior.passcode = validated_data.get('passcode', junior.passcode) junior.country_name = validated_data.get('country_name', junior.country_name) @@ -305,7 +305,7 @@ class AddJuniorSerializer(serializers.ModelSerializer): """Notification email""" junior_notification_email(email, full_name, email, password) # push notification - send_notification.delay(SKIPPED_PROFILE_SETUP, None, junior_data.auth.id, {}) + send_notification_to_junior.delay(SKIPPED_PROFILE_SETUP, None, junior_data.auth.id, {}) return junior_data @@ -337,8 +337,10 @@ 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, {}) + send_notification_to_junior.delay(TASK_SUBMITTED, instance.guardian.user.id, + instance.junior.auth.id, {'task_id': instance.id}) + send_notification_to_guardian.delay(TASK_ACTION, instance.junior.auth.id, + instance.guardian.user.id, {'task_id': instance.id}) return instance class JuniorPointsSerializer(serializers.ModelSerializer): @@ -448,8 +450,8 @@ class AddGuardianSerializer(serializers.ModelSerializer): """Notification email""" junior_notification_email(email, full_name, email, password) junior_approval_mail(email, full_name) - send_notification.delay(INVITED_GUARDIAN, None, junior_data.auth.id, {}) - send_notification.delay(APPROVED_JUNIOR, None, guardian_data.user.id, {}) + send_notification_to_junior.delay(INVITED_GUARDIAN, guardian_data.user.id, junior_data.auth.id, {}) + send_notification_to_guardian.delay(APPROVED_JUNIOR, junior_data.auth.id, guardian_data.user.id, {}) return guardian_data class StartTaskSerializer(serializers.ModelSerializer): diff --git a/junior/views.py b/junior/views.py index a86083d..6b8bc2f 100644 --- a/junior/views.py +++ b/junior/views.py @@ -42,7 +42,7 @@ from base.constants import NUMBER, ARTICLE_STATUS 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.utils import send_notification, send_notification_to_junior from notifications.constants import REMOVE_JUNIOR from web_admin.models import Article, ArticleSurvey, SurveyOption, ArticleCard from web_admin.serializers.article_serializer import (ArticleSerializer, ArticleListSerializer, @@ -269,7 +269,7 @@ class RemoveJuniorAPIView(views.APIView): if serializer.is_valid(): # save serializer serializer.save() - send_notification.delay(REMOVE_JUNIOR, None, junior_queryset.auth.id, {}) + send_notification_to_junior.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: @@ -334,7 +334,8 @@ class CompleteJuniorTaskAPIView(views.APIView): image_url = upload_image_to_alibaba(image, filename) # fetch junior query - task_queryset = JuniorTask.objects.filter(id=task_id, junior__auth__email=self.request.user).last() + task_queryset = JuniorTask.objects.filter(id=task_id, junior__auth__email=self.request.user + ).select_related('guardian', 'junior').last() if task_queryset: # use CompleteTaskSerializer serializer if task_queryset.task_status in [str(NUMBER['four']), str(NUMBER['five'])]: diff --git a/notifications/constants.py b/notifications/constants.py index 46bfbef..59c2328 100644 --- a/notifications/constants.py +++ b/notifications/constants.py @@ -3,7 +3,7 @@ notification constants file """ from base.constants import NUMBER REGISTRATION = NUMBER['one'] -TASK_CREATED = NUMBER['two'] +TASK_ASSIGNED = NUMBER['two'] INVITED_GUARDIAN = NUMBER['three'] APPROVED_JUNIOR = NUMBER['four'] REFERRAL_POINTS = NUMBER['five'] @@ -21,17 +21,17 @@ NOTIFICATION_DICT = { "title": "Successfully registered!", "body": "You have registered successfully. Now login and complete your profile." }, - TASK_CREATED: { - "title": "Task created!", - "body": "Task created successfully." + TASK_ASSIGNED: { + "title": "New task assigned !!", + "body": "{from_user} has assigned you a new task." }, INVITED_GUARDIAN: { "title": "Invite guardian", "body": "Invite guardian successfully" }, APPROVED_JUNIOR: { - "title": "Approve junior", - "body": "You have request from junior to associate with you" + "title": "Approve junior !", + "body": "You have request from {from_user} to associate with you." }, REFERRAL_POINTS: { "title": "Earn Referral points", @@ -47,15 +47,15 @@ NOTIFICATION_DICT = { }, SKIPPED_PROFILE_SETUP: { "title": "Skipped profile setup!", - "body": "Your guardian has been setup your profile." + "body": "Your guardian {from_user} has 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." + "title": "Task completion approval!", + "body": "You have request from {from_user} for task approval." }, LEADERBOARD_RANKING: { "title": "Leader board rank!", diff --git a/notifications/utils.py b/notifications/utils.py index 38df5ba..50dee81 100644 --- a/notifications/utils.py +++ b/notifications/utils.py @@ -11,6 +11,8 @@ from django.contrib.auth import get_user_model from account.models import UserNotification from account.utils import get_user_full_name +from guardian.models import Guardian +from junior.models import Junior from notifications.constants import NOTIFICATION_DICT from notifications.models import Notification @@ -72,12 +74,11 @@ def get_notification_data(notification_type, from_user, to_user, extra_data): return notification_data, push_data -@shared_task() -def send_notification(notification_type, from_user_id, to_user_id, extra_data): +def send_notification(notification_type, from_user, to_user, extra_data): """ used to send the push for the given notification type """ - (from_user, to_user) = get_basic_detail(from_user_id, to_user_id) + # (from_user, to_user) = get_basic_detail(from_user_id, to_user_id) notification_data, push_data = get_notification_data(notification_type, from_user, to_user, extra_data) user_notification_type = UserNotification.objects.filter(user=to_user).first() notification_data.update({'badge': Notification.objects.filter(notification_to=to_user, is_read=False).count()}) @@ -92,3 +93,29 @@ def send_push(user, data): user.fcmdevice_set.filter(active=True).send_message( Message(notification=FirebaseNotification(data['title'], data['body']), data=data) ) + + +@shared_task() +def send_notification_to_guardian(notification_type, from_user_id, to_user_id, extra_data): + from_user = None + if from_user_id: + from_user = Junior.objects.filter(auth_id=from_user_id).select_related('auth').first() + extra_data['from_user_image'] = from_user.image + from_user = from_user.auth + to_user = Guardian.objects.filter(user_id=to_user_id).select_related('user').first() + extra_data['to_user_image'] = to_user.image + send_notification(notification_type, from_user, to_user.user, extra_data) + + +@shared_task() +def send_notification_to_junior(notification_type, from_user_id, to_user_id, extra_data): + from_user = None + if from_user_id: + from_user = Guardian.objects.filter(user_id=from_user_id).select_related('user').first() + extra_data['from_user_image'] = from_user.image + from_user = from_user.user + to_user = Junior.objects.filter(auth_id=to_user_id).select_related('auth').first() + extra_data['to_user_image'] = to_user.image + send_notification(notification_type, from_user, to_user.auth, extra_data) + + diff --git a/notifications/views.py b/notifications/views.py index f932f0f..dc6e891 100644 --- a/notifications/views.py +++ b/notifications/views.py @@ -13,7 +13,7 @@ from base.messages import SUCCESS_CODE, ERROR_CODE from notifications.constants import TEST_NOTIFICATION # Import serializer from notifications.serializers import RegisterDevice, NotificationListSerializer, ReadNotificationSerializer -from notifications.utils import send_notification +from notifications.utils import send_notification, send_notification_to_guardian, send_notification_to_junior # Import model from notifications.models import Notification @@ -56,8 +56,10 @@ class NotificationViewSet(viewsets.GenericViewSet): to send test notification :return: """ - send_notification(TEST_NOTIFICATION, None, request.auth.payload['user_id'], - {'task_id': None}) + send_notification_to_guardian(TEST_NOTIFICATION, None, request.auth.payload['user_id'], + {'task_id': None}) + send_notification_to_junior(TEST_NOTIFICATION, request.auth.payload['user_id'], None, + {'task_id': None}) return custom_response(SUCCESS_CODE["3000"]) @action(methods=['get'], detail=False, url_path='list', url_name='list',