Coder Social home page Coder Social logo

Comments (4)

lukasgraf avatar lukasgraf commented on June 19, 2024

I recently did this in our own project integration of plone.restapi.

I would propose to include @type in the default summary (and maybe drop description while we're at it). @tisto any thoughts regarding this?


@pigeonflight for now, this can be done by registering your own ISerializeToJsonSummary adapter for your project's browser layer:

from my.project import IMyProjectLayer
from plone.restapi.interfaces import ISerializeToJsonSummary
from plone.restapi.serializer.summary import DefaultJSONSummarySerializer
from zope.component import adapter
from zope.interface import implementer
from zope.interface import Interface


@implementer(ISerializeToJsonSummary)
@adapter(Interface, IMyProjectLayer)
class MyJSONSummarySerializer(DefaultJSONSummarySerializer):
    """Customize summary representations to include the object's portal_type.
    """

    def __init__(self, context, request):
        self.context = context
        self.request = request

    def __call__(self):
        # Get the default summary first, then modify it as needed
        summary = super(MyJSONSummarySerializer, self).__call__()

        # Include portal_type
        summary['@type'] = self.context.portal_type

        return summary
    <adapter factory=".summary.MyJSONSummarySerializer" />

Note that this will override the DefaultJSONSummarySerializer and therefore affect summary representations for items other than the site root in all places (not just collections, but also relation lists, folder contents, parent pointers (and eventually search results)).

Also note that plone.restapi is still in pre-alpha state: This behavior might still change in the future.

from plone.restapi.

pigeonflight avatar pigeonflight commented on June 19, 2024

@lukasgraf thanks for the pointer, will look into that.

Definitely keep description. plone.restapi advertises itself as an "out of the box" restful api for Plone. In that context the description is important.

Having the type will allow me to filter the json results based on content type.

BTW.. I'm on a project now where we're using Plone as an API.

update:
@lukasgraf I just tested the workaround. Works very well. I needed to swap out the "IMyProjectLayer" of course for my own, apart from that, it works as advertised! Thanks again.

from plone.restapi.

tisto avatar tisto commented on June 19, 2024

@lukasgraf +1 for adding a type to listing (and keep the description). I wasn't 100% sure if we should use "@type" or just "type" since "@type" has a very specifig meaning in JSON-LD/Hydra. Though, I think "@type" is the correct key to use. See:

https://www.w3.org/TR/json-ld/#typed-values
https://www.hydra-cg.com/spec/latest/core/#collections
http://stackoverflow.com/a/25306010/644831 (first answer by the main author of hydra)

from plone.restapi.

lukasgraf avatar lukasgraf commented on June 19, 2024

I also believe @type is correct - the way I understand it, it should be a term in one of the document's vocabularies. Eventually, that term should be derefenceable (be an URI that can actually be fetched), but it may very well be a relative URI, as described in the StackOverflow example above.

The way I understand it, we would eventually be building our own dynamic JSON-LD contexts, vocabularies and types, based on information from FTIs, (probably annotated) schema interfaces, and field types. Related: #11

from plone.restapi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.