class PostSchema(Schema):
    id = fields.Str(dump_only=True)
    title = fields.Str()

    author = HyperlinkRelated(
        endpoint='author_detail',
        url_kwargs={
            'author_id': '<author.id>',
            '_external': True
        },
        include_data=True,
        type_='people',
    )

    comments = HyperlinkRelated(endpoint='posts_comments',
                                url_kwargs={
                                    'post_id': '<id>',
                                    '_external': True
                                },
                                many=True,
                                include_data=True,
                                type_='comments')

    def get_top_level_links(self, data, many):
        if many:
            self_link = url_for('posts_list', _external=True)
        else:
            self_link = url_for('posts_detail',
                                post_id=data['id'],
                                _external=True)
        return {'self': self_link}

    class Meta:
        type_ = 'posts'
Example #2
0
 def test_serialize_basic(self, app, post):
     field = HyperlinkRelated(
         endpoint='posts_comments',
         url_kwargs={'post_id': '<id>'},
     )
     result = field.serialize('comments', post)
     assert 'comments' in result
     related = result['comments']['links']['related']
     assert related == url_for('posts_comments', post_id=post.id)
Example #3
0
 def test_serialize_external(self, app, post):
     field = HyperlinkRelated(
         endpoint='posts_comments',
         url_kwargs={
             'post_id': '<id>',
             '_external': True
         },
     )
     result = field.serialize('comments', post)
     related = result['comments']['links']['related']
     assert related == url_for('posts_comments',
                               post_id=post.id,
                               _external=True)
Example #4
0
 def test_include_data_requires_type(self, app, post):
     with pytest.raises(ValueError) as excinfo:
         HyperlinkRelated(endpoint='posts_comments',
                          url_kwargs={'post_id': '<id>'},
                          include_data=True)
     assert excinfo.value.args[
         0] == 'include_data=True requires the type_ argument.'
Example #5
0
 def test_is_dump_only_by_default(self):
     field = HyperlinkRelated('author_detail',
                              url_kwargs={'author_id': '<id>'})
     assert field.dump_only is True
 def test_serialize_external(self, app, post):
     field = HyperlinkRelated(endpoint="posts_comments", url_kwargs={"post_id": "<id>", "_external": True})
     result = field.serialize("comments", post)
     related = result["comments"]["links"]["related"]
     assert related == url_for("posts_comments", post_id=post.id, _external=True)
 def test_serialize_basic(self, app, post):
     field = HyperlinkRelated(endpoint="posts_comments", url_kwargs={"post_id": "<id>"})
     result = field.serialize("comments", post)
     assert "comments" in result
     related = result["comments"]["links"]["related"]
     assert related == url_for("posts_comments", post_id=post.id)