From 57fce402f9458cc89c7969a28f57924802bd8651 Mon Sep 17 00:00:00 2001 From: andrepapoti Date: Mon, 18 Mar 2024 11:37:21 -0300 Subject: [PATCH] serializers: Add 'related_versions' field to series serializer For two series to be linked togheter they must belong to the same project Closes #506 Signed-off-by: andrepapoti --- patchwork/api/series.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/patchwork/api/series.py b/patchwork/api/series.py index b88ed1f5..f95ff0d5 100644 --- a/patchwork/api/series.py +++ b/patchwork/api/series.py @@ -6,6 +6,7 @@ from rest_framework.generics import ListAPIView from rest_framework.generics import RetrieveAPIView from rest_framework.serializers import SerializerMethodField +from rest_framework.exceptions import ValidationError from patchwork.api.base import BaseHyperlinkedModelSerializer from patchwork.api.base import PatchworkPermission @@ -14,6 +15,7 @@ from patchwork.api.embedded import PatchSerializer from patchwork.api.embedded import PersonSerializer from patchwork.api.embedded import ProjectSerializer +from patchwork.api.embedded import SeriesSerializer as RelatedSeriesSerializer from patchwork.models import Series @@ -24,6 +26,14 @@ class SeriesSerializer(BaseHyperlinkedModelSerializer): mbox = SerializerMethodField() cover_letter = CoverSerializer(read_only=True) patches = PatchSerializer(read_only=True, many=True) + related_series = RelatedSeriesSerializer(many=True) + + def get_related_series(self, obj): + urls = [] + for related_series in obj.related_series.all(): + url = self.get_web_url(related_series) + urls.append(url) + return urls def get_web_url(self, instance): request = self.context.get('request') @@ -33,6 +43,16 @@ def get_mbox(self, instance): request = self.context.get('request') return request.build_absolute_uri(instance.get_mbox_url()) + def validate_related_series(self, related_series): + for series in related_series: + if self.instance.id != series.id: + raise ValidationError('A series cannot be linked to itself.') + if self.instance.project.id != series.project.id: + raise ValidationError( + 'Series must belong to the same project.' + ) + return related_series + class Meta: model = Series fields = ( @@ -44,6 +64,7 @@ class Meta: 'date', 'submitter', 'version', + 'related_series', 'total', 'received_total', 'received_all',