mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-16 18:36:18 +00:00
jira-15 top leadership API
This commit is contained in:
@ -13,7 +13,7 @@ from account.serializers import JuniorSerializer
|
||||
from junior.serializers import JuniorDetailSerializer
|
||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||
from .utils import upload_image_to_alibaba
|
||||
from junior.models import Junior
|
||||
from junior.models import Junior, JuniorPoints
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
"""User serializer"""
|
||||
auth_token = serializers.SerializerMethodField('get_auth_token')
|
||||
@ -181,37 +181,13 @@ class TaskDetailsSerializer(serializers.ModelSerializer):
|
||||
'junior', 'task_status', 'is_active', 'created_at','updated_at']
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class TopJuniorSerializer(serializers.ModelSerializer):
|
||||
total_points = serializers.SerializerMethodField()
|
||||
|
||||
email = serializers.SerializerMethodField('get_auth')
|
||||
first_name = serializers.SerializerMethodField('get_first_name')
|
||||
last_name = serializers.SerializerMethodField('get_last_name')
|
||||
|
||||
def get_auth(self, obj):
|
||||
return obj.auth.username
|
||||
|
||||
def get_first_name(self, obj):
|
||||
return obj.auth.first_name
|
||||
|
||||
def get_last_name(self, obj):
|
||||
return obj.auth.last_name
|
||||
junior = JuniorDetailSerializer()
|
||||
|
||||
class Meta:
|
||||
model = Junior
|
||||
fields = ['id', 'email', 'first_name', 'last_name', 'phone', 'country_code', 'country_name', 'gender', 'dob', 'image', 'junior_code', 'guardian_code', 'referral_code', 'referral_code_used', 'is_active', 'is_complete_profile', 'passcode', 'is_verified', 'created_at', 'updated_at', 'total_points']
|
||||
model = JuniorPoints
|
||||
fields = ['id', 'junior', 'total_task_points', 'created_at', 'updated_at']
|
||||
|
||||
def get_total_points(self, obj):
|
||||
junior_ids_with_total_points = self.context.get('junior_ids_with_total_points')
|
||||
if junior_ids_with_total_points:
|
||||
junior_id = obj.id
|
||||
for item in junior_ids_with_total_points:
|
||||
if item['junior'] == junior_id:
|
||||
return item['total_points']
|
||||
return 0
|
||||
|
||||
class GuardianProfileSerializer(serializers.ModelSerializer):
|
||||
"""junior serializer"""
|
||||
|
@ -10,7 +10,7 @@ from datetime import datetime, timedelta
|
||||
from .serializers import (UserSerializer, CreateGuardianSerializer, TaskSerializer, TaskDetailsSerializer,
|
||||
TopJuniorSerializer)
|
||||
from .models import Guardian, JuniorTask
|
||||
from junior.models import Junior
|
||||
from junior.models import Junior, JuniorPoints
|
||||
from junior.serializers import JuniorDetailSerializer
|
||||
from account.models import UserEmailOtp
|
||||
from .tasks import generate_otp
|
||||
@ -139,23 +139,15 @@ class SearchTaskListAPIView(viewsets.ModelViewSet):
|
||||
serializer = TaskDetailsSerializer(paginated_queryset, many=True)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
class TopJuniorListAPIView(viewsets.ModelViewSet):
|
||||
"""Top juniors list"""
|
||||
serializer_class = TopJuniorSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
queryset = JuniorTask.objects.all()
|
||||
queryset = JuniorPoints.objects.all()
|
||||
def list(self, request, *args, **kwargs):
|
||||
"""fetch junior list those complete their task"""
|
||||
junior_ids_with_total_points = JuniorTask.objects.filter(task_status=1) \
|
||||
.values('junior') \
|
||||
.annotate(total_points=Sum('points')) \
|
||||
.order_by('-total_points')
|
||||
|
||||
junior_ids = [item['junior'] for item in junior_ids_with_total_points]
|
||||
|
||||
juniors = Junior.objects.filter(id__in=junior_ids)
|
||||
|
||||
serializer = self.get_serializer(juniors, many=True, context={'junior_ids_with_total_points':
|
||||
junior_ids_with_total_points})
|
||||
junior_total_points = self.get_queryset().order_by('-total_task_points')
|
||||
serializer = self.get_serializer(junior_total_points, many=True)
|
||||
return custom_response(serializer.data, response_status=status.HTTP_200_OK)
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
"""Third party Django app"""
|
||||
from django.contrib import admin
|
||||
"""Import Django app"""
|
||||
from .models import Junior
|
||||
from .models import Junior, JuniorPoints
|
||||
# Register your models here.
|
||||
@admin.register(Junior)
|
||||
class JuniorAdmin(admin.ModelAdmin):
|
||||
@ -12,3 +12,12 @@ class JuniorAdmin(admin.ModelAdmin):
|
||||
def __str__(self):
|
||||
"""Return email id"""
|
||||
return self.auth__email
|
||||
|
||||
@admin.register(JuniorPoints)
|
||||
class JuniorPointsAdmin(admin.ModelAdmin):
|
||||
"""Junior Points Admin"""
|
||||
list_display = ['junior', 'total_task_points']
|
||||
|
||||
def __str__(self):
|
||||
"""Return email id"""
|
||||
return self.junior.auth.email
|
||||
|
28
junior/migrations/0008_juniorpoints.py
Normal file
28
junior/migrations/0008_juniorpoints.py
Normal file
@ -0,0 +1,28 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-09 12:40
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('junior', '0007_alter_junior_image'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='JuniorPoints',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('total_task_points', models.IntegerField(blank=True, default=0, null=True)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('junior', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='junior_points', to='junior.junior')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Junior Task Points',
|
||||
'db_table': 'junior_task_points',
|
||||
},
|
||||
),
|
||||
]
|
@ -41,3 +41,21 @@ class Junior(models.Model):
|
||||
def __str__(self):
|
||||
"""Return email id"""
|
||||
return f'{self.auth}'
|
||||
|
||||
class JuniorPoints(models.Model):
|
||||
"""Junior model"""
|
||||
junior = models.OneToOneField(Junior, on_delete=models.CASCADE, related_name='junior_points')
|
||||
"""Contact details"""
|
||||
total_task_points = models.IntegerField(blank=True, null=True, default=0)
|
||||
"""Profile created and updated time"""
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta(object):
|
||||
""" Meta class """
|
||||
db_table = 'junior_task_points'
|
||||
verbose_name = 'Junior Task Points'
|
||||
|
||||
def __str__(self):
|
||||
"""Return email id"""
|
||||
return f'{self.junior.auth}'
|
||||
|
Reference in New Issue
Block a user