Skip to content

Commit

Permalink
SPORT-636: Handle 'relation semester does not exist'
Browse files Browse the repository at this point in the history
  • Loading branch information
FleshRazer committed Feb 5, 2023
1 parent d8fc21c commit 0f1fddb
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions adminpage/sport/models/student.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,36 @@ def validate_course(course):


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

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:
pass

return qs

Expand Down

0 comments on commit 0f1fddb

Please sign in to comment.