def test_toplevel_collection_with_repeats():
    example_resource = ExampleSerializer(10)
    resource = JSONAPICollectionSerializer([
        example_resource,
        LinkingResource(2,
            JSONAPICollectionSerializer([
                example_resource,
                LinkingResource(3, example_resource)
            ])
        )
    ])
    expected_json = {
        "data": [
            {
                "type": "example",
                "id": "10",
                "attributes": {
                    "title": "an example"
                }
            },
            {
                "type": "linking_example",
                "id": "2",
                "attributes": {
                    "title": "an example of linking"
                },
                "relationships": {
                    "something": {"data": [
                        {"type": "example", "id": "10"},
                        {"type": "linking_example", "id": "3"}
                    ]}
                },
            },
        ],
        "included": [
            {
                "type": "linking_example",
                "id": "3",
                "attributes": {
                    "title": "an example of linking"
                },
                "relationships": {
                    "something": {
                        "data": {"type": "example", "id": "10"},
                        "links": {
                            "related": "http://www.example.com/examples/10",
                        },
                    },
                },
            },
        ]
    }
    assert_equal(expected_json, resource.as_json_api_document())
def test_linked_collection():
    resource = LinkingResource(1, JSONAPICollectionSerializer([ExampleSerializer(2), ExampleSerializer(3)]))
    expected_json = {
        "data": {
            "type": "linking_example",
            "id": "1",
            "attributes": {
                "title": "an example of linking"
            },
            "relationships": {
                "something": {
                    "data": [{"type": "example", "id": "2"}, {"type": "example", "id": "3"}]
                }
            }
        },
        "included": [
            {
                "type": "example",
                "id": "2",
                "attributes": {
                    "title": "an example"
                }
            },
            {
                "type": "example",
                "id": "3",
                "attributes": {
                    "title": "an example"
                }
            },
        ]
    }
    assert_equal(expected_json, resource.as_json_api_document())
Пример #3
0
def list_follows(request_):
    return jsonify(JSONAPICollectionSerializer([
        FollowSerializer(
            follow,
            inbound_request=request_
        )
        for follow in follows_dbm.all()
    ]).as_json_api_document())
Пример #4
0
def list_posts(request_):
    return jsonify(JSONAPICollectionSerializer([
        PostSerializer(
            post,
            inbound_request=request_
        )
        for post in posts_dbm.all()
    ]).as_json_api_document())
Пример #5
0
def read_news_feed(user_id):
    user = get_user_or_404(user_id)
    posts = posts_dbm.find_posts_for_follower(user_id)
    return jsonify(
        JSONAPICollectionSerializer([
            PostSerializer(post,
                           inbound_request=request,
                           inbound_session=session) for post in posts
        ]).as_json_api_document())
def test_linked_heterogeneous_collection():
    resource = LinkingResource(1, JSONAPICollectionSerializer([ExampleSerializer(2), LinkingResource(2, ExampleSerializer(3))]))
    expected_json = {
        "data": {
            "type": "linking_example",
            "id": "1",
            "attributes": {
                "title": "an example of linking"
            },
            "relationships": {
                "something": {
                    "data": [{
                        "type": "example",
                        "id": "2"
                    }, {
                        "type": "linking_example",
                        "id": "2"
                    }]
                }
            }
        },
        "included": [
            {
                "type": "example",
                "id": "2",
                "attributes": {
                    "title": "an example"
                }
            },
            {
                "type": "linking_example",
                "id": "2",
                "attributes": {
                    "title": "an example of linking"
                },
                "relationships": {
                    "something": {
                        "data": {"type": "example", "id": "3"},
                        "links": {
                            "related": "http://www.example.com/examples/3",
                        },
                    },
                },
            },
            {
                "type": "example",
                "id": "3",
                "attributes": {
                    "title": "an example"
                },
            },
        ]
    }
    assert_equal.__self__.maxDiff = None
    assert_equal(expected_json, resource.as_json_api_document())
def test_toplevel_collection():
    resource = JSONAPICollectionSerializer([ExampleSerializer(2), LinkingResource(2, ExampleSerializer(3))])
    expected_json = {
        "data": [
            {
                "type": "example",
                "id": "2",
                "attributes": {
                    "title": "an example"
                }
            },
            {
                "type": "linking_example",
                "id": "2",
                "attributes": {
                    "title": "an example of linking"
                },
                "relationships": {
                    "something": {
                        "data": {"type": "example", "id": "3"},
                        "links": {
                            "related": "http://www.example.com/examples/3",
                        },
                    },
                },
            },
        ],
        "included": [
            {
                "type": "example",
                "id": "3",
                "attributes": {
                    "title": "an example"
                }
            },
        ]
    }
    assert_equal.__self__.maxDiff = None
    assert_equal(expected_json, resource.as_json_api_document())
Пример #8
0
    def related_serializer(self, parent_serializer, relationship_key):
        from cartographer.serializers import JSONAPICollectionSerializer

        if self.serializer_method is not None:
            return getattr(parent_serializer, self.serializer_method)()

        models = []
        if self.id_attribute is not None:
            raise Exception("id_attribute provided for an ArrayRelationship.")
        elif self.model_property is not None:
            models = getattr(parent_serializer.model, self.model_property)
        elif self.model_method is not None:
            models = getattr(parent_serializer.model, self.model_method)()

        serializer_class = self.resource_registry_entry().get(
            ResourceRegistryKeys.SERIALIZER)
        # TODO: custom collection class, custom arguments to serializer_class
        return JSONAPICollectionSerializer([
            serializer_class(model,
                             parent_serializer=parent_serializer,
                             relationship_name=relationship_key)
            for model in models
        ])
Пример #9
0
def list_users(request_):
    return jsonify(
        JSONAPICollectionSerializer([
            UserSerializer(user, inbound_request=request_)
            for user in users_dbm.all()
        ]).as_json_api_document())