示例#1
0
class TrekViewSet(api_viewsets.GeotrekViewset):
    filter_backends = (DjangoFilterBackend,
                       api_filters.GeotrekQueryParamsFilter,
                       api_filters.GeotrekInBBoxFilter,
                       api_filters.GeotrekDistanceToPointFilter,
                       api_filters.GeotrekPublishedFilter,
                       api_filters.GeotrekTrekQueryParamsFilter)
    serializer_class = api_serializers.TrekListSerializer
    serializer_detail_class = api_serializers.TrekDetailSerializer
    queryset = trekking_models.Trek.objects.existing() \
        .select_related('topo_object', 'difficulty', 'practice') \
        .prefetch_related('topo_object__aggregations', 'themes', 'accessibilities', 'networks', 'attachments') \
        .annotate(geom2d_transformed=Transform(F('geom'), settings.API_SRID),
                  geom3d_transformed=Transform(F('geom_3d'), settings.API_SRID),
                  length_2d_m=Length('geom'),
                  length_3d_m=Length3D('geom_3d')) \
        .order_by('pk')  # Required for reliable pagination
    filterset_fields = ('difficulty', 'themes', 'networks', 'practice')

    @decorators.action(detail=False, methods=['get'])
    def practices(self, request, *args, **kwargs):
        return HttpResponseRedirect(reverse('apiv2:practice-list', args=args))

    @decorators.action(detail=False, methods=['get'])
    def networks(self, request, *args, **kwargs):
        return HttpResponseRedirect(reverse('apiv2:network-list', args=args))

    @decorators.action(detail=False, methods=['get'])
    def difficulties(self, request, *args, **kwargs):
        return HttpResponseRedirect(reverse('apiv2:difficulty-list',
                                            args=args))
示例#2
0
 def get_steps(self, obj):
     qs = obj.children \
         .select_related('topo_object', 'difficulty') \
         .prefetch_related('topo_object__aggregations', 'themes', 'networks', 'attachments') \
         .annotate(geom3d_transformed=Transform(F('geom_3d'), settings.API_SRID),
                   length_2d_m=Length('geom'),
                   length_3d_m=Length3D('geom_3d'))
     FinalClass = override_serializer(
         self.context.get('request').GET.get('format'), TrekSerializer)
     return FinalClass(qs, many=True, context=self.context).data
示例#3
0
class PathViewSet(api_viewsets.GeotrekViewset):
    """
    Use HTTP basic authentication to access this endpoint.
    """
    serializer_class = api_serializers.PathListSerializer
    serializer_detail_class = api_serializers.PathListSerializer
    queryset = core_models.Path.objects.all() \
        .select_related('comfort', 'source', 'stake') \
        .prefetch_related('usages', 'networks') \
        .annotate(geom2d_transformed=Transform('geom', settings.API_SRID),
                  geom3d_transformed=Transform('geom_3d', settings.API_SRID),
                  length_2d_m=Length('geom'),
                  length_3d_m=Length3D('geom_3d'))
示例#4
0
class PathViewSet(api_viewsets.GeotrekGeometricViewset):
    """
    Use HTTP basic authentication to access this endpoint.
    """
    permission_classes = [IsAuthenticated]
    serializer_class = api_serializers.PathSerializer
    queryset = core_models.Path.objects.all() \
        .select_related('comfort', 'source', 'stake') \
        .prefetch_related('usages', 'networks') \
        .annotate(geom3d_transformed=Transform(F('geom_3d'), settings.API_SRID),
                  length_2d_m=Length('geom'),
                  length_3d_m=Length3D('geom_3d')) \
        .order_by('pk')  # Required for reliable pagination
示例#5
0
class TrekViewSet(api_viewsets.GeotrekViewset):
    serializer_class = api_serializers.TrekListSerializer
    serializer_detail_class = api_serializers.TrekDetailSerializer
    queryset = trekking_models.Trek.objects.existing() \
        .select_related('topo_object', 'difficulty', 'practice') \
        .prefetch_related('topo_object__aggregations', 'themes', 'networks', 'attachments') \
        .annotate(geom2d_transformed=Transform('geom', settings.API_SRID),
                  geom3d_transformed=Transform('geom_3d', settings.API_SRID),
                  length_2d_m=Length('geom'),
                  length_3d_m=Length3D('geom_3d'))
    filter_fields = ('difficulty', 'themes', 'networks', 'practice')

    @decorators.list_route(methods=['get'])
    def all_practices(self, request, *args, **kwargs):
        """
        Get all practices list
        """
        data = api_serializers.TrekPracticeSerializer(
            trekking_models.Practice.objects.all(),
            many=True,
            context={
                'request': request
            }).data
        return response.Response(data)

    @decorators.list_route(methods=['get'])
    def used_practices(self, request, *args, **kwargs):
        """
        Get practices used by Trek instances
        """
        data = api_serializers.TrekPracticeSerializer(
            trekking_models.Practice.objects.filter(
                pk__in=trekking_models.Trek.objects.existing().values_list(
                    'practice_id', flat=True)),
            many=True,
            context={
                'request': request
            }).data
        return response.Response(data)

    @decorators.list_route(methods=['get'])
    def all_themes(self, request, *args, **kwargs):
        """
        Get all themes list
        """
        data = api_serializers.TrekThemeSerializer(
            trekking_models.Theme.objects.all(),
            many=True,
            context={
                'request': request
            }).data
        return response.Response(data)

    @decorators.list_route(methods=['get'])
    def used_themes(self, request, *args, **kwargs):
        """
        Get themes used by Trek instances
        """
        data = api_serializers.TrekThemeSerializer(
            trekking_models.Theme.objects.filter(
                pk__in=trekking_models.Trek.objects.existing().values_list(
                    'themes', flat=True)),
            many=True,
            context={
                'request': request
            }).data
        return response.Response(data)

    @decorators.list_route(methods=['get'])
    def all_networks(self, request, *args, **kwargs):
        """
        Get all networks list
        """
        data = api_serializers.TrekNetworkSerializer(
            trekking_models.TrekNetwork.objects.all(),
            many=True,
            context={
                'request': request
            }).data
        return response.Response(data)

    @decorators.list_route(methods=['get'])
    def used_networks(self, request, *args, **kwargs):
        """
        Get networks used by Trek instances
        """
        data = api_serializers.TrekNetworkSerializer(
            trekking_models.TrekNetwork.objects.filter(
                pk__in=trekking_models.Trek.objects.existing().values_list(
                    'networks', flat=True)),
            many=True,
            context={
                'request': request
            }).data
        return response.Response(data)

    @decorators.list_route(methods=['get'])
    def all_difficulties(self, request, *args, **kwargs):
        """
        Get all difficulties list
        """
        qs = trekking_models.DifficultyLevel.objects.all()
        data = api_serializers.DifficultySerializer(qs,
                                                    many=True,
                                                    context={
                                                        'request': request
                                                    }).data
        return response.Response(data)

    @decorators.list_route(methods=['get'])
    def used_difficulties(self, request, *args, **kwargs):
        """
        Get difficulties used by Trek instances
        """
        data = api_serializers.DifficultySerializer(
            trekking_models.DifficultyLevel.objects.filter(
                pk__in=trekking_models.Trek.objects.existing().values_list(
                    'difficulty_id', flat=True)),
            many=True,
            context={
                'request': request
            }).data
        return response.Response(data)