Exemple #1
0
    def update(self, request, *args, **kwargs):
        self.permission_classes = (CanEditParentDictionary,)
        if hasattr(self.parent_resource, 'versioned_object'):
            parent_versioned_object = self.parent_resource.versioned_object
            if self.parent_resource != type(self.parent_resource).get_latest_version_of(parent_versioned_object):
                return Response({'non_field_errors': 'Parent version is not the latest.  Cannot update concept.'}, status=status.HTTP_400_BAD_REQUEST)

        self.serializer_class = ConceptVersionUpdateSerializer
        partial = kwargs.pop('partial', True)
        self.object = self.get_object_or_none()

        if self.object is None:
            return Response({'non_field_errors': 'Could not find concept to update'}, status=status.HTTP_404_NOT_FOUND)
        else:
            latest_version = ConceptVersion.get_latest_version_of(self.object)
            self.object = latest_version.clone()
            save_kwargs = {'force_update': False}
            success_status_code = status.HTTP_200_OK

        serializer = self.get_serializer(self.object, data=request.DATA,
                                         files=request.FILES, partial=partial)

        if serializer.is_valid():
            self.object = serializer.save(**save_kwargs)
            if serializer.is_valid():
                self.post_save(self.object, created=True)
                serializer = ConceptVersionDetailSerializer(self.object, context={'request': request})
                return Response(serializer.data, status=success_status_code)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemple #2
0
    def retrieve(self, request, *args, **kwargs):
        super(SourceRetrieveUpdateDestroyView,
              self).retrieve(request, *args, **kwargs)
        self.object = self.get_object()
        serializer = self.get_serializer(self.object)
        data = serializer.data

        source_version = None
        offset = request.QUERY_PARAMS.get(OFFSET_PARAM, DEFAULT_OFFSET)
        try:
            offset = int(offset)
        except ValueError:
            offset = DEFAULT_OFFSET
        limit = settings.REST_FRAMEWORK.get('MAX_PAGINATE_BY',
                                            self.paginate_by)
        include_retired = False
        include_concepts = request.QUERY_PARAMS.get(INCLUDE_CONCEPTS_PARAM,
                                                    False)
        include_mappings = request.QUERY_PARAMS.get(INCLUDE_MAPPINGS_PARAM,
                                                    False)
        updated_since = None
        if include_concepts or include_mappings:
            source_version = SourceVersion.get_latest_version_of(self.object)
            paginate_by = self.get_paginate_by(EmptyQuerySet())
            if paginate_by:
                limit = min(limit, paginate_by)
            include_retired = request.QUERY_PARAMS.get(INCLUDE_RETIRED_PARAM,
                                                       False)
            updated_since = parse_updated_since_param(request)

        if include_concepts:
            source_version_concepts = source_version.concepts
            queryset = ConceptVersion.objects.filter(is_active=True)
            if not include_retired:
                queryset = queryset.filter(~Q(retired=True))
            if updated_since:
                queryset = queryset.filter(updated_at__gte=updated_since)
            queryset = queryset.filter(id__in=source_version_concepts)
            queryset = queryset[offset:offset + limit]
            serializer = ConceptVersionDetailSerializer(queryset, many=True)
            data['concepts'] = serializer.data

        if include_mappings:
            all_children = source_version.mappings
            queryset = MappingVersion.objects.filter(is_active=True)
            if not include_retired:
                queryset = queryset.filter(~Q(retired=True))
            queryset = queryset.filter(id__in=all_children)
            if updated_since:
                queryset = queryset.filter(updated_at__gte=updated_since)
            queryset = queryset[offset:offset + limit]
            serializer = MappingVersionDetailSerializer(queryset, many=True)
            data['mappings'] = serializer.data

        return Response(data)
Exemple #3
0
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.DATA, files=request.FILES)
        if serializer.is_valid():
            self.pre_save(serializer.object)
            save_kwargs = {
                'force_insert': True,
                'parent_resource': self.parent_resource
            }
            self.object = serializer.save(**save_kwargs)
            if serializer.is_valid():
                self.post_save(self.object, created=True)
                headers = self.get_success_headers(serializer.data)
                latest_version = ConceptVersion.get_latest_version_of(self.object)
                serializer = ConceptVersionDetailSerializer(latest_version)
                return Response(serializer.data, status=status.HTTP_201_CREATED,
                                headers=headers)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemple #4
0
    def includeConceptsAndMappings(self, request, data, container_version):
        offset = request.QUERY_PARAMS.get(OFFSET_PARAM, DEFAULT_OFFSET)
        try:
            offset = int(offset)
        except ValueError:
            offset = DEFAULT_OFFSET
        limit = settings.REST_FRAMEWORK.get('MAX_PAGINATE_BY', self.paginate_by)
        include_retired = False
        include_concepts = request.QUERY_PARAMS.get(INCLUDE_CONCEPTS_PARAM, False)
        include_mappings = request.QUERY_PARAMS.get(INCLUDE_MAPPINGS_PARAM, False)
        updated_since = None
        if include_concepts or include_mappings:
            paginate_by = self.get_paginate_by(EmptyQuerySet())
            if paginate_by:
                limit = min(limit, paginate_by)
            include_retired = request.QUERY_PARAMS.get(INCLUDE_RETIRED_PARAM, False)
            updated_since = parse_updated_since_param(request)

        if include_concepts:
            queryset = container_version.get_concepts()
            queryset = queryset.filter(is_active=True)
            if not include_retired:
                queryset = queryset.filter(~Q(retired=True))
            if updated_since:
                queryset = queryset.filter(updated_at__gte=updated_since)
            queryset = queryset[offset:offset+limit]
            serializer = ConceptVersionDetailSerializer(queryset, many=True)
            data['concepts'] = serializer.data

        if include_mappings:
            queryset = container_version.get_mappings()
            queryset = queryset.filter(is_active=True)
            if not include_retired:
                queryset = queryset.filter(~Q(retired=True))
            if updated_since:
                queryset = queryset.filter(updated_at__gte=updated_since)
            queryset = queryset[offset:offset+limit]
            serializer = MappingVersionDetailSerializer(queryset, many=True)
            data['mappings'] = serializer.data