Exemplo n.º 1
0
    async def __call__(self):
        """ data input : { 'interface': 'INTERFACE' }"""
        registry = await get_registry()
        if registry is None:
            return ErrorResponse("BadRequest",
                                 _("Not in a container request"),
                                 status=412)

        data = await self.request.json()
        interface = data.get("interface", None)
        initial_values = data.get("initial_values", {})
        if interface is None:
            return ErrorResponse("InvalidRequest",
                                 "Non existent Interface",
                                 status=412)

        iObject = import_class(interface)
        registry.register_interface(iObject)
        config = registry.for_interface(iObject)

        # Initialize values
        # If its defined on the guillotina.schema default will not be overwritten
        #  you will need to PATCH
        for key, field in get_fields(iObject).items():
            if key in initial_values and getattr(config, key,
                                                 _marker) == _marker:
                # We don't have a value
                config[key] = initial_values[key]

        await notify(
            RegistryEditedEvent(self.context, registry,
                                {interface: initial_values}))

        return Response(status=201)
Exemplo n.º 2
0
    async def __call__(self):
        if self.key is _marker:
            # No option to write the root of registry
            return ErrorResponse("InvalidRequest",
                                 "Needs the registry key",
                                 status=412)

        data = await self.request.json()
        if "value" in data:
            value = data["value"]
        else:
            value = data

        assert "." in self.key, "Registry key must be dotted.iface.name.fieldname"  # noqa
        iface_name, name = self.key.rsplit(".", 1)
        iface = resolve_dotted_name(iface_name)

        assert iface is not None, "Must provide valid registry interface"  # noqa
        try:
            field = iface[name]
        except KeyError:
            return ErrorResponse("DeserializationError",
                                 "Invalid field name {}".format(str(name)),
                                 status=412)

        try:
            new_value = get_adapter((field),
                                    IJSONToValue,
                                    args=[value, self.context])
        except ComponentLookupError:
            return ErrorResponse("DeserializationError",
                                 "Cannot deserialize type {}".format(
                                     str(self.field)),
                                 status=412)

        try:
            registry = await get_registry()
            registry[self.key] = new_value
        except (DeserializationError, ValueDeserializationError) as e:
            return ErrorResponse("DeserializationError",
                                 str(e),
                                 exc=e,
                                 status=412)

        await notify(
            RegistryEditedEvent(self.context, registry,
                                {iface_name: {
                                    name: value
                                }}))

        return Response(status=204)