mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-08-26 06:09:41 +00:00
Compare commits
27 Commits
ZBKADM-72-
...
email-veri
Author | SHA1 | Date | |
---|---|---|---|
04ed9c668c | |||
36427b50c1 | |||
5da07002e0 | |||
3017b0ece0 | |||
e73113fcca | |||
c278670c39 | |||
60af098e1e | |||
d29a60558f | |||
451c3bdae7 | |||
7c809776b6 | |||
e46d649fdc | |||
adb827f5a0 | |||
a02dfd4e31 | |||
d3b0be953e | |||
11b9f00285 | |||
08dc9f8538 | |||
6373d1e02c | |||
a1d959299a | |||
070637bf1d | |||
fd9a4902ae | |||
082f93ff9d | |||
25eaee31a4 | |||
d5c30f2029 | |||
cae28e0b54 | |||
54200dba52 | |||
846a9d42d4 | |||
4f02cef0f9 |
@ -216,10 +216,17 @@ class GuardianSerializer(serializers.ModelSerializer):
|
||||
|
||||
def get_user_type(self, obj):
|
||||
"""user type"""
|
||||
email_verified = UserEmailOtp.objects.filter(email=obj.user.username).last()
|
||||
if email_verified and email_verified.user_type is not None:
|
||||
return email_verified.user_type
|
||||
return str(NUMBER['two'])
|
||||
if self.context.get('user_type', ''):
|
||||
return self.context.get('user_type')
|
||||
# remove the below code once user_type can be passed
|
||||
# from everywhere from where this serializer is being called
|
||||
else:
|
||||
email_verified = UserEmailOtp.objects.filter(
|
||||
email=obj.user.username
|
||||
).last()
|
||||
if email_verified and email_verified.user_type is not None:
|
||||
return email_verified.user_type
|
||||
return str(NUMBER['two'])
|
||||
|
||||
def get_auth(self, obj):
|
||||
"""user email address"""
|
||||
|
@ -1,4 +1,6 @@
|
||||
"""Account utils"""
|
||||
from celery import shared_task
|
||||
|
||||
"""Import django"""
|
||||
from django.conf import settings
|
||||
from rest_framework import viewsets, status
|
||||
@ -20,7 +22,7 @@ from rest_framework import serializers
|
||||
# Import messages from base package"""
|
||||
from junior.models import Junior
|
||||
from guardian.models import Guardian
|
||||
from account.models import UserDelete
|
||||
from account.models import UserDelete, UserDeviceDetails
|
||||
from base.messages import ERROR_CODE
|
||||
from django.utils import timezone
|
||||
from base.constants import NUMBER
|
||||
@ -44,7 +46,7 @@ from junior.models import JuniorPoints
|
||||
# referral code,
|
||||
# Define function for generating
|
||||
# alphanumeric code
|
||||
|
||||
# otp expiry
|
||||
def delete_user_account_condition(user, user_type_data, user_type, user_tb, data, random_num):
|
||||
"""delete user account"""
|
||||
if user_type == '1' and user_type_data == '1':
|
||||
@ -109,6 +111,7 @@ def guardian_account_update(user_tb):
|
||||
for data in jun_data:
|
||||
data.guardian_code.remove(guardian_data.guardian_code)
|
||||
data.save()
|
||||
@shared_task()
|
||||
def send_otp_email(recipient_email, otp):
|
||||
"""Send otp on email with template"""
|
||||
from_email = settings.EMAIL_FROM_ADDRESS
|
||||
@ -124,6 +127,20 @@ def send_otp_email(recipient_email, otp):
|
||||
)
|
||||
return otp
|
||||
|
||||
@shared_task
|
||||
def user_device_details(user, device_id):
|
||||
"""
|
||||
Used to store the device id of the user
|
||||
user: user object
|
||||
device_id: string
|
||||
return
|
||||
"""
|
||||
device_details, created = UserDeviceDetails.objects.get_or_create(user=user)
|
||||
if device_details:
|
||||
device_details.device_id = device_id
|
||||
device_details.save()
|
||||
|
||||
|
||||
def send_support_email(name, sender, subject, message):
|
||||
"""Send otp on email with template"""
|
||||
to_email = [settings.EMAIL_FROM_ADDRESS]
|
||||
@ -234,3 +251,9 @@ def generate_code(value, user_id):
|
||||
|
||||
|
||||
OTP_EXPIRY = timezone.now() + timezone.timedelta(days=1)
|
||||
|
||||
def get_user_full_name(user_obj):
|
||||
"""
|
||||
to get user's full name
|
||||
"""
|
||||
return f"{user_obj.first_name} {user_obj.last_name}" if user_obj.last_name else user_obj.first_name
|
||||
|
@ -1,11 +1,12 @@
|
||||
"""Account view """
|
||||
import threading
|
||||
|
||||
from notifications.utils import remove_fcm_token
|
||||
|
||||
# django imports
|
||||
from datetime import datetime, timedelta
|
||||
from rest_framework import viewsets, status, views
|
||||
from rest_framework.decorators import action
|
||||
import random
|
||||
import logging
|
||||
from django.utils import timezone
|
||||
import jwt
|
||||
@ -35,10 +36,10 @@ from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSeriali
|
||||
AdminLoginSerializer)
|
||||
from rest_framework_simplejwt.tokens import RefreshToken
|
||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||
from base.constants import NUMBER, ZOD, JUN, GRD
|
||||
from base.constants import NUMBER, ZOD, JUN, GRD, USER_TYPE_FLAG
|
||||
from guardian.tasks import generate_otp
|
||||
from account.utils import (send_otp_email, send_support_email, custom_response, custom_error_response,
|
||||
generate_code, OTP_EXPIRY)
|
||||
generate_code, OTP_EXPIRY, user_device_details)
|
||||
from junior.serializers import JuniorProfileSerializer
|
||||
from guardian.serializers import GuardianProfileSerializer
|
||||
|
||||
@ -280,29 +281,48 @@ class UserPhoneVerification(viewsets.ModelViewSet):
|
||||
return custom_error_response(ERROR_CODE["2008"], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
|
||||
class UserLogin(viewsets.ViewSet):
|
||||
"""User login"""
|
||||
@action(methods=['post'], detail=False)
|
||||
def login(self, request):
|
||||
username = request.data.get('username')
|
||||
password = request.data.get('password')
|
||||
user_type = request.data.get('user_type')
|
||||
device_id = request.META.get('HTTP_DEVICE_ID')
|
||||
user = authenticate(request, username=username, password=password)
|
||||
|
||||
try:
|
||||
if user is not None:
|
||||
login(request, user)
|
||||
guardian_data = Guardian.objects.filter(user__username=username, is_verified=True).last()
|
||||
if guardian_data:
|
||||
serializer = GuardianSerializer(guardian_data).data
|
||||
junior_data = Junior.objects.filter(auth__username=username, is_verified=True).last()
|
||||
if junior_data:
|
||||
serializer = JuniorSerializer(junior_data).data
|
||||
device_details, created = UserDeviceDetails.objects.get_or_create(user=user)
|
||||
if device_details:
|
||||
device_details.device_id = device_id
|
||||
device_details.save()
|
||||
if str(user_type) == USER_TYPE_FLAG["TWO"]:
|
||||
guardian_data = Guardian.objects.filter(user__username=username, is_verified=True).last()
|
||||
if guardian_data:
|
||||
serializer = GuardianSerializer(
|
||||
guardian_data, context={'user_type': user_type}
|
||||
).data
|
||||
else:
|
||||
return custom_error_response(
|
||||
ERROR_CODE["2070"],
|
||||
response_status=status.HTTP_401_UNAUTHORIZED
|
||||
)
|
||||
elif str(user_type) == USER_TYPE_FLAG["FIRST"]:
|
||||
junior_data = Junior.objects.filter(auth__username=username, is_verified=True).last()
|
||||
if junior_data:
|
||||
serializer = JuniorSerializer(
|
||||
junior_data, context={'user_type': user_type}
|
||||
).data
|
||||
else:
|
||||
return custom_error_response(
|
||||
ERROR_CODE["2071"],
|
||||
response_status=status.HTTP_401_UNAUTHORIZED
|
||||
)
|
||||
else:
|
||||
return custom_error_response(
|
||||
ERROR_CODE["2069"],
|
||||
response_status=status.HTTP_401_UNAUTHORIZED
|
||||
)
|
||||
# storing device id in using thread so the time would be reduced
|
||||
threading.Thread(target=user_device_details, args=(user, device_id))
|
||||
return custom_response(SUCCESS_CODE['3003'], serializer, response_status=status.HTTP_200_OK)
|
||||
else:
|
||||
return custom_error_response(ERROR_CODE["2002"], response_status=status.HTTP_401_UNAUTHORIZED)
|
||||
@ -312,9 +332,12 @@ class UserLogin(viewsets.ViewSet):
|
||||
refresh = RefreshToken.for_user(user)
|
||||
access_token = str(refresh.access_token)
|
||||
refresh_token = str(refresh)
|
||||
data = {"auth_token":access_token, "refresh_token":refresh_token, "is_profile_complete": False,
|
||||
"user_type": email_verified.user_type,
|
||||
}
|
||||
data = {
|
||||
"auth_token":access_token,
|
||||
"refresh_token":refresh_token,
|
||||
"is_profile_complete": False,
|
||||
"user_type": user_type,
|
||||
}
|
||||
is_verified = False
|
||||
if email_verified:
|
||||
is_verified = email_verified.is_verified
|
||||
@ -323,11 +346,18 @@ class UserLogin(viewsets.ViewSet):
|
||||
email_verified.otp = otp
|
||||
email_verified.save()
|
||||
data.update({"email_otp":otp})
|
||||
send_otp_email(username, otp)
|
||||
return custom_response(ERROR_CODE['2024'], {"email_otp": otp, "is_email_verified": is_verified},
|
||||
response_status=status.HTTP_200_OK)
|
||||
send_otp_email.delay(username, otp)
|
||||
return custom_response(
|
||||
ERROR_CODE['2024'],
|
||||
{"email_otp": otp, "is_email_verified": is_verified},
|
||||
response_status=status.HTTP_200_OK
|
||||
)
|
||||
data.update({"is_email_verified": is_verified})
|
||||
return custom_response(SUCCESS_CODE['3003'], data, response_status=status.HTTP_200_OK)
|
||||
return custom_response(
|
||||
SUCCESS_CODE['3003'],
|
||||
data,
|
||||
response_status=status.HTTP_200_OK
|
||||
)
|
||||
|
||||
@action(methods=['post'], detail=False)
|
||||
def admin_login(self, request):
|
||||
@ -371,12 +401,13 @@ class AdminLoginViewSet(viewsets.GenericViewSet):
|
||||
class UserEmailVerification(viewsets.ModelViewSet):
|
||||
"""User Email verification"""
|
||||
serializer_class = EmailVerificationSerializer
|
||||
http_method_names = ('post',)
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
def create(self, request, *args, **kwargs):
|
||||
try:
|
||||
user_obj = User.objects.filter(username=self.request.GET.get('email')).last()
|
||||
email_data = UserEmailOtp.objects.filter(email=self.request.GET.get('email'),
|
||||
otp=self.request.GET.get('otp')).last()
|
||||
user_obj = User.objects.filter(username=self.request.data.get('email')).last()
|
||||
email_data = UserEmailOtp.objects.filter(email=self.request.data.get('email'),
|
||||
otp=self.request.data.get('otp')).last()
|
||||
if email_data:
|
||||
input_datetime_str = str(email_data.expired_at)
|
||||
input_format = "%Y-%m-%d %H:%M:%S.%f%z"
|
||||
@ -390,12 +421,12 @@ class UserEmailVerification(viewsets.ModelViewSet):
|
||||
email_data.is_verified = True
|
||||
email_data.save()
|
||||
if email_data.user_type == '1':
|
||||
junior_data = Junior.objects.filter(auth__email=self.request.GET.get('email')).last()
|
||||
junior_data = Junior.objects.filter(auth__email=self.request.data.get('email')).last()
|
||||
if junior_data:
|
||||
junior_data.is_verified = True
|
||||
junior_data.save()
|
||||
else:
|
||||
guardian_data = Guardian.objects.filter(user__email=self.request.GET.get('email')).last()
|
||||
guardian_data = Guardian.objects.filter(user__email=self.request.data.get('email')).last()
|
||||
if guardian_data:
|
||||
guardian_data.is_verified = True
|
||||
guardian_data.save()
|
||||
@ -505,7 +536,7 @@ class UserNotificationAPIViewSet(viewsets.ModelViewSet):
|
||||
permission_classes = [IsAuthenticated]
|
||||
def list(self, request, *args, **kwargs):
|
||||
"""profile view"""
|
||||
queryset = self.queryset.filter(user=request.user)
|
||||
queryset = UserNotification.objects.filter(user=request.user)
|
||||
serializer = UserNotificationSerializer(queryset, many=True)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
|
||||
|
@ -50,6 +50,13 @@ USER_TYPE = (
|
||||
('2', 'guardian'),
|
||||
('3', 'superuser')
|
||||
)
|
||||
|
||||
USER_TYPE_FLAG = {
|
||||
"FIRST" : "1",
|
||||
"TWO" : "2",
|
||||
"THREE": "3"
|
||||
}
|
||||
|
||||
"""gender"""
|
||||
GENDERS = (
|
||||
('1', 'Male'),
|
||||
|
@ -94,7 +94,10 @@ ERROR_CODE = {
|
||||
"2065": "Passwords do not match. Please try again.",
|
||||
"2066": "Task does not exist or not in expired state",
|
||||
"2067": "Action not allowed. User type missing.",
|
||||
"2068": "No guardian associated with this junior"
|
||||
"2068": "No guardian associated with this junior",
|
||||
"2069": "Invalid user type",
|
||||
"2070": "You did not find as a guardian",
|
||||
"2071": "You did not find as a junior"
|
||||
|
||||
}
|
||||
"""Success message code"""
|
||||
|
Binary file not shown.
@ -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
|
||||
|
@ -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, {})
|
||||
|
||||
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
@ -146,8 +146,8 @@ class JuniorDetailSerializer(serializers.ModelSerializer):
|
||||
"""Meta info"""
|
||||
model = Junior
|
||||
fields = ['id', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob',
|
||||
'guardian_code', 'image', 'is_invited', 'referral_code','is_active', 'is_complete_profile', 'created_at',
|
||||
'image', 'updated_at']
|
||||
'guardian_code', 'image', 'is_invited', 'referral_code','is_active', 'is_complete_profile',
|
||||
'created_at', 'image', 'updated_at']
|
||||
|
||||
class JuniorDetailListSerializer(serializers.ModelSerializer):
|
||||
"""junior serializer"""
|
||||
@ -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
|
||||
|
||||
|
||||
@ -320,7 +320,7 @@ class RemoveJuniorSerializer(serializers.ModelSerializer):
|
||||
if instance:
|
||||
instance.is_invited = False
|
||||
instance.guardian_code = '{}'
|
||||
instance.guardian_code_status = str(NUMBER['1'])
|
||||
instance.guardian_code_status = str(NUMBER['one'])
|
||||
instance.save()
|
||||
return instance
|
||||
|
||||
@ -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):
|
||||
@ -502,6 +504,7 @@ class RemoveGuardianCodeSerializer(serializers.ModelSerializer):
|
||||
model = Junior
|
||||
fields = ('id', )
|
||||
def update(self, instance, validated_data):
|
||||
instance.guardian_code = None
|
||||
instance.guardian_code_status = str(NUMBER['one'])
|
||||
instance.save()
|
||||
return instance
|
||||
|
@ -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'])]:
|
||||
|
@ -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!",
|
||||
@ -67,6 +67,6 @@ NOTIFICATION_DICT = {
|
||||
},
|
||||
TEST_NOTIFICATION: {
|
||||
"title": "Test Notification",
|
||||
"body": "This notification is for testing purpose"
|
||||
"body": "This notification is for testing purpose from {from_user}."
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,8 @@ class NotificationListSerializer(serializers.ModelSerializer):
|
||||
class Meta(object):
|
||||
"""meta info"""
|
||||
model = Notification
|
||||
fields = ['id', 'data', 'is_read']
|
||||
fields = ['id', 'data', 'is_read', 'created_at']
|
||||
|
||||
|
||||
class ReadNotificationSerializer(serializers.ModelSerializer):
|
||||
"""User task Serializer"""
|
||||
|
@ -10,6 +10,9 @@ from firebase_admin.messaging import Message, Notification as FirebaseNotificati
|
||||
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
|
||||
|
||||
@ -39,30 +42,80 @@ def remove_fcm_token(user_id: int, access_token: str, registration_id) -> None:
|
||||
print(e)
|
||||
|
||||
|
||||
def get_basic_detail(notification_type, from_user_id, to_user_id):
|
||||
""" used to get the basic details """
|
||||
notification_data = NOTIFICATION_DICT[notification_type]
|
||||
def get_basic_detail(from_user_id, to_user_id):
|
||||
"""
|
||||
used to get the basic details
|
||||
"""
|
||||
from_user = User.objects.get(id=from_user_id) if from_user_id else None
|
||||
to_user = User.objects.get(id=to_user_id)
|
||||
return notification_data, from_user, to_user
|
||||
return from_user, to_user
|
||||
|
||||
|
||||
@shared_task()
|
||||
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)
|
||||
def get_notification_data(notification_type, from_user, to_user, extra_data):
|
||||
"""
|
||||
get notification and push data
|
||||
:param notification_type: notification_type
|
||||
:param from_user: from_user obj
|
||||
:param to_user: to_user obj
|
||||
:param extra_data: any extra data provided
|
||||
:return: notification and push data
|
||||
"""
|
||||
push_data = NOTIFICATION_DICT[notification_type].copy()
|
||||
notification_data = push_data.copy()
|
||||
notification_data['to_user'] = get_user_full_name(to_user)
|
||||
if from_user:
|
||||
from_user_name = get_user_full_name(from_user)
|
||||
push_data['body'] = push_data['body'].format(from_user=from_user_name)
|
||||
notification_data['body'] = notification_data['body'].format(from_user=from_user_name)
|
||||
notification_data['from_user'] = from_user_name
|
||||
|
||||
notification_data.update(extra_data)
|
||||
|
||||
return notification_data, push_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)
|
||||
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()
|
||||
data = notification_data
|
||||
notification_data.update({'badge': Notification.objects.filter(notification_to=to_user, is_read=False).count()})
|
||||
Notification.objects.create(notification_type=notification_type, notification_from=from_user,
|
||||
notification_to=to_user, data=data)
|
||||
notification_to=to_user, data=notification_data)
|
||||
if user_notification_type.push_notification:
|
||||
data.update({'badge': Notification.objects.filter(notification_to=to_user, is_read=False).count()})
|
||||
send_push(to_user, data)
|
||||
send_push(to_user, push_data)
|
||||
|
||||
|
||||
def send_push(user, data):
|
||||
""" used to send push notification to specific user """
|
||||
notification_data = data.pop('data', None)
|
||||
user.fcmdevice_set.filter(active=True).send_message(
|
||||
Message(notification=FirebaseNotification(data['title'], data['body']), data=notification_data)
|
||||
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)
|
||||
|
||||
|
||||
|
@ -11,8 +11,10 @@ from rest_framework import viewsets, status, views
|
||||
from account.utils import custom_response, custom_error_response
|
||||
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
|
||||
|
||||
|
||||
@ -54,7 +56,10 @@ class NotificationViewSet(viewsets.GenericViewSet):
|
||||
to send test notification
|
||||
:return:
|
||||
"""
|
||||
send_notification.delay(TEST_NOTIFICATION, None, request.auth.payload['user_id'], {})
|
||||
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',
|
||||
|
13
web_admin/pagination.py
Normal file
13
web_admin/pagination.py
Normal file
@ -0,0 +1,13 @@
|
||||
"""
|
||||
web_admin pagination file
|
||||
"""
|
||||
from rest_framework.pagination import PageNumberPagination
|
||||
|
||||
|
||||
class CustomPageNumberPagination(PageNumberPagination):
|
||||
"""
|
||||
custom paginator class
|
||||
"""
|
||||
page_size = 10 # Set the desired page size
|
||||
page_size_query_param = 'page_size'
|
||||
max_page_size = 100 # Set a maximum page size if needed
|
@ -224,7 +224,7 @@ class ArticleListSerializer(serializers.ModelSerializer):
|
||||
total_points = serializers.SerializerMethodField('get_total_points')
|
||||
is_completed = serializers.SerializerMethodField('get_is_completed')
|
||||
|
||||
class Meta:
|
||||
class Meta(object):
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
@ -238,9 +238,10 @@ class ArticleListSerializer(serializers.ModelSerializer):
|
||||
|
||||
def get_is_completed(self, obj):
|
||||
"""complete all question"""
|
||||
junior_article = JuniorArticle.objects.filter(article=obj).last()
|
||||
context_data = self.context.get('user')
|
||||
junior_article = JuniorArticle.objects.filter(junior__auth=context_data, article=obj).last()
|
||||
if junior_article:
|
||||
junior_article.is_completed
|
||||
return junior_article.is_completed
|
||||
return False
|
||||
|
||||
class ArticleQuestionSerializer(serializers.ModelSerializer):
|
||||
@ -278,7 +279,7 @@ class ArticleQuestionSerializer(serializers.ModelSerializer):
|
||||
return junior_article_obj.submitted_answer.id
|
||||
return None
|
||||
|
||||
class Meta:
|
||||
class Meta(object):
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
@ -299,7 +300,7 @@ class StartAssessmentSerializer(serializers.ModelSerializer):
|
||||
if data:
|
||||
return data.current_que_page
|
||||
return NUMBER['zero']
|
||||
class Meta:
|
||||
class Meta(object):
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
@ -325,7 +326,7 @@ class ArticleCardlistSerializer(serializers.ModelSerializer):
|
||||
return data.current_card_page
|
||||
return NUMBER['zero']
|
||||
|
||||
class Meta:
|
||||
class Meta(object):
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
|
@ -1,10 +1,15 @@
|
||||
"""
|
||||
web_admin analytics view file
|
||||
"""
|
||||
# python imports
|
||||
import datetime
|
||||
|
||||
# third party imports
|
||||
from rest_framework.viewsets import GenericViewSet
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
|
||||
# django imports
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.db.models import Q
|
||||
from django.db.models import Count
|
||||
@ -12,10 +17,13 @@ from django.db.models.functions import TruncDate
|
||||
from django.db.models import F, Window
|
||||
from django.db.models.functions.window import Rank
|
||||
|
||||
# local imports
|
||||
from account.utils import custom_response
|
||||
from base.constants import PENDING, IN_PROGRESS, REJECTED, REQUESTED, COMPLETED, EXPIRED, DATE_FORMAT
|
||||
from guardian.models import JuniorTask
|
||||
from junior.models import JuniorPoints
|
||||
from web_admin.pagination import CustomPageNumberPagination
|
||||
from web_admin.permission import AdminPermission
|
||||
from web_admin.serializers.analytics_serializer import LeaderboardSerializer
|
||||
|
||||
USER = get_user_model()
|
||||
@ -30,6 +38,7 @@ class AnalyticsViewSet(GenericViewSet):
|
||||
to get junior leaderboard and ranking
|
||||
"""
|
||||
serializer_class = None
|
||||
permission_classes = [IsAuthenticated, AdminPermission]
|
||||
|
||||
def get_queryset(self):
|
||||
user_qs = USER.objects.filter(
|
||||
@ -128,7 +137,7 @@ class AnalyticsViewSet(GenericViewSet):
|
||||
expression=Rank(),
|
||||
order_by=[F('total_points').desc(), 'junior__created_at']
|
||||
)).order_by('-total_points', 'junior__created_at')
|
||||
paginator = self.pagination_class()
|
||||
paginator = CustomPageNumberPagination()
|
||||
paginated_queryset = paginator.paginate_queryset(queryset, request)
|
||||
serializer = self.serializer_class(paginated_queryset, many=True)
|
||||
return custom_response(None, serializer.data)
|
||||
|
@ -221,10 +221,7 @@ class ArticleListViewSet(GenericViewSet, mixins.ListModelMixin):
|
||||
:return: list of article
|
||||
"""
|
||||
queryset = self.get_queryset()
|
||||
count = queryset.count()
|
||||
paginator = self.pagination_class()
|
||||
paginated_queryset = paginator.paginate_queryset(queryset, request)
|
||||
serializer = self.serializer_class(paginated_queryset, many=True)
|
||||
serializer = self.serializer_class(queryset, context={"user": request.user}, many=True)
|
||||
return custom_response(None, data=serializer.data)
|
||||
|
||||
class ArticleCardListViewSet(viewsets.ModelViewSet):
|
||||
|
Reference in New Issue
Block a user