Re: [PATCH 2/2] REST: Add 'web_url' link to API responses
This series all looks good to me - I'm always a fan of tests and I think a web_url is a win for usability. Regards, Daniel Stephen Finucane writes: > This provides an easy way for clients to navigate to the web view. The > URL is added to four resources: bundles, comments, cover letters and > series. We could also extend this to projects and users in the future, > but the latter would require renaming an existing property while the > latter would require a public "user" page which does not currently > exists. > > Signed-off-by: Stephen Finucane > --- > Unless anyone complains, I'm probably going to merge this within a day > or two and cut 2.1. This particular shortcoming has been a constant > annoyance for me and I should have addressed this sooner than I did. > --- > patchwork/api/bundle.py| 16 +--- > patchwork/api/comment.py | 12 +++-- > patchwork/api/cover.py | 11 ++--- > patchwork/api/embedded.py | 39 +++--- > patchwork/api/patch.py | 13 +++--- > patchwork/api/series.py| 18 ++ > patchwork/models.py| 12 + > patchwork/tests/api/test_bundle.py | 29 +++--- > patchwork/tests/api/test_cover.py | 15 +--- > patchwork/tests/api/test_patch.py | 16 +++- > patchwork/tests/api/test_series.py | 18 ++ > 11 files changed, 165 insertions(+), 34 deletions(-) > > diff --git a/patchwork/api/bundle.py b/patchwork/api/bundle.py > index 733e4881..b0005daa 100644 > --- a/patchwork/api/bundle.py > +++ b/patchwork/api/bundle.py > @@ -20,9 +20,9 @@ > from django.db.models import Q > from rest_framework.generics import ListAPIView > from rest_framework.generics import RetrieveAPIView > -from rest_framework.serializers import HyperlinkedModelSerializer > from rest_framework.serializers import SerializerMethodField > > +from patchwork.api.base import BaseHyperlinkedModelSerializer > from patchwork.api.base import PatchworkPermission > from patchwork.api.filters import BundleFilterSet > from patchwork.api.embedded import PatchSerializer > @@ -32,22 +32,30 @@ from patchwork.compat import is_authenticated > from patchwork.models import Bundle > > > -class BundleSerializer(HyperlinkedModelSerializer): > +class BundleSerializer(BaseHyperlinkedModelSerializer): > > +web_url = SerializerMethodField() > project = ProjectSerializer(read_only=True) > mbox = SerializerMethodField() > owner = UserSerializer(read_only=True) > patches = PatchSerializer(many=True, read_only=True) > > +def get_web_url(self, instance): > +request = self.context.get('request') > +return request.build_absolute_uri(instance.get_absolute_url()) > + > def get_mbox(self, instance): > request = self.context.get('request') > return request.build_absolute_uri(instance.get_mbox_url()) > > class Meta: > model = Bundle > -fields = ('id', 'url', 'project', 'name', 'owner', 'patches', > - 'public', 'mbox') > +fields = ('id', 'url', 'web_url', 'project', 'name', 'owner', > + 'patches', 'public', 'mbox') > read_only_fields = ('owner', 'patches', 'mbox') > +versioned_fields = { > +'1.1': ('web_url', ), > +} > extra_kwargs = { > 'url': {'view_name': 'api-bundle-detail'}, > } > diff --git a/patchwork/api/comment.py b/patchwork/api/comment.py > index fbb7cc83..5a5adb1d 100644 > --- a/patchwork/api/comment.py > +++ b/patchwork/api/comment.py > @@ -30,10 +30,15 @@ from patchwork.models import Comment > > class CommentListSerializer(BaseHyperlinkedModelSerializer): > > +web_url = SerializerMethodField() > subject = SerializerMethodField() > headers = SerializerMethodField() > submitter = PersonSerializer(read_only=True) > > +def get_web_url(self, instance): > +request = self.context.get('request') > +return request.build_absolute_uri(instance.get_absolute_url()) > + > def get_subject(self, comment): > return email.parser.Parser().parsestr(comment.headers, >True).get('Subject', '') > @@ -54,9 +59,12 @@ class > CommentListSerializer(BaseHyperlinkedModelSerializer): > > class Meta: > model = Comment > -fields = ('id', 'msgid', 'date', 'subject', 'submitter', 'content', > - 'headers') > +fields = ('id', 'web_url', 'msgid', 'date', 'subject', 'submitter', > + 'content', 'headers') > read_only_fields = fields > +versioned_fields = { > +'1.1': ('web_url', ), > +} > > > class CommentList(ListAPIView): > diff --git a/patchwork/api/cover.py b/patchwork/api/cover.py > index 246b7cb9..b497fd85 100644 > --- a/patchwork/api/cover.py > +++ b/patchwork/api/cover.py >
[PATCH 2/2] REST: Add 'web_url' link to API responses
This provides an easy way for clients to navigate to the web view. The URL is added to four resources: bundles, comments, cover letters and series. We could also extend this to projects and users in the future, but the latter would require renaming an existing property while the latter would require a public "user" page which does not currently exists. Signed-off-by: Stephen Finucane --- Unless anyone complains, I'm probably going to merge this within a day or two and cut 2.1. This particular shortcoming has been a constant annoyance for me and I should have addressed this sooner than I did. --- patchwork/api/bundle.py| 16 +--- patchwork/api/comment.py | 12 +++-- patchwork/api/cover.py | 11 ++--- patchwork/api/embedded.py | 39 +++--- patchwork/api/patch.py | 13 +++--- patchwork/api/series.py| 18 ++ patchwork/models.py| 12 + patchwork/tests/api/test_bundle.py | 29 +++--- patchwork/tests/api/test_cover.py | 15 +--- patchwork/tests/api/test_patch.py | 16 +++- patchwork/tests/api/test_series.py | 18 ++ 11 files changed, 165 insertions(+), 34 deletions(-) diff --git a/patchwork/api/bundle.py b/patchwork/api/bundle.py index 733e4881..b0005daa 100644 --- a/patchwork/api/bundle.py +++ b/patchwork/api/bundle.py @@ -20,9 +20,9 @@ from django.db.models import Q from rest_framework.generics import ListAPIView from rest_framework.generics import RetrieveAPIView -from rest_framework.serializers import HyperlinkedModelSerializer from rest_framework.serializers import SerializerMethodField +from patchwork.api.base import BaseHyperlinkedModelSerializer from patchwork.api.base import PatchworkPermission from patchwork.api.filters import BundleFilterSet from patchwork.api.embedded import PatchSerializer @@ -32,22 +32,30 @@ from patchwork.compat import is_authenticated from patchwork.models import Bundle -class BundleSerializer(HyperlinkedModelSerializer): +class BundleSerializer(BaseHyperlinkedModelSerializer): +web_url = SerializerMethodField() project = ProjectSerializer(read_only=True) mbox = SerializerMethodField() owner = UserSerializer(read_only=True) patches = PatchSerializer(many=True, read_only=True) +def get_web_url(self, instance): +request = self.context.get('request') +return request.build_absolute_uri(instance.get_absolute_url()) + def get_mbox(self, instance): request = self.context.get('request') return request.build_absolute_uri(instance.get_mbox_url()) class Meta: model = Bundle -fields = ('id', 'url', 'project', 'name', 'owner', 'patches', - 'public', 'mbox') +fields = ('id', 'url', 'web_url', 'project', 'name', 'owner', + 'patches', 'public', 'mbox') read_only_fields = ('owner', 'patches', 'mbox') +versioned_fields = { +'1.1': ('web_url', ), +} extra_kwargs = { 'url': {'view_name': 'api-bundle-detail'}, } diff --git a/patchwork/api/comment.py b/patchwork/api/comment.py index fbb7cc83..5a5adb1d 100644 --- a/patchwork/api/comment.py +++ b/patchwork/api/comment.py @@ -30,10 +30,15 @@ from patchwork.models import Comment class CommentListSerializer(BaseHyperlinkedModelSerializer): +web_url = SerializerMethodField() subject = SerializerMethodField() headers = SerializerMethodField() submitter = PersonSerializer(read_only=True) +def get_web_url(self, instance): +request = self.context.get('request') +return request.build_absolute_uri(instance.get_absolute_url()) + def get_subject(self, comment): return email.parser.Parser().parsestr(comment.headers, True).get('Subject', '') @@ -54,9 +59,12 @@ class CommentListSerializer(BaseHyperlinkedModelSerializer): class Meta: model = Comment -fields = ('id', 'msgid', 'date', 'subject', 'submitter', 'content', - 'headers') +fields = ('id', 'web_url', 'msgid', 'date', 'subject', 'submitter', + 'content', 'headers') read_only_fields = fields +versioned_fields = { +'1.1': ('web_url', ), +} class CommentList(ListAPIView): diff --git a/patchwork/api/cover.py b/patchwork/api/cover.py index 246b7cb9..b497fd85 100644 --- a/patchwork/api/cover.py +++ b/patchwork/api/cover.py @@ -34,12 +34,17 @@ from patchwork.models import CoverLetter class CoverLetterListSerializer(BaseHyperlinkedModelSerializer): +web_url = SerializerMethodField() project = ProjectSerializer(read_only=True) submitter = PersonSerializer(read_only=True) mbox = SerializerMethodField() series = SeriesSerializer(many=True, read_only=True) comments =