Ejemplo n.º 1
0
    def create(self, validated_data):
        with transaction.atomic():
            structure_unit = validated_data.pop('structure_unit', None)
            parent = validated_data.pop('parent', None)
            structure = validated_data.pop('structure', None)
            notes_data = validated_data.pop('notes', [])
            identifiers_data = validated_data.pop('identifiers', [])
            information_package = validated_data.pop('information_package', None)
            appraisal_date = validated_data.pop('appraisal_date', None)
            index = validated_data.pop('index')

            tag = Tag.objects.create(
                information_package=information_package,
                appraisal_date=appraisal_date,
            )
            tag_structure = TagStructure(tag=tag)

            if structure_unit is not None:
                tag_structure.structure_unit = structure_unit
                tag_structure.structure = structure_unit.structure

                archive_structure = TagStructure.objects.filter(structure=structure_unit.structure).first().get_root()
                tag_structure.parent = archive_structure

                tag_structure.save()

            else:
                if structure is None:
                    parent_structure = parent.get_active_structure()
                else:
                    parent_structure = parent.get_structures(structure).get()

                tag_structure.parent = parent_structure
                tag_structure.structure = parent_structure.structure

            tag_structure.save()

            tag_version = TagVersion.objects.create(
                tag=tag, elastic_index=index, **validated_data,
            )
            tag.current_version = tag_version
            tag.save()

            for agent_link in AgentTagLink.objects.filter(tag=tag_version):
                AgentTagLink.objects.create(tag=tag_version, agent=agent_link.agent, type=agent_link.type)

            tag_structure.refresh_from_db()

            # Prel remove code, does not need copy to related structures??
            # if structure_unit is None:
            #     structure_unit = tag_structure.get_ancestors(
            #         include_self=True
            #     ).filter(structure_unit__isnull=False).get().structure_unit
            # related_units = structure_unit.related_structure_units.filter(
            #     structure__is_template=False
            # ).exclude(
            #     structure=tag_structure.structure
            # )
            # logger.debug('ComponentWriteSerializer - create - tag: {tag}, tag_structure: {tag_structure},
            # tag_structure.structure: {tag_structure_structure},
            # tag_structure.structure_unit: {tag_structure_structure_unit},
            # related_units: {related_units}'.format(
            #     tag=tag, tag_structure=tag_structure, tag_structure_structure=tag_structure.structure,
            # tag_structure_structure_unit=tag_structure.structure_unit, related_units=related_units
            # ))
            # for related in related_units:
            #     new_unit = related if tag_structure.structure_unit is not None else None
            #     logger.debug('ComponentWriteSerializer - create (for related) - related: {related},
            # new_unit: {new_unit}, related.structure: {related_structure}'.format(
            #         related=related, new_unit=new_unit, related_structure=related.structure
            #     ))
            #     new_tag_structure = tag_structure.copy_to_new_structure(related.structure, new_unit=new_unit)
            #     logger.debug('ComponentWriteSerializer - create (for related 2) - new_tag_structure:
            # {new_tag_structure}'.format(
            #         new_tag_structure=new_tag_structure
            #     ))

            self.create_identifiers(tag_version, identifiers_data)
            self.create_notes(tag_version, notes_data)

            tag_version.refresh_from_db()

        doc = Component.from_obj(tag_version)
        doc.save()

        return tag
Ejemplo n.º 2
0
    def create(self, validated_data):
        with transaction.atomic():
            structure_unit = validated_data.pop('structure_unit', None)
            parent = validated_data.pop('parent', None)
            structure = validated_data.pop('structure', None)
            notes_data = validated_data.pop('notes', [])
            identifiers_data = validated_data.pop('identifiers', [])
            information_package = validated_data.pop('information_package', None)
            index = validated_data.pop('index')

            tag = Tag.objects.create(information_package=information_package)
            tag_structure = TagStructure(tag=tag)

            if structure_unit is not None:
                tag_structure.structure_unit = structure_unit
                tag_structure.structure = structure_unit.structure

                archive_structure = TagStructure.objects.filter(structure=structure_unit.structure).first().get_root()
                tag_structure.parent = archive_structure

                tag_structure.save()

            else:
                if structure is None:
                    parent_structure = parent.get_active_structure()
                else:
                    parent_structure = parent.get_structures(structure).get()

                tag_structure.parent = parent_structure
                tag_structure.structure = parent_structure.structure

            tag_structure.save()

            tag_version = TagVersion.objects.create(
                tag=tag, elastic_index=index, **validated_data,
            )
            tag.current_version = tag_version
            tag.save()

            for agent_link in AgentTagLink.objects.filter(tag=tag_version):
                AgentTagLink.objects.create(tag=tag_version, agent=agent_link.agent, type=agent_link.type)

            tag_structure.refresh_from_db()
            if structure_unit is None:
                structure_unit = tag_structure.get_ancestors(
                    include_self=True
                ).filter(structure_unit__isnull=False).get().structure_unit
            related_units = structure_unit.related_structure_units.filter(
                structure__is_template=False
            ).exclude(
                structure=tag_structure.structure
            )

            for related in related_units:
                new_unit = related if tag_structure.structure_unit is not None else None
                tag_structure.copy_to_new_structure(related.structure, new_unit=new_unit)

            self.create_identifiers(self, identifiers_data)
            self.create_notes(self, notes_data)

        doc = Component.from_obj(tag_version)
        doc.save()

        return tag