Exemple #1
0
    def __call__(self, include_items=True):
        content_url = self.context.__parent__.__parent__.absolute_url()
        comments_url = f"{content_url}/@comments"
        url = f"{comments_url}/{self.context.id}"

        if self.context.in_reply_to:
            parent_url = f"{comments_url}/{self.context.in_reply_to}"
            in_reply_to = str(self.context.in_reply_to)
        else:
            parent_url = None
            in_reply_to = None

        doc_allowed = delete_own_comment_allowed()
        delete_own = doc_allowed and can_delete_own(self.context)

        if self.context.mime_type == "text/plain":
            text_data = self.context.text
            text_mime_type = self.context.mime_type
        else:
            text_data = self.context.getText()
            text_mime_type = "text/html"
        return {
            "@id": url,
            "@type": self.context.portal_type,
            "@parent": parent_url,
            "comment_id": str(self.context.id),
            "in_reply_to": in_reply_to,
            "text": {
                "data": text_data,
                "mime-type": text_mime_type
            },
            "user_notification": self.context.user_notification,
            "author_username": self.context.author_username,
            "author_name": self.context.author_name,
            "author_image":
            self.get_author_image(self.context.author_username),
            "creation_date": IJsonCompatible(self.context.creation_date),
            "modification_date":
            IJsonCompatible(self.context.modification_date),  # noqa
            "is_editable": edit_comment_allowed() and can_edit(self.context),
            "is_deletable": can_delete(self.context) or delete_own,
        }
Exemple #2
0
def json_compatible(value):
    """The json_compatible function converts any value to JSON compatible
    data when possible, raising a TypeError for unsupported values.
    This is done by using the IJsonCompatible converters.

    Be aware that adapting the value `None` will result in a component
    lookup error unless `None` is passed in as default value.
    Because of that the `json_compatible` helper method should always be
    used for converting values that may be None.
    """
    return IJsonCompatible(value, None)
Exemple #3
0
    def reply(self):
        if self.controlpanel_name:
            return self.reply_panel()

        def serialize(panels):
            for panel in panels:
                serializer = ISerializeToJsonSummary(panel)
                yield serializer()

        panels = self.available_controlpanels()
        return IJsonCompatible(list(serialize(panels)))
Exemple #4
0
    def reply(self):
        if self.portletmanager_id:
            return self.reply_portletmanager()

        def serialize(portletmanagers):
            for name, manager in portletmanagers:
                serializer = queryMultiAdapter(
                   (manager, self.context, self.request),
                   ISerializeToJsonSummary)
                yield serializer()

        portletmanagers = get_portletmanagers()
        return IJsonCompatible(list(serialize(portletmanagers)))
Exemple #5
0
    def reply(self):
        data = json_body(self.request)

        # Disable CSRF protection
        if "IDisableCSRFProtection" in dir(plone.protect.interfaces):
            alsoProvides(self.request,
                         plone.protect.interfaces.IDisableCSRFProtection)

        portletmanagers = dict(get_portletmanagers())

        portlet_type = data.get('@type', None)
        portlet_manager = data.get('manager', None)

        if not (portlet_manager or portlet_type):
            raise BadRequest(
                "'@type', and 'manager' are required properties for a portlet to be added"
            )

        if portlet_manager not in portletmanagers:
            self.request.response.setStatus(501)
            return dict(error=dict(
                message="Invalid manager {}".format(portlet_manager)))

        pm = portletmanagers.get(portlet_manager)

        # Create portlet
        deserializer = queryMultiAdapter((self.context, pm, self.request),
                                         IDeserializeFromJson)
        if deserializer is None:
            self.request.response.setStatus(501)
            return dict(error=dict(
                message="Cannot deserialize portlet for manager {}".format(
                    portlet_manager)))

        try:
            deserializer(data)
        except DeserializationError as e:
            self.request.response.setStatus(400)
            return dict(
                error=dict(type="DeserializationError", message=str(e)))

        results = list()
        for name, manager in portletmanagers.items():
            serializer = queryMultiAdapter(
                (manager, self.context, self.request), ISerializeToJson)
            results.append(serializer())

        return IJsonCompatible(results)
Exemple #6
0
 def reply_panel(self):
     panel = self.panel_by_name(self.controlpanel_name)
     if panel is None:
         self.request.response.setStatus(404)
         return
     return IJsonCompatible(ISerializeToJson(panel)())