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

Apply ordering of CIVs to API serializations #3768

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions app/grandchallenge/components/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
ComponentInterface,
ComponentInterfaceValue,
)
from grandchallenge.components.templatetags.civ import sort_civs
from grandchallenge.core.guardian import filter_by_permission
from grandchallenge.uploads.models import UserUpload
from grandchallenge.workstation_configs.serializers import (
Expand Down Expand Up @@ -162,6 +163,12 @@ def validate(self, attrs):
return attrs


class SortedCIVSerializer(serializers.ListSerializer):
def to_representation(self, data):
sorted_data = sort_civs(data.all())
return super().to_representation(sorted_data)
Comment on lines +166 to +169
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but maybe add a test for this new serializer?



class ComponentInterfaceValueSerializer(serializers.ModelSerializer):
# Serializes images in place rather than with hyperlinks for internal usage
image = SimpleImageSerializer(required=False)
Expand All @@ -170,6 +177,7 @@ class ComponentInterfaceValueSerializer(serializers.ModelSerializer):
class Meta:
model = ComponentInterfaceValue
fields = ["interface", "value", "file", "image", "pk"]
list_serializer_class = SortedCIVSerializer


class HyperlinkedComponentInterfaceValueSerializer(
Expand Down
4 changes: 2 additions & 2 deletions app/grandchallenge/components/templatetags/civ.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ def sort_civs(civs: Iterable[ComponentInterfaceValue]):
charts.append(v)
else:
values.append(v)
elif v.file:
elif v.interface.is_file_kind:
if v.interface.is_thumbnail_kind:
thumbnails.append(v)
else:
files.append(v)
elif v.image:
elif v.interface.is_image_kind:
images.append(v)
else:
residual.append(v)
Expand Down
Loading