diff --git a/apiserver/apiserver/api/models.py b/apiserver/apiserver/api/models.py index 7b3dda6..a34780f 100644 --- a/apiserver/apiserver/api/models.py +++ b/apiserver/apiserver/api/models.py @@ -144,7 +144,7 @@ class Training(models.Model): class Interest(models.Model): user = models.ForeignKey(User, related_name='interests', null=True, on_delete=models.SET_NULL) - course = models.ForeignKey(Course, related_name='courses', null=True, on_delete=models.SET_NULL) + course = models.ForeignKey(Course, related_name='interests', null=True, on_delete=models.SET_NULL) satisfied_by = models.ForeignKey(Session, related_name='satisfies', null=True, on_delete=models.SET_NULL) diff --git a/apiserver/apiserver/api/serializers.py b/apiserver/apiserver/api/serializers.py index ec3efa0..de055bf 100644 --- a/apiserver/apiserver/api/serializers.py +++ b/apiserver/apiserver/api/serializers.py @@ -462,9 +462,11 @@ class StudentTrainingSerializer(TrainingSerializer): class CourseSerializer(serializers.ModelSerializer): + num_interested = serializers.IntegerField(read_only=True) + class Meta: model = models.Course - fields = ['id', 'name', 'is_old', 'description', 'tags'] + fields = ['id', 'name', 'is_old', 'description', 'tags', 'num_interested'] class SessionSerializer(serializers.ModelSerializer): student_count = serializers.SerializerMethodField() diff --git a/apiserver/apiserver/api/views.py b/apiserver/apiserver/api/views.py index a0dcada..1f53e8d 100644 --- a/apiserver/apiserver/api/views.py +++ b/apiserver/apiserver/api/views.py @@ -238,7 +238,13 @@ class CardViewSet(Base, Create, Retrieve, Update, Destroy): # https://stackoverflow.com/a/58689019 class CourseViewSet(Base, List, Retrieve, Create, Update): permission_classes = [AllowMetadata | IsAuthenticatedOrReadOnly, IsAdminOrReadOnly | IsInstructorOrReadOnly] - queryset = models.Course.objects.annotate(date=Max('sessions__datetime')).order_by('-date') + queryset = models.Course.objects.annotate( + date=Max('sessions__datetime'), + num_interested=Count('interests', filter=Q(interests__satisfied_by__isnull=True), distinct=True), + ).order_by( + '-num_interested', + '-date', + ) def get_serializer_class(self): if self.action == 'list': diff --git a/webclient/src/Courses.js b/webclient/src/Courses.js index 0526082..83addbe 100644 --- a/webclient/src/Courses.js +++ b/webclient/src/Courses.js @@ -98,6 +98,7 @@ export function Courses(props) {