Ejemplo n.º 1
0
 def get_queryset(self):
     wkt = self.request.query_params.get('polygon', None)
     queryset = GenericAPIView.get_queryset(self)
     if wkt is not None:
         polygon = GEOSGeometry(wkt)
         queryset = queryset.filter(
             train_object__the_geom__intersects=polygon)
     return queryset
Ejemplo n.º 2
0
def check_api_user_permissions(view: GenericAPIView) -> bool:
    if view.request.user.is_superuser:
        return True

    queryset: QuerySet = view.get_queryset()
    app: str = queryset.model._meta.app_label
    model: str = queryset.model._meta.model_name
    action: str = PERMISSION_ACTIONS_BY_METHOD[view.request.method]

    permission: str = f"{app}.{action}_{model}"
    return permission in view.request.user.get_all_permissions()
Ejemplo n.º 3
0
    def get_list_view_data(self, view: GenericAPIView, limit: int = None):
        view = view(request=self.request)

        queryset = view.get_queryset()

        if limit:
            queryset = queryset[:limit]

        serializer = view.serializer_class(queryset,
                                           many=True,
                                           context={'request': self.request})

        return serializer.data
Ejemplo n.º 4
0
    def get_filterset_class(view: GenericAPIView,
                            filter_map: dict) -> Type[FilterMapFilterSet]:
        """
        build own filterset class for the filter
        """
        model_class = view.get_queryset().model
        filter_map = filter_map or dict()

        # plain filter map, no tuples
        plain_filter_map = {
            key: "__".join(val) if isinstance(val, tuple) else val
            for key, val in filter_map.items()
        }

        class Filterset(FilterMapFilterSet):
            class Meta:
                model = model_class
                # clean filed map for proper fields with expressions
                fields = FilterMapBackend.clean_field_names(
                    filter_map.values())
                fil_map = plain_filter_map

            def get_form_class(self):
                val_to_name = inverse_mapping(plain_filter_map)
                fields = OrderedDict()

                for name, filter_ in self.filters.items():
                    f = filter_.field

                    # set label for field using filter_map
                    # eg. if map is {"name": "profile__name"} then set label to "Name", by default it is "Profile Name"
                    f.label = pretty_name(val_to_name[name])

                    fields.update({val_to_name[name]: f})

                return type(str('%sForm' % self.__class__.__name__),
                            (self._meta.form, ), fields)

        return Filterset