Пример #1
0
 def _get_required_arg(self, argname, default=None):
     try:
         return self.KVP[argname]
     except KeyError:
         if default is not None:
             return default
         raise MissingParameterValue(argname.lower()) from None
Пример #2
0
    def get_context_data(self, typeNames, **params):
        if self.view.KVP.get("TYPENAMES") == "" or self.view.KVP.get("TYPENAME") == "":
            # Using TYPENAMES= does result in an error.
            raise MissingParameterValue("typeNames", "Empty TYPENAMES parameter")
        elif typeNames is None:
            # Not given, all types are returned
            typeNames = self.all_feature_types

        return {"feature_types": typeNames}
Пример #3
0
    def from_kvp_request(cls, **params):
        """Build this object from a HTTP GET (key-value-pair) request."""
        # Validate optionally required parameters
        if not params["typeNames"] and not params["resourceID"]:
            raise MissingParameterValue("typeNames",
                                        "Empty TYPENAMES parameter")

        # Validate mutually exclusive parameters
        if params["filter"] and (params["bbox"] or params["resourceID"]):
            raise InvalidParameterValue(
                "filter",
                "The FILTER parameter is mutually exclusive with BBOX and RESOURCEID",
            )

        # Validate mutually exclusive parameters
        if params["resourceID"]:
            if params["bbox"] or params["filter"]:
                raise InvalidParameterValue(
                    "resourceID",
                    "The RESOURCEID parameter is mutually exclusive with BBOX and FILTER",
                )

            # When ResourceId + typenames is defined, it should be a value from typenames
            # see WFS spec 7.9.2.4.1
            if params["typeNames"]:
                id_type_names = params["resourceID"].type_names
                if id_type_names:
                    # Only test when the RESOURCEID has a typename.id format
                    # Otherwise, this breaks the CITE RESOURCEID=test-UUID parameter.
                    kvp_type_names = {
                        feature_type.name
                        for feature_type in params["typeNames"]
                    }
                    if not kvp_type_names.issuperset(id_type_names):
                        raise InvalidParameterValue(
                            "resourceID",
                            "When TYPENAMES and RESOURCEID are combined, "
                            "the RESOURCEID type should be included in TYPENAMES.",
                        )

        return AdhocQuery(
            typeNames=params["typeNames"],
            filter=params["filter"],
            filter_language=params["filter_language"],
            bbox=params["bbox"],
            sortBy=params["sortBy"],
            resourceId=params["resourceID"],
            value_reference=params.get("valueReference"),
        )
Пример #4
0
    def extract_parameters(cls, KVP) -> dict[str, str]:
        """Extract the arguments from the key-value-pair (=HTTP GET) request."""
        args = {}
        for name, _xsd_type in cls.meta.parameters.items():
            try:
                args[name] = KVP[name]
            except KeyError:
                raise MissingParameterValue(
                    name,
                    f"Stored query {cls.meta.id} requires an '{name}' parameter"
                ) from None

        # Avoid unexpected behavior, check whether the client also sends adhoc query parameters
        for name in ("filter", "bbox", "resourceID"):
            if name not in args and KVP.get(name.upper()):
                raise InvalidParameterValue(
                    name,
                    "Stored query can't be combined with adhoc-query parameters"
                )

        return args
Пример #5
0
    def value_from_query(self, KVP: dict):  # noqa: C901
        """Parse a request variable using the type definition.

        This uses the dataclass settings to parse the incoming request value.
        """
        # URL-based key-value-pair parameters use uppercase.
        kvp_name = self.name.upper()
        value = KVP.get(kvp_name)
        if not value and self.alias:
            value = KVP.get(self.alias.upper())

        # Check required field settings, both empty and missing value are treated the same.
        if not value:
            if not self.required:
                return self.default
            elif value is None:
                raise MissingParameterValue(self.name,
                                            f"Missing {kvp_name} parameter")
            else:
                raise InvalidParameterValue(self.name,
                                            f"Empty {kvp_name} parameter")

        # Allow conversion into a python object
        if self.parser is not None:
            try:
                value = self.parser(value)
            except ExternalParsingError as e:
                raise OperationParsingFailed(
                    self.name,
                    f"Unable to parse {kvp_name} argument: {e}") from None
            except (TypeError, ValueError, NotImplementedError) as e:
                # TypeError/ValueError are raised by most handlers for unexpected data
                # The NotImplementedError can be raised by fes parsing.
                raise InvalidParameterValue(
                    self.name, f"Invalid {kvp_name} argument: {e}") from None

        # Validate against value choices
        self.validate_value(value)

        return value