Esempio n. 1
0
    def add_content(self, content):
        """
        Add a content unit to this version.

        Args:
           content (django.db.models.QuerySet): Set of Content to add

        Raise:
            pulpcore.exception.ResourceImmutableError: if add_content is called on a
                complete RepositoryVersion
        """
        if self.complete:
            raise ResourceImmutableError(self)

        repo_content = []
        for content_pk in content.exclude(pk__in=self.content).values_list('pk', flat=True):
            repo_content.append(
                RepositoryContent(
                    repository=self.repository,
                    content_id=content_pk,
                    version_added=self
                )
            )

        RepositoryContent.objects.bulk_create(repo_content)
Esempio n. 2
0
    def remove_content(self, content):
        """
        Remove content from the repository.

        Args:
            content (django.db.models.QuerySet): Set of Content to remove

        Raise:
            pulpcore.exception.ResourceImmutableError: if remove_content is called on a
                complete RepositoryVersion
        """

        if self.complete:
            raise ResourceImmutableError(self)

        if not content or not content.count():
            return

        # Normalize representation if content has already been added in this version.
        # Undo addition by deleting the RepositoryContent.
        RepositoryContent.objects.filter(
            repository=self.repository,
            content_id__in=content,
            version_added=self,
            version_removed=None,
        ).delete()

        q_set = RepositoryContent.objects.filter(repository=self.repository,
                                                 content_id__in=content,
                                                 version_removed=None)
        q_set.update(version_removed=self)
Esempio n. 3
0
    def add_content(self, content):
        """
        Add a content unit to this version, and possibly remove duplcates in some situations.

        In some situations plugin writers have configured new content to automatically remove other
        content in the repository. For example, `pulp_file` content would conflict with a
        `relative_path`. That is resolved by having the newer associated unit automatically remove
        the other `pulp_file` content with the same `relative_path`.

        Args:
           content (django.db.models.QuerySet): Set of Content to add

        Raise:
            pulpcore.exception.ResourceImmutableError: if add_content is called on a
                complete RepositoryVersion
        """

        if self.complete:
            raise ResourceImmutableError(self)

        query_for_repo_duplicates_by_type = defaultdict(lambda: Q())
        for item in content.all():
            if item.repo_key == ():
                continue
            unit_q_dict = {
                field: getattr(item, field) for field in item.repo_key
            }
            query_for_repo_duplicates_by_type[item._meta.model] |= Q(**unit_q_dict)

        for model in query_for_repo_duplicates_by_type:
            _logger.debug(_("Removing duplicates for type: {}".format(model)))
            qs = model.objects.filter(query_for_repo_duplicates_by_type[model])
            self.remove_content(qs)

        repo_content = []
        for content_pk in content.exclude(pk__in=self.content).values_list('pk', flat=True):
            repo_content.append(
                RepositoryContent(
                    repository=self.repository,
                    content_id=content_pk,
                    version_added=self
                )
            )

        RepositoryContent.objects.bulk_create(repo_content)
Esempio n. 4
0
    def remove_content(self, content):
        """
        Remove content from the repository.

        Args:
            content (django.db.models.QuerySet): Set of Content to remove

        Raise:
            pulpcore.exception.ResourceImmutableError: if remove_content is called on a
                complete RepositoryVersion
        """

        if self.complete:
            raise ResourceImmutableError(self)

        q_set = RepositoryContent.objects.filter(repository=self.repository,
                                                 content_id__in=content,
                                                 version_removed=None)
        q_set.update(version_removed=self)
Esempio n. 5
0
    def add_content(self, content):
        """
        Add a content unit to this version.

        Args:
           content (pulpcore.app.models.Content): a content model to add

        Raise:
            pulpcore.exception.ResourceImmutableError: if add_content is called on a
                complete RepositoryVersion
        """
        if self.complete:
            raise ResourceImmutableError(self)

        if self.contains(content):
            return

        association = RepositoryContent(repository=self.repository,
                                        content=content,
                                        version_added=self)
        association.save()
Esempio n. 6
0
    def add_content(self, content):
        """
        Add a content unit to this version.

        Args:
           content (django.db.models.QuerySet): Set of Content to add

        Raise:
            pulpcore.exception.ResourceImmutableError: if add_content is called on a
                complete RepositoryVersion
        """

        if self.complete:
            raise ResourceImmutableError(self)

        repo_content = []
        to_add = set(content.values_list('pk', flat=True))
        for existing in batch_qs(
                self.content.order_by('pk').values_list('pk', flat=True)):
            to_add = to_add - set(existing.all())

        # Normalize representation if content has already been removed in this version and
        # is re-added: Undo removal by setting version_removed to None.
        for removed in batch_qs(self.removed().order_by('pk').values_list(
                'pk', flat=True)):
            to_readd = to_add.intersection(set(removed))
            if to_readd:
                RepositoryContent.objects.filter(
                    content__in=to_readd,
                    repository=self.repository,
                    version_removed=self).update(version_removed=None)
                to_add = to_add - to_readd

        for content_pk in to_add:
            repo_content.append(
                RepositoryContent(repository=self.repository,
                                  content_id=content_pk,
                                  version_added=self))

        RepositoryContent.objects.bulk_create(repo_content)
Esempio n. 7
0
    def remove_content(self, content):
        """
        Remove content from the repository.

        Args:
            content (pulpcore.app.models.Content): A content model to remove

        Raise:
            pulpcore.exception.ResourceImmutableError: if remove_content is called on a
                complete RepositoryVersion
        """

        if self.complete:
            raise ResourceImmutableError(self)

        if not self.contains(content):
            return

        q_set = RepositoryContent.objects.filter(repository=self.repository,
                                                 content=content,
                                                 version_removed=None)
        q_set.update(version_removed=self)
Esempio n. 8
0
    def add_content(self, content):
        """
        Add a content unit to this version.

        Args:
           content (django.db.models.QuerySet): Set of Content to add

        Raise:
            pulpcore.exception.ResourceImmutableError: if add_content is called on a
                complete RepositoryVersion
        """

        if self.complete:
            raise ResourceImmutableError(self)

        error_messages = []
        for type_obj in self.repository.CONTENT_TYPES:
            if type_obj.repo_key_fields == ():
                continue

            pulp_type = type_obj.get_pulp_type()
            repo_key_fields = type_obj.repo_key_fields
            new_content_total = type_obj.objects.filter(
                pk__in=content.filter(pulp_type=pulp_type)
            ).count()
            unique_new_content_total = type_obj.objects.filter(
                pk__in=content.filter(pulp_type=pulp_type)
            ).distinct(*repo_key_fields).count()

            if unique_new_content_total < new_content_total:
                error_messages.append(_(
                    "More than one {pulp_type} content with the duplicate values for {fields}."
                    ).format(
                        pulp_type=pulp_type,
                        fields=", ".join(repo_key_fields),
                    )
                )
        if error_messages:
            raise ValueError(
                _("Cannot create repository version. {msg}").format(msg=", ".join(error_messages))
            )

        repo_content = []
        to_add = set(content.exclude(pk__in=self.content).values_list('pk', flat=True))

        # Normalize representation if content has already been removed in this version and
        # is re-added: Undo removal by setting version_removed to None.
        for removed in batch_qs(self.removed().order_by('pk').values_list('pk', flat=True)):
            to_readd = to_add.intersection(set(removed))
            if to_readd:
                RepositoryContent.objects.filter(
                    content__in=to_readd,
                    repository=self.repository,
                    version_removed=self
                ).update(version_removed=None)
                to_add = to_add - to_readd

        for content_pk in to_add:
            repo_content.append(
                RepositoryContent(
                    repository=self.repository,
                    content_id=content_pk,
                    version_added=self
                )
            )

        RepositoryContent.objects.bulk_create(repo_content)