Display number of interests in course list

This commit is contained in:
Tanner Collin 2022-05-04 20:36:55 +00:00
parent f252d7882d
commit 2dd2d8dc41
4 changed files with 15 additions and 3 deletions

View File

@ -144,7 +144,7 @@ class Training(models.Model):
class Interest(models.Model): class Interest(models.Model):
user = models.ForeignKey(User, related_name='interests', null=True, on_delete=models.SET_NULL) 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) satisfied_by = models.ForeignKey(Session, related_name='satisfies', null=True, on_delete=models.SET_NULL)

View File

@ -462,9 +462,11 @@ class StudentTrainingSerializer(TrainingSerializer):
class CourseSerializer(serializers.ModelSerializer): class CourseSerializer(serializers.ModelSerializer):
num_interested = serializers.IntegerField(read_only=True)
class Meta: class Meta:
model = models.Course model = models.Course
fields = ['id', 'name', 'is_old', 'description', 'tags'] fields = ['id', 'name', 'is_old', 'description', 'tags', 'num_interested']
class SessionSerializer(serializers.ModelSerializer): class SessionSerializer(serializers.ModelSerializer):
student_count = serializers.SerializerMethodField() student_count = serializers.SerializerMethodField()

View File

@ -238,7 +238,13 @@ class CardViewSet(Base, Create, Retrieve, Update, Destroy):
# https://stackoverflow.com/a/58689019 # https://stackoverflow.com/a/58689019
class CourseViewSet(Base, List, Retrieve, Create, Update): class CourseViewSet(Base, List, Retrieve, Create, Update):
permission_classes = [AllowMetadata | IsAuthenticatedOrReadOnly, IsAdminOrReadOnly | IsInstructorOrReadOnly] 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): def get_serializer_class(self):
if self.action == 'list': if self.action == 'list':

View File

@ -98,6 +98,7 @@ export function Courses(props) {
<Table.Header> <Table.Header>
<Table.Row> <Table.Row>
<Table.HeaderCell>Name</Table.HeaderCell> <Table.HeaderCell>Name</Table.HeaderCell>
<Table.HeaderCell>Interest</Table.HeaderCell>
<Table.HeaderCell></Table.HeaderCell> <Table.HeaderCell></Table.HeaderCell>
</Table.Row> </Table.Row>
</Table.Header> </Table.Header>
@ -109,6 +110,9 @@ export function Courses(props) {
<Table.Cell> <Table.Cell>
<Link to={'/courses/'+x.id}>{x.name}</Link> <Link to={'/courses/'+x.id}>{x.name}</Link>
</Table.Cell> </Table.Cell>
<Table.Cell>
{x.num_interested}
</Table.Cell>
<Table.Cell> <Table.Cell>
{!!x.tags && x.tags.split(',').map(name => {!!x.tags && x.tags.split(',').map(name =>
<Label color={tags[name]} tag> <Label color={tags[name]} tag>