Skip to content

Commit

Permalink
serializers: Add 'related_versions' field to series serializer
Browse files Browse the repository at this point in the history
For two series to be linked togheter they must belong to the same project

Closes getpatchwork#506

Signed-off-by: andrepapoti <[email protected]>
  • Loading branch information
andrepapoti committed Mar 19, 2024
1 parent ad538d3 commit 57fce40
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions patchwork/api/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand All @@ -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')
Expand All @@ -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 = (
Expand All @@ -44,6 +64,7 @@ class Meta:
'date',
'submitter',
'version',
'related_series',
'total',
'received_total',
'received_all',
Expand Down

0 comments on commit 57fce40

Please sign in to comment.