Exemple #1
0
    def test_unretire(self):
        source = OrganizationSourceFactory(version=HEAD)
        concept = Concept.persist_new({
            **factory.build(dict, FACTORY_CLASS=ConceptFactory), 'mnemonic': 'c1', 'parent': source, 'retired': True,
            'names': [LocalizedTextFactory.build(locale='en', name='English', locale_preferred=True)]
        })
        Concept.persist_clone(concept.clone(), concept.created_by)
        concept_v1 = Concept.objects.order_by('-created_at').first()
        concept.refresh_from_db()

        self.assertEqual(concept.versions.count(), 2)
        self.assertTrue(concept.retired)
        self.assertFalse(concept.is_latest_version)
        self.assertTrue(concept.is_versioned_object)
        self.assertTrue(concept_v1.is_latest_version)

        concept_v1.unretire(concept.created_by, 'World needs you!')  # concept will become old/prev version
        concept.refresh_from_db()
        concept_v1.refresh_from_db()

        self.assertFalse(concept_v1.is_latest_version)
        self.assertEqual(concept.versions.count(), 3)
        self.assertFalse(concept.retired)
        latest_version = concept.get_latest_version()
        self.assertFalse(latest_version.retired)
        self.assertEqual(latest_version.comment, 'World needs you!')

        self.assertEqual(
            concept.unretire(concept.created_by),
            {'__all__': CONCEPT_IS_ALREADY_NOT_RETIRED}
        )
Exemple #2
0
    def update(self, instance, validated_data):
        instance.concept_class = validated_data.get('concept_class', instance.concept_class)
        instance.datatype = validated_data.get('datatype', instance.datatype)
        instance.extras = validated_data.get('extras', instance.extras)
        instance.external_id = validated_data.get('external_id', instance.external_id)
        instance.comment = validated_data.get('update_comment') or validated_data.get('comment')
        instance.retired = validated_data.get('retired', instance.retired)

        new_names = [
            LocalizedText(
                **{k: v for k, v in name.items() if k not in ['name_type']}
            ) for name in validated_data.get('names', [])
        ]
        new_descriptions = [
            LocalizedText(
                **{k: v for k, v in desc.items() if k not in ['description_type']}
            ) for desc in validated_data.get('descriptions', [])
        ]

        instance.cloned_names = compact(new_names)
        instance.cloned_descriptions = compact(new_descriptions)
        errors = Concept.persist_clone(instance, self.context.get('request').user)
        if errors:
            self._errors.update(errors)
        return instance
Exemple #3
0
    def test_persist_clone(self):
        es_locale = LocalizedTextFactory(locale='es', name='Not English')
        en_locale = LocalizedTextFactory(locale='en', name='English')

        source_head = OrganizationSourceFactory(version=HEAD)
        source_version0 = OrganizationSourceFactory(
            version='v0',
            mnemonic=source_head.mnemonic,
            organization=source_head.organization)

        self.assertEqual(source_head.versions.count(), 2)

        concept = ConceptFactory(descriptions=(es_locale, en_locale),
                                 names=(en_locale, ),
                                 sources=(source_version0, ),
                                 parent=source_version0)
        cloned_concept = Concept.version_for_concept(concept, 'v1',
                                                     source_version0)

        self.assertEqual(
            Concept.persist_clone(cloned_concept),
            dict(
                version_created_by=
                'Must specify which user is attempting to create a new concept version.'
            ))

        self.assertEqual(
            Concept.persist_clone(cloned_concept, concept.created_by), {})

        persisted_concept = Concept.objects.filter(
            mnemonic=cloned_concept.mnemonic,
            version=cloned_concept.version).first()
        self.assertEqual(persisted_concept.names.count(), 1)
        self.assertEqual(persisted_concept.descriptions.count(), 2)
        self.assertEqual(persisted_concept.parent, source_version0)
        self.assertEqual(persisted_concept.sources.count(), 2)
        self.assertEqual(source_head.concepts.first().id, persisted_concept.id)
        self.assertEqual(
            persisted_concept.uri,
            '/orgs/{}/sources/{}/{}/concepts/{}/{}/'.format(
                source_version0.organization.mnemonic,
                source_version0.mnemonic, source_version0.version,
                persisted_concept.mnemonic, persisted_concept.version))
        self.assertEqual(persisted_concept.version_url, persisted_concept.uri)
Exemple #4
0
 def delete(self, request, *args, **kwargs):
     key = kwargs.get('extra')
     new_version = self.get_object().clone()
     if key in new_version.extras:
         del new_version.extras[key]
         new_version.comment = 'Deleted extra %s.' % key
         errors = Concept.persist_clone(new_version, request.user)
         if errors:
             return Response(errors, status=status.HTTP_400_BAD_REQUEST)
         return Response(status=status.HTTP_204_NO_CONTENT)
     return Response(dict(detail='Not found.'),
                     status=status.HTTP_404_NOT_FOUND)
Exemple #5
0
    def update(self, request, **kwargs):
        key = kwargs.get('extra')
        value = request.data.get(key)
        if not value:
            return Response(['Must specify %s param in body.' % key],
                            status=status.HTTP_400_BAD_REQUEST)

        new_version = self.get_object().clone()
        new_version.extras[key] = value
        new_version.comment = 'Updated extras: %s=%s.' % (key, value)
        errors = Concept.persist_clone(new_version, request.user)
        if errors:
            return Response(errors, status=status.HTTP_400_BAD_REQUEST)
        return Response({key: value})
Exemple #6
0
    def delete(self, request, *args, **kwargs):
        instance = self.get_object()

        if instance:
            resource_instance = self.get_resource_object()
            new_version = resource_instance.clone()
            subject_label_attr = "cloned_{}".format(self.parent_list_attribute)
            labels = [name.clone() for name in resource_instance.names.exclude(id=instance.id)]
            setattr(new_version, subject_label_attr, labels)
            new_version.comment = 'Deleted %s in %s.' % (instance.name, self.parent_list_attribute)
            errors = Concept.persist_clone(new_version, request.user)
            if errors:
                return Response(errors, status=status.HTTP_400_BAD_REQUEST)
        return Response(status=status.HTTP_204_NO_CONTENT)
Exemple #7
0
    def update(self, request, **_):
        partial = True
        instance = self.get_object()
        serializer = self.get_serializer(instance, data=request.data, partial=partial)

        if serializer.is_valid():
            resource_instance = self.get_resource_object()
            new_version = resource_instance.clone()
            saved_instance = serializer.save()
            subject_label_attr = "cloned_{}".format(self.parent_list_attribute)
            labels = getattr(new_version, subject_label_attr, [])
            labels.append(saved_instance)
            setattr(new_version, subject_label_attr, labels)
            new_version.comment = 'Updated %s in %s.' % (saved_instance.name, self.parent_list_attribute)
            errors = Concept.persist_clone(new_version, request.user)
            if errors:
                return Response(errors, status=status.HTTP_400_BAD_REQUEST)
            return Response(serializer.data, status=status.HTTP_200_OK)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemple #8
0
 def create(self, request, **_):
     serializer = self.get_serializer(data=request.data)
     if serializer.is_valid():
         instance = self.get_object()
         new_version = instance.clone()
         subject_label_attr = "cloned_{}".format(self.parent_list_attribute)
         # get the current labels from the object
         labels = getattr(new_version, subject_label_attr, [])
         # If labels are None then we would want to initialize the labels in new_version
         saved_instance = serializer.save()
         labels.append(saved_instance)
         setattr(new_version, subject_label_attr, labels)
         new_version.comment = 'Added to %s: %s.' % (self.parent_list_attribute, saved_instance.name)
         # save updated ConceptVersion into database
         errors = Concept.persist_clone(new_version, request.user)
         if errors:
             return Response(errors, status=status.HTTP_400_BAD_REQUEST)
         headers = self.get_success_headers(serializer.data)
         return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)