Files
zod-backend/junior/utils.py

83 lines
2.9 KiB
Python

"""Account utils"""
"""Import django"""
from django.conf import settings
"""Third party Django app"""
from templated_email import send_templated_mail
from .models import JuniorPoints
from base.constants import NUMBER
from django.db.models import F, Window
from django.db.models.functions.window import Rank
# junior notification
# email for sending email
# when guardian create junior profile
# guardian get email when junior send
# request for approving the profile and
# being part of the zod bank and access the platform
# define junior notification email
# junior approval email
from celery import shared_task
@shared_task()
def junior_notification_email(recipient_email, full_name, email, password):
"""Notification email"""
from_email = settings.EMAIL_FROM_ADDRESS
# recipient email"""
recipient_list = [recipient_email]
# use send template mail for sending email"""
send_templated_mail(
template_name='junior_notification_email.email',
from_email=from_email,
recipient_list=recipient_list,
context={
'full_name': full_name,
'url':'link',
'email': email,
'password': password
}
)
return full_name
@shared_task()
def junior_approval_mail(guardian, full_name):
"""junior approval mail"""
from_email = settings.EMAIL_FROM_ADDRESS
recipient_list = [guardian]
# use send template mail for sending email"""
send_templated_mail(
template_name='junior_approval_mail.email',
from_email=from_email,
recipient_list=recipient_list,
context={
'full_name': full_name
}
)
return full_name
def update_positions_based_on_points():
"""Update position of the junior"""
# First, retrieve all the JuniorPoints instances ordered by total_points in descending order.
juniors_points = JuniorPoints.objects.order_by('-total_points', 'created_at')
# Now, iterate through the queryset and update the position field based on the order.
position = NUMBER['one']
for junior_point in juniors_points:
junior_point.position = position
junior_point.save()
position += NUMBER['one']
def get_junior_leaderboard_rank(junior_obj):
"""
to get junior's position/rank
:param junior_obj:
:return: junior's position/rank
"""
queryset = JuniorPoints.objects.filter(
junior__is_verified=True
).select_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')
junior = next((query for query in queryset if query.junior == junior_obj), None)
return junior.rank if junior else None