示例#1
0
    def reply(self):
        if not self.params:
            raise BadRequest("Missing parameter typename")
        if len(self.params) < 2:
            raise BadRequest("Missing parameter fieldname")
        if len(self.params) > 2:
            raise BadRequest("Too many parameters")

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

        # Make sure we don't get the right dexterity-types adapter
        if IPloneRestapiLayer.providedBy(self.request):
            noLongerProvides(self.request, IPloneRestapiLayer)

        context = queryMultiAdapter(
            (self.context, self.request), name="dexterity-types"
        )

        # Get content type SchemaContext
        name = self.params.pop(0)
        context = context.publishTraverse(self.request, name)

        name = self.params.pop(0)
        try:
            context.publishTraverse(self.request, name)
        except AttributeError:
            delete_fieldset(context, self.request, name)
        else:
            delete_field(context, self.request, name)

        return self.reply_no_content()
示例#2
0
文件: add.py 项目: eea/plone.restapi
    def reply(self):
        if not self.params:
            raise BadRequest("Missing parameter typename")

        data = json_body(self.request)

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

        # Make sure we get the right dexterity-types adapter
        if IPloneRestapiLayer.providedBy(self.request):
            noLongerProvides(self.request, IPloneRestapiLayer)

        name = self.params.pop()
        context = queryMultiAdapter((self.context, self.request),
                                    name="dexterity-types")
        context = context.publishTraverse(self.request, name)

        factory = data.get("factory", None)
        if not factory:
            raise BadRequest("Missing parameter: 'factory'")

        if factory == "fieldset":
            res = add_fieldset(context, self.request, data)
        else:
            res = add_field(context, self.request, data)

        self.request.response.setStatus(201)
        return res
示例#3
0
文件: get.py 项目: eea/plone.restapi
    def reply_for_type(self):
        check_security(self.context)
        portal_type = self.params.pop()

        # Make sure we get the right dexterity-types adapter
        if IPloneRestapiLayer.providedBy(self.request):
            noLongerProvides(self.request, IPloneRestapiLayer)

        try:
            dtool = queryMultiAdapter(
                (self.context, self.request), name="dexterity-types"
            )
            dtype = dtool.publishTraverse(self.request, portal_type)
        except Exception:
            dtype = self.context

        try:
            schema = get_info_for_type(dtype, self.request, portal_type)
        except KeyError:
            self.content_type = "application/json"
            self.request.response.setStatus(404)
            return {
                "type": "NotFound",
                "message": "Type '%s' could not be found." % portal_type,
            }

        self.content_type = "application/json+schema"
        return schema
示例#4
0
    def add(self, names):
        data = json_body(self.request)

        title = data.get("title", None)
        if not title:
            raise BadRequest("Property 'title' is required")

        tid = data.get("id", None)
        if not tid:
            tid = idnormalizer.normalize(title).replace("-", "_")

        description = data.get("description", "")

        properties = {"id": tid, "title": title, "description": description}

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

        if IPloneRestapiLayer.providedBy(self.request):
            noLongerProvides(self.request, IPloneRestapiLayer)

        context = queryMultiAdapter((self.context, self.request),
                                    name='dexterity-types')
        add_type = queryMultiAdapter((context, self.request), name='add-type')
        fti = add_type.form_instance.create(data=properties)
        add_type.form_instance.add(fti)
        return self.get([
            tid,
        ])
示例#5
0
    def reply(self):
        if not self.params:
            raise BadRequest("Missing parameter typename")

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

        # Make sure we get the right dexterity-types adapter
        if IPloneRestapiLayer.providedBy(self.request):
            noLongerProvides(self.request, IPloneRestapiLayer)

        data = json_body(self.request)
        name = self.params.pop()
        dtypes = queryMultiAdapter((self.context, self.request),
                                   name="dexterity-types")
        ctype = dtypes.publishTraverse(self.request, name)

        self.update_layouts(name, data)
        self.remove_fields(ctype, data)
        self.remove_fieldsets(ctype, data)
        self.add_fields(ctype, data)
        self.add_fieldsets(ctype, data)
        self.update_fields(ctype, data)
        self.update_fieldsets(ctype, data)

        serializeSchema(ctype.schema)
        return self.reply_no_content()
示例#6
0
    def delete(self, names):
        name = names[0]

        if IPloneRestapiLayer.providedBy(self.request):
            noLongerProvides(self.request, IPloneRestapiLayer)

        context = queryMultiAdapter((self.context, self.request),
                                    name='dexterity-types')
        edit = queryMultiAdapter((context, self.request), name='edit')
        edit.form_instance.remove((name, None))
示例#7
0
    def update(self, names):
        name = names[0]

        if IPloneRestapiLayer.providedBy(self.request):
            noLongerProvides(self.request, IPloneRestapiLayer)

        context = queryMultiAdapter((self.context, self.request),
                                    name='dexterity-types')
        context = context.publishTraverse(self.request, name)
        deserializer = IDeserializeFromJson(self)
        return deserializer(context)
示例#8
0
    def get(self, names):
        name = names[0]

        if IPloneRestapiLayer.providedBy(self.request):
            noLongerProvides(self.request, IPloneRestapiLayer)

        context = queryMultiAdapter((self.context, self.request),
                                    name="dexterity-types")
        context = context.publishTraverse(self.request, name)
        serializer = ISerializeToJson(self)
        return serializer(context)
示例#9
0
    def reply(self):
        if not self.params:
            raise BadRequest("Missing parameter typename")

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

        # Make sure we get the right dexterity-types adapter
        if IPloneRestapiLayer.providedBy(self.request):
            noLongerProvides(self.request, IPloneRestapiLayer)

        data = json_body(self.request)
        if len(self.params) == 1:
            name = self.params.pop(0)
        elif len(self.params) == 2:
            name = self.params.pop(0)
            fname = self.params.pop(0)
            if "fields" in data:
                return self.reply_for_fieldset(name, fname, data)
            return self.reply_for_field(name, fname, data)
        else:
            raise BadRequest("Too many parameters")

        context = queryMultiAdapter(
            (self.context, self.request), name="dexterity-types"
        )

        # Get content type SchemaContext
        context = context.publishTraverse(self.request, name)

        # Update Field properties
        properties = data.get("properties", {})
        for key, value in properties.items():
            self.reply_for_field(name, key, value, create=True)

        # Update Fieldset properties
        fieldsets = data.get("fieldsets", [])
        for fieldset in fieldsets:
            fname = fieldset.get("id")
            self.reply_for_fieldset(name, fname, fieldset, create=True)

        return self.reply_no_content()
示例#10
0
    def reply_for_field(self):
        check_security(self.context)
        name = self.params[0]
        field_name = self.params[1]

        # Make sure we get the right dexterity-types adapter
        if IPloneRestapiLayer.providedBy(self.request):
            noLongerProvides(self.request, IPloneRestapiLayer)

        context = queryMultiAdapter((self.context, self.request),
                                    name="dexterity-types")
        context = context.publishTraverse(self.request, name)

        try:
            # Get field
            return get_info_for_field(context, self.request, field_name)
        except (KeyError, AttributeError):
            # Get fieldset
            return self.reply_for_fieldset()
示例#11
0
    def reply_for_fieldset(self):
        name = self.params[0]
        field_name = self.params[1]

        # Make sure we get the right dexterity-types adapter
        if IPloneRestapiLayer.providedBy(self.request):
            noLongerProvides(self.request, IPloneRestapiLayer)

        context = queryMultiAdapter((self.context, self.request),
                                    name="dexterity-types")
        context = context.publishTraverse(self.request, name)

        try:
            return get_info_for_fieldset(context, self.request, field_name)
        except KeyError:
            self.content_type = "application/json"
            self.request.response.setStatus(404)
            return {
                "type": "NotFound",
                "message": "Field(set) '%s' could not be found." % field_name,
            }