Skip to content

Passing kwargs to VectorLayer #48

@StefanBrand

Description

@StefanBrand

v1 introduced the layer_classes class variable, together with a get_layer_class_kwargs. Previously I was able to directly use the request.query_params and kwargs when filtering the queryset. Unfortunately get_layer_class_kwargs only receives self, but self does not directly have access to request and kwargs of the path. I work around this, by setting some magic variables on the class instance, but this does not feel safe. What is the recommended way to do it in v1?

class FeatureVectorLayer(VectorLayer):
    def __init__(self, fields: tuple[str, ...], **kwargs) -> "FeatureVectorLayer":
        self.queryset = get_queryset(**kwargs)
        self.tile_fields = fields


class TileServerView(MVTAPIView):
    view_name = "tileserver"
    layer_classes = [FeatureVectorLayer]

    def get_layer_class_kwargs(self):
        return {"fields": self.request.query_params.get_list("fields"), **self.path_kwargs}

    @method_decorator(cache_page(60 * 60 * 24))
    def get(self, request: Request, *args, **kwargs) -> HttpResponse:
        self.path_kwargs = kwargs  # magic instance variable

        return super().get(
            request=request,
            z=kwargs.get("z"),
            x=kwargs.get("x"),
            y=kwargs.get("y"),
        )

Edit: During review I found that self does have a request instance variable, but it was not set in one of our tests using RequestFactory.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions