frogress/frog_api/serializers/model_serializers.py

46 lines
1.1 KiB
Python
Raw Normal View History

2022-08-19 10:35:05 +00:00
from rest_framework import serializers
2022-08-22 02:01:56 +00:00
from frog_api.models import Entry, Measure, Project, Version
2022-08-19 10:35:05 +00:00
2022-08-21 11:25:44 +00:00
class VersionSerializer(serializers.HyperlinkedModelSerializer):
2022-08-19 10:35:05 +00:00
class Meta:
2022-08-21 11:25:44 +00:00
model = Version
2022-08-19 10:35:05 +00:00
fields = ["slug", "name"]
2022-08-21 11:25:44 +00:00
class ProjectSerializer(serializers.HyperlinkedModelSerializer):
versions = VersionSerializer(many=True)
2022-08-19 10:35:05 +00:00
class Meta:
2022-08-21 11:25:44 +00:00
model = Project
fields = [
"slug",
"name",
"versions",
"repository",
"discord",
"website",
]
2022-08-19 10:35:05 +00:00
2022-08-22 02:01:56 +00:00
class MeasureSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Measure
fields = ["type", "value"]
2022-08-19 10:35:05 +00:00
class EntrySerializer(serializers.HyperlinkedModelSerializer):
measures = serializers.SerializerMethodField()
2022-08-21 11:25:44 +00:00
def get_measures(self, instance: Entry) -> dict[str, int]:
return {m.type: m.value for m in instance.measures.all()}
2022-08-22 02:01:56 +00:00
2022-08-21 11:25:44 +00:00
class Meta:
model = Entry
fields = [
"timestamp",
"git_hash",
2022-08-22 02:01:56 +00:00
"measures",
2022-08-21 11:25:44 +00:00
]