Merge branch 'dev' into ZBKADM-67

This commit is contained in:
abutalib-kiwi
2023-07-26 17:22:23 +05:30
13 changed files with 117 additions and 59 deletions

View File

@ -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 = {

Binary file not shown.

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -1,10 +1,19 @@
"""
notification constants file
"""
REGISTRATION = 1
TASK_CREATED = 2
INVITED_GUARDIAN = 3
APPROVED_JUNIOR = 4
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 = {
@ -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",

View File

@ -52,7 +52,6 @@ def send_notification(notification_type, from_user_id, to_user_id, extra_data):
""" used to send the push for the given notification type """
(notification_data, from_user, to_user) = get_basic_detail(notification_type, from_user_id, to_user_id)
user_notification_type = UserNotification.objects.filter(user=to_user).first()
# data = notification_data.data
data = notification_data
Notification.objects.create(notification_type=notification_type, notification_from=from_user,
notification_to=to_user, data=data)

View File

@ -18,7 +18,7 @@ from notifications.utils import send_notification
class NotificationViewSet(viewsets.GenericViewSet):
""" used to do the notification actions """
serializer_class = None
serializer_class = RegisterDevice
permission_classes = [IsAuthenticated, ]
@action(methods=['post'], detail=False, url_path='device', url_name='device', serializer_class=RegisterDevice)
@ -32,7 +32,7 @@ class NotificationViewSet(viewsets.GenericViewSet):
serializer.save()
return custom_response(SUCCESS_CODE["3000"])
@action(methods=['get'], detail=False, url_path='test', url_name='test', serializer_class=None)
@action(methods=['get'], detail=False, url_path='test', url_name='test')
def send_test_notification(self, request):
"""
to send test notification

View File

@ -20,7 +20,7 @@ from guardian.tasks import generate_otp
USER = get_user_model()
class AdminForgotPasswordSerializer(serializers.ModelSerializer):
class AdminOTPSerializer(serializers.ModelSerializer):
"""
admin forgot password serializer
"""

View File

@ -110,7 +110,7 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
return custom_error_response(ERROR_CODE["2041"], status.HTTP_400_BAD_REQUEST)
@action(methods=['get'], url_name='remove_card', url_path='remove_card',
detail=True, serializer_class=None)
detail=True)
def remove_article_card(self, request, *args, **kwargs):
"""
article card remove (delete) api method
@ -126,7 +126,7 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
return custom_error_response(ERROR_CODE["2042"], response_status=status.HTTP_400_BAD_REQUEST)
@action(methods=['get'], url_name='remove_survey', url_path='remove_survey',
detail=True, serializer_class=None)
detail=True)
def remove_article_survey(self, request, *args, **kwargs):
"""
article survey remove (delete) api method
@ -170,7 +170,7 @@ class DefaultArticleCardImagesViewSet(GenericViewSet, mixins.CreateModelMixin, m
api to upload and list default article card images
"""
serializer_class = DefaultArticleCardImageSerializer
# permission_classes = [IsAuthenticated, AdminPermission]
permission_classes = [IsAuthenticated, AdminPermission]
queryset = DefaultArticleCardImage.objects.all()
def create(self, request, *args, **kwargs):

View File

@ -9,7 +9,7 @@ from django.contrib.auth import get_user_model
# local imports
from account.utils import custom_response
from base.messages import SUCCESS_CODE
from web_admin.serializers.auth_serializer import (AdminForgotPasswordSerializer, AdminVerifyOTPSerializer,
from web_admin.serializers.auth_serializer import (AdminOTPSerializer, AdminVerifyOTPSerializer,
AdminCreatePasswordSerializer)
USER = get_user_model()
@ -21,9 +21,9 @@ class ForgotAndResetPasswordViewSet(GenericViewSet):
"""
queryset = None
@action(methods=['post'], url_name='forgot-password', url_path='forgot-password',
detail=False, serializer_class=AdminForgotPasswordSerializer)
def admin_forgot_password(self, request):
@action(methods=['post'], url_name='otp', url_path='otp',
detail=False, serializer_class=AdminOTPSerializer)
def admin_otp(self, request):
"""
api method to send otp
:return: success message
@ -44,18 +44,6 @@ class ForgotAndResetPasswordViewSet(GenericViewSet):
serializer.is_valid(raise_exception=True)
return custom_response(SUCCESS_CODE['3011'])
@action(methods=['post'], url_name='resend-otp', url_path='resend-otp',
detail=False, serializer_class=AdminForgotPasswordSerializer)
def admin_resend_otp(self, request):
"""
api method to resend otp
:return: success message
"""
serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return custom_response(SUCCESS_CODE['3015'])
@action(methods=['post'], url_name='create-password', url_path='create-password',
detail=False, serializer_class=AdminCreatePasswordSerializer)
def admin_create_password(self, request):