Skip to content

Commit

Permalink
Merge pull request #102 from NIAEFEUP/fix/multple-professor-lessons
Browse files Browse the repository at this point in the history
Fix class/ endpoint to return all professors
  • Loading branch information
tomaspalma committed Aug 23, 2024
2 parents 9f2a23b + dd4f6ab commit 73c906f
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions django/university/controllers/ClassController.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
from university.models import Class, SlotClass, Slot, SlotProfessor
from university.models import Class, Professor, Slot, SlotProfessor


class ClassController:
@staticmethod
def get_professors(slot_obj):
try:
professor = slot_obj.slotprofessor.professor
professors = [
{'id': professor.id, 'acronym': professor.professor_acronym, 'name': professor.professor_name}]
except SlotProfessor.DoesNotExist:
professors = []
def get_professors(slot):
slot_professors = SlotProfessor.objects.filter(slot_id=slot.id).values()

professors = []

for slot_professor in slot_professors:
professor = Professor.objects.get(id=slot_professor['professor_id'])
professors.append({
'id': professor.id,
'acronym': professor.professor_acronym,
'name': professor.professor_name
})

return {
'id': slot_obj.id,
'lesson_type': slot_obj.lesson_type,
'day': slot_obj.day,
'start_time': float(slot_obj.start_time),
'duration': float(slot_obj.duration),
'location': slot_obj.location,
'is_composed': slot_obj.is_composed,
'id': slot.id,
'lesson_type': slot.lesson_type,
'day': slot.day,
'start_time': float(slot.start_time),
'duration': float(slot.duration),
'location': slot.location,
'is_composed': slot.is_composed,
'professors': professors
}

Expand All @@ -30,8 +35,7 @@ def get_classes(course_unit_id: int):
result = []
for class_obj in classes:
slot_ids = [sc.slot_id for sc in class_obj.slotclass_set.all()]
slots = Slot.objects.filter(id__in=slot_ids).prefetch_related(
'slotprofessor__professor')
slots = Slot.objects.filter(id__in=slot_ids)

slot_list = list(map(ClassController.get_professors, slots))

Expand Down

0 comments on commit 73c906f

Please sign in to comment.