Esempio n. 1
0
    def update(self, request, *args, **kwargs):
        self.object = self.get_object()
        partial = kwargs.pop('partial', True)
        if self.object is None:
            return Response(
                {'non_field_errors': 'Could not find concept to update'},
                status=status.HTTP_404_NOT_FOUND)

        self.parent_resource = self.object.parent

        if self.parent_resource != self.parent_resource.head:
            return Response(
                {
                    'non_field_errors':
                    'Parent version is not the latest. Cannot update concept.'
                },
                status=status.HTTP_400_BAD_REQUEST)
        self.object = self.object.clone()
        serializer = self.get_serializer(self.object,
                                         data=request.data,
                                         partial=partial)
        success_status_code = status.HTTP_200_OK

        if serializer.is_valid():
            self.object = serializer.save()
            if serializer.is_valid():
                serializer = ConceptDetailSerializer(
                    self.object, context={'request': request})
                return Response(serializer.data, status=success_status_code)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 2
0
class MappingDetailSerializer(MappingListSerializer):
    type = CharField(source='resource_type', read_only=True)
    uuid = CharField(source='id', read_only=True)
    extras = JSONField(required=False, allow_null=True)
    created_by = CharField(source='created_by.username', read_only=True)
    updated_by = CharField(source='created_by.username', read_only=True)
    parent_id = IntegerField(required=True, write_only=True)
    map_type = CharField(required=True)
    to_concept_url = CharField(required=False)
    from_concept_url = CharField(required=False)
    from_concept = ConceptDetailSerializer()
    to_concept = ConceptDetailSerializer()
    from_source = SourceDetailSerializer()
    to_source = SourceDetailSerializer()
    created_on = DateTimeField(source='created_at', read_only=True)
    updated_on = DateTimeField(source='updated_at', read_only=True)

    class Meta:
        model = Mapping
        fields = MappingListSerializer.Meta.fields + (
            'type',
            'uuid',
            'extras',
            'created_on',
            'updated_on',
            'created_by',
            'updated_by',
            'parent_id',
            'internal_reference_id',
        )

    def create(self, validated_data):
        mapping = Mapping.persist_new(data=validated_data,
                                      user=self.context.get('request').user)
        if mapping.errors:
            self._errors.update(mapping.errors)
        return mapping

    def update(self, instance, validated_data):
        errors = Mapping.create_new_version_for(
            instance, validated_data,
            self.context.get('request').user)
        if errors:
            self._errors.update(errors)
        return instance
Esempio n. 3
0
 def post(self, request, **kwargs):  # pylint: disable=unused-argument
     self.set_parent_resource()
     serializer = self.get_serializer(data={
         **request.data, 'parent_id': self.parent_resource.id, 'name': request.data.get('id', None)
     })
     if serializer.is_valid():
         self.object = serializer.save()
         if serializer.is_valid():
             headers = self.get_success_headers(serializer.data)
             serializer = ConceptDetailSerializer(self.object, context=dict(request=request))
             return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 4
0
 def get_detail_serializer(self, obj, data=None, files=None, partial=False):
     return ConceptDetailSerializer(obj,
                                    data,
                                    files,
                                    partial,
                                    context=dict(request=self.request))
Esempio n. 5
0
 def get_hierarchy_root(self, obj):
     if self.include_hierarchy_root:
         from core.concepts.serializers import ConceptDetailSerializer
         return ConceptDetailSerializer(obj.hierarchy_root).data
     return None
Esempio n. 6
0
 def get_detail_serializer(obj, data=None, files=None, partial=False):
     return ConceptDetailSerializer(obj, data, files, partial)