def get_context_data(self, **kwargs): context = super(PathJsonList, self).get_context_data(**kwargs) context["sumPath"] = round( self.object_list.aggregate(sumPath=Coalesce( Sum(Length('geom'), output_field=FloatField()), 0))['sumPath'] / 1000, 1) return context
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))
def get_children(self, obj): children = obj.children.all().annotate( length_2d_m=Length('geom'), start_point=Transform(StartPoint('geom'), settings.API_SRID), end_point=Transform(EndPoint('geom'), settings.API_SRID)) serializer_children = TrekListSerializer( children, many=True, context={'root_pk': obj.pk}) return serializer_children.data
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
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'))
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
def get_queryset(self, *args, **kwargs): queryset = trekking_models.Trek.objects.existing()\ .select_related('topo_object') \ .prefetch_related('topo_object__aggregations', 'attachments') \ .order_by('pk').annotate(length_2d_m=Length('geom')) if not self.action == 'list': queryset = queryset.annotate( geom2d_transformed=Transform(F('geom'), settings.API_SRID)) if 'portal' in self.request.GET: queryset = queryset.filter( Q(portal__name__in=self.request.GET['portal'].split(',')) | Q(portal=None)) return queryset.annotate(start_point=Transform(StartPoint('geom'), settings.API_SRID), end_point=Transform(EndPoint('geom'), settings.API_SRID)).\ filter(Q(published=True) | Q(trek_parents__parent__published=True, trek_parents__parent__deleted=False)).distinct()
def get_queryset(self, *args, **kwargs): lang = self.request.LANGUAGE_CODE queryset = trekking_models.Trek.objects.existing()\ .select_related('topo_object') \ .prefetch_related('topo_object__aggregations', 'attachments') \ .order_by('pk').annotate(length_2d_m=Length('geom')) if not self.action == 'list': queryset = queryset.annotate( geom2d_transformed=Transform(F('geom'), settings.API_SRID)) if self.action == 'list': queryset = queryset.annotate(count_parents=Count('trek_parents')).\ exclude(Q(count_parents__gt=0) & Q(published=False)) if 'portal' in self.request.GET: queryset = queryset.filter( Q(portal__name=self.request.GET['portal']) | Q(portal=None)) return queryset.annotate(start_point=Transform(StartPoint('geom'), settings.API_SRID), end_point=Transform(EndPoint('geom'), settings.API_SRID)). \ filter(Q(**{'published_{lang}'.format(lang=lang): True}) | Q(**{'trek_parents__parent__published_{lang}'.format(lang=lang): True, 'trek_parents__parent__deleted': False})).distinct()
def get_queryset(self): qs = self.model.objects.existing() qs = qs.select_related('structure', 'difficulty', 'practice', 'route') qs = qs.prefetch_related( 'networks', 'source', 'portal', 'web_links', 'accessibilities', 'themes', 'aggregations', 'information_desks', 'attachments', Prefetch('trek_relationship_a', queryset=TrekRelationship.objects.select_related('trek_a', 'trek_b')), Prefetch('trek_relationship_b', queryset=TrekRelationship.objects.select_related('trek_a', 'trek_b')), Prefetch('trek_children', queryset=OrderedTrekChild.objects.select_related('parent', 'child')), Prefetch('trek_parents', queryset=OrderedTrekChild.objects.select_related('parent', 'child')), ) qs = qs.filter(Q(published=True) | Q(trek_parents__parent__published=True)).distinct('practice__order', 'pk').\ order_by('-practice__order', 'pk') if 'source' in self.request.GET: qs = qs.filter(source__name__in=self.request.GET['source'].split(',')) if 'portal' in self.request.GET: qs = qs.filter(Q(portal__name=self.request.GET['portal']) | Q(portal=None)) qs = qs.annotate(api_geom=Transform("geom", settings.API_SRID)) qs = qs.annotate(length_2d_m=Length('geom')) return qs
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)
def get_parents(self, obj): parents = obj.parents.all().annotate(length_2d_m=Length('geom'), start_point=Transform(StartPoint('geom'), settings.API_SRID), end_point=Transform(EndPoint('geom'), settings.API_SRID)) serializer_parents = TrekListSerializer(parents, many=True) return serializer_parents.data