Better Entry measures serialization (#11)

This commit is contained in:
Ethan Roseman 2022-08-26 11:16:04 +09:00 committed by GitHub
parent 686e15385e
commit 3b62d63fbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 17 deletions

View File

@ -24,7 +24,10 @@ class MeasureSerializer(serializers.HyperlinkedModelSerializer):
class EntrySerializer(serializers.HyperlinkedModelSerializer):
measures = MeasureSerializer(many=True)
measures = serializers.SerializerMethodField()
def get_measures(self, instance: Entry) -> dict[str, int]:
return {m.type: m.value for m in instance.measures.all()}
class Meta:
model = Entry
@ -33,14 +36,3 @@ class EntrySerializer(serializers.HyperlinkedModelSerializer):
"git_hash",
"measures",
]
class TerseEntrySerializer(serializers.HyperlinkedModelSerializer):
measures = MeasureSerializer(many=True)
class Meta:
model = Entry
fields = [
"timestamp",
"measures",
]

View File

@ -7,7 +7,7 @@ from frog_api.exceptions import (
NoEntriesException,
)
from frog_api.models import Entry, Measure, Project, Version
from frog_api.serializers import TerseEntrySerializer
from frog_api.serializers import EntrySerializer
from frog_api.views.common import (
get_category,
get_project,
@ -32,10 +32,7 @@ def get_latest_entry(
if entry is None:
raise NoEntriesException(project_slug, version_slug, category_slug)
# Re-format the measures (TODO: handle this in a DRF serializer)
entry_data = TerseEntrySerializer(entry).data
entry_data["measures"] = {m["type"]: m["value"] for m in entry_data["measures"]}
return entry_data
return EntrySerializer(entry).data
def get_versions_digest_for_project(project: Project) -> dict[Any, Any]: