Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPORT-636: Add student manager #300

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions adminpage/sport/admin/studentAdmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ def lookups(self, request, model_admin):
def queryset(self, request, queryset):
value = self.value()
if value == 'less':
return queryset.filter(complex_hours__lt=-60)
return queryset.filter(hours__lt=-60)
if value == '-31':
return queryset.filter(complex_hours__lt=-30).filter(complex_hours__gte=-60)
return queryset.filter(hours__lt=-30).filter(hours__gte=-60)
if value == '-1':
return queryset.filter(complex_hours__lt=0).filter(complex_hours__gte=-30)
return queryset.filter(hours__lt=0).filter(hours__gte=-30)
if value == '29':
return queryset.filter(complex_hours__lt=30).filter(complex_hours__gte=0)
return queryset.filter(hours__lt=30).filter(hours__gte=0)
elif value == 'more':
return queryset.filter(complex_hours__gte=30)
return queryset.filter(hours__gte=30)
return queryset


Expand All @@ -80,7 +80,7 @@ class StudentResource(resources.ModelResource):
widget=ForeignKeyWidget(Sport, "name"),
)

complex_hours = fields.Field(attribute='complex_hours', readonly=True)
hours = fields.Field(attribute='hours', readonly=True)

def get_or_init_instance(self, instance_loader, row):
student_group = get_or_create_student_group()
Expand Down Expand Up @@ -138,7 +138,7 @@ class Meta:
"student_status",
"is_online",
'sport',
'complex_hours',
'hours',
"telegram",
)
import_id_fields = ("user",)
Expand Down Expand Up @@ -234,7 +234,7 @@ def get_fields(self, request, obj=None):
"course",
"medical_group",
"sport",
"complex_hours",
"hours",
"student_status",
"write_to_telegram",
)
Expand All @@ -251,9 +251,9 @@ def write_to_telegram(self, obj):
obj.telegram
)

def complex_hours(self, obj: Student):
return obj.complex_hours
complex_hours.admin_order_field = 'complex_hours'
def hours(self, obj: Student):
return obj.hours
hours.admin_order_field = 'hours'

ordering = (
"user__first_name",
Expand All @@ -278,24 +278,24 @@ def change_view(self, request, object_id, form_url='', extra_context=None):
self.inlines = (ViewAttendanceInline,)
return super().change_view(request, object_id, form_url, extra_context)

def get_queryset(self, request):
qs = super().get_queryset(request)

qs = qs.annotate(_debt=Coalesce(
SumSubquery(Debt.objects.filter(semester_id=get_ongoing_semester().pk,
student_id=OuterRef("pk")), 'debt'),
0
))
qs = qs.annotate(_ongoing_semester_hours=Coalesce(
SumSubquery(Attendance.objects.filter(
training__group__semester_id=get_ongoing_semester().pk, student_id=OuterRef("pk")), 'hours'),
0
))
qs = qs.annotate(complex_hours=ExpressionWrapper(
F('_ongoing_semester_hours') - F('_debt'), output_field=IntegerField()
))

return qs
# def get_queryset(self, request):
# qs = super().get_queryset(request)
#
# qs = qs.annotate(_debt=Coalesce(
# SumSubquery(Debt.objects.filter(semester_id=get_ongoing_semester().pk,
# student_id=OuterRef("pk")), 'debt'),
# 0
# ))
# qs = qs.annotate(_ongoing_semester_hours=Coalesce(
# SumSubquery(Attendance.objects.filter(
# training__group__semester_id=get_ongoing_semester().pk, student_id=OuterRef("pk")), 'hours'),
# 0
# ))
# qs = qs.annotate(complex_hours=ExpressionWrapper(
# F('_ongoing_semester_hours') - F('_debt'), output_field=IntegerField()
# ))
#
# return qs
FleshRazer marked this conversation as resolved.
Show resolved Hide resolved

def get_hijack_user(self, obj):
return obj.user
Expand Down
40 changes: 40 additions & 0 deletions adminpage/sport/models/student.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
from django.core.mail import send_mail
from django.db import models
from django.core.exceptions import ValidationError
from django.db.models import OuterRef, ExpressionWrapper, F
from django.db.models.functions import Coalesce
from django.db.models.signals import post_save
from django.dispatch.dispatcher import receiver
from django.db.models import IntegerField
from tinymce.models import HTMLField

# from api.crud import SumSubquery, get_ongoing_semester
FleshRazer marked this conversation as resolved.
Show resolved Hide resolved
from sport.models import MedicalGroupHistory, Gender
from sport.utils import get_current_study_year

Expand All @@ -17,6 +21,40 @@ def validate_course(course):
raise ValidationError('Course is bounded by 1 and 4')


class StudentManager(models.Manager):
def get_queryset(self):
from api.crud import get_ongoing_semester
from api.crud import SumSubquery
from sport.models import Attendance, Debt
from django.db.utils import ProgrammingError

qs = super().get_queryset()

try:
qs = qs.annotate(_debt=Coalesce(
SumSubquery(Debt.objects.filter(
semester_id=get_ongoing_semester().pk,
student_id=OuterRef("pk")),
'debt',
),
0
))
qs = qs.annotate(_ongoing_semester_hours=Coalesce(
SumSubquery(Attendance.objects.filter(
training__group__semester_id=get_ongoing_semester().pk,
student_id=OuterRef("pk")),
'hours',
),
0
))
qs = qs.annotate(hours=ExpressionWrapper(
F('_ongoing_semester_hours') - F('_debt'), output_field=IntegerField()
))
except (ProgrammingError, IndexError):
pass

return qs

class Student(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
Expand Down Expand Up @@ -78,6 +116,8 @@ class Student(models.Model):

comment = HTMLField(null=True, blank=True, default='')

objects = StudentManager()

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__original_medical_group = self.medical_group
Expand Down