How to use the pulpcore.app.models.Content function in pulpcore

To help you get started, we’ve selected a few pulpcore examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pulp / pulp / pulpcore / app / serializers / content.py View on Github external
)

    class Meta:
        model = models.Content
        fields = base.MasterModelSerializer.Meta.fields + ('_artifact',)


class MultipleArtifactContentSerializer(BaseContentSerializer):
    _artifacts = fields.ContentArtifactsField(
        help_text=_("A dict mapping relative paths inside the Content to the corresponding"
                    "Artifact URLs. E.g.: {'relative/path': "
                    "'/artifacts/1/'"),
    )

    class Meta:
        model = models.Content
        fields = base.MasterModelSerializer.Meta.fields + ('_artifacts',)


class ArtifactSerializer(base.ModelSerializer):
    _href = base.IdentityField(
        view_name='artifacts-detail',
    )

    file = serializers.FileField(
        help_text=_("The stored file."),
        required=True
    )

    size = serializers.IntegerField(
        help_text=_("The size of the file in bytes."),
        required=False
github pulp / pulp / pulpcore / app / viewsets / repository.py View on Github external
qs (django.db.models.query.QuerySet): The RepositoryVersion Queryset
            value (string): of content href to filter

        Returns:
            Queryset of the RepositoryVersions containing the specified content
        """

        if value is None:
            # user didn't supply a value
            return qs

        if not value:
            raise serializers.ValidationError(detail=_('No value supplied for content filter'))

        # Get the content object from the content_href
        content = NamedModelViewSet.get_resource(value, Content)

        # Get the repository from the parent request.
        repository_pk = self.parent.request.parser_context['kwargs']['repository_pk']
        repository = Repository.objects.get(pk=repository_pk)

        repository_content_set = RepositoryContent.objects.filter(content=content,
                                                                  repository=repository)

        # Get the sorted list of version_added and version_removed.
        version_added = list(repository_content_set.values_list('version_added__number', flat=True))

        # None values have to be filtered out from version_removed,
        # in order for zip_longest to pass it a default fillvalue
        version_removed = list(filter(None.__ne__, repository_content_set
                                      .values_list('version_removed__number', flat=True)))
github pulp / pulpcore / pulpcore / plugin / actions.py View on Github external
base_version_pk = self.get_resource(request.data["base_version"], RepositoryVersion).pk
        else:
            base_version_pk = None

        if "add_content_units" in request.data:
            for url in request.data["add_content_units"]:
                content = self.get_resource(url, Content)
                add_content_units.append(content.pk)

        if "remove_content_units" in request.data:
            for url in request.data["remove_content_units"]:
                if url == "*":
                    remove_content_units = [url]
                    break
                else:
                    content = self.get_resource(url, Content)
                    remove_content_units.append(content.pk)

        result = enqueue_with_reservation(
            tasks.repository.add_and_remove,
            [repository],
            kwargs={
                "repository_pk": pk,
                "base_version_pk": base_version_pk,
                "add_content_units": add_content_units,
                "remove_content_units": remove_content_units,
            },
        )
        return OperationPostponedResponse(result, request)
github pulp / pulpcore / pulpcore / app / tasks / importer.py View on Github external
cfg = get_plugin_config(plugin_name)

        resulting_content_ids = []
        for res_class in cfg.exportable_classes:
            filename = f"{res_class.__module__}.{res_class.__name__}.json"
            a_result = _import_file(os.path.join(rv_path, filename), res_class)
            resulting_content_ids.extend(
                row.object_id for row in a_result.rows if row.import_type in ("new", "update")
            )

        # Once all content exists, create the ContentArtifact links
        ca_path = os.path.join(rv_path, CA_FILE)
        _import_file(ca_path, ContentArtifactResource)

        # Create the repo version
        content = Content.objects.filter(pk__in=resulting_content_ids)
        with dest_repo.new_version() as new_version:
            new_version.set_content(content)
github ansible / galaxy / galaxy / main / models / collection.py View on Github external
metadata = psql_fields.JSONField(default=dict)
    contents = psql_fields.JSONField(default=list)
    quality_score = models.FloatField(
        null=True,
        validators=[MinValueValidator(0.0), MaxValueValidator(5.0)],
    )

    readme_mimetype = models.CharField(max_length=32, blank=True)
    readme_text = models.TextField(blank=True)
    readme_html = models.TextField(blank=True)

    # References
    collection = models.ForeignKey(
        Collection, on_delete=models.CASCADE, related_name='versions')
    _parent_ref = models.OneToOneField(
        pulp_models.Content, on_delete=models.CASCADE,
        parent_link=True, db_column='id'
    )

    class Meta:
        unique_together = (
            'collection',
            'version',
        )

    def get_content_artifact(self) -> pulp_models.ContentArtifact:
        """Returns a ContentArtifact object related to collection version."""
        return pulp_models.ContentArtifact.objects.filter(content=self).first()

    def get_download_url(self) -> str:
        """Builds artifact download url pointing to Pulp's content app."""
        prefix = settings.GALAXY_DOWNLOAD_URL
github pulp / pulp / pulpcore / app / tasks / repository.py View on Github external
add_content_units (list): List of PKs for :class:`~pulpcore.app.models.Content` that
            should be added to the previous Repository Version for this Repository.
        remove_content_units (list): List of PKs for:class:`~pulpcore.app.models.Content` that
            should be removed from the previous Repository Version for this Repository.
        base_version_pk (int): the primary key for a RepositoryVersion whose content will be used
            as the initial set of content for our new RepositoryVersion
    """
    repository = models.Repository.objects.get(pk=repository_pk)

    if base_version_pk:
        base_version = models.RepositoryVersion.objects.get(pk=base_version_pk)
    else:
        base_version = None

    with models.RepositoryVersion.create(repository, base_version=base_version) as new_version:
        new_version.add_content(models.Content.objects.filter(pk__in=add_content_units))
        new_version.remove_content(models.Content.objects.filter(pk__in=remove_content_units))
github pulp / pulpcore / pulpcore / app / tasks / orphan.py View on Github external
def orphan_cleanup():
    """
    Delete all orphan Content and Artifact records.
    This task removes Artifact files from the filesystem as well.
    """
    # Content cleanup
    content = Content.objects.exclude(
        pk__in=RepositoryContent.objects.values_list("content_id", flat=True)
    )
    content = content.exclude(pulp_type="core.{}".format(PublishedMetadata.TYPE))
    progress_bar = ProgressReport(
        message="Clean up orphan Content",
        total=content.count(),
        code="clean-up.content",
        done=0,
        state="running",
    )
    progress_bar.save()
    content.delete()
    progress_bar.done = progress_bar.total
    progress_bar.state = "completed"
    progress_bar.save()
github pulp / pulpcore / pulpcore / app / tasks / repository.py View on Github external
repository = models.Repository.objects.get(pk=repository_pk).cast()

    if base_version_pk:
        base_version = models.RepositoryVersion.objects.get(pk=base_version_pk)
    else:
        base_version = None

    if "*" in remove_content_units:
        latest = repository.latest_version()
        if latest:
            remove_content_units = latest.content.values_list("pk", flat=True)
        else:
            remove_content_units = []

    with repository.new_version(base_version=base_version) as new_version:
        new_version.remove_content(models.Content.objects.filter(pk__in=remove_content_units))
        new_version.add_content(models.Content.objects.filter(pk__in=add_content_units))
github pulp / pulp / pulpcore / app / tasks / orphan.py View on Github external
def orphan_cleanup():
    """
    Delete all orphan Content and Artifact records.
    This task removes Artifact files from the filesystem as well.
    """
    # Content cleanup
    content = Content.objects.exclude(pk__in=RepositoryContent.objects.values_list('content_id',
                                                                                   flat=True))
    progress_bar = ProgressBar(message='Clean up orphan Content', total=content.count(),
                               done=0, state='running')
    progress_bar.save()
    content.delete()
    progress_bar.done = progress_bar.total
    progress_bar.state = 'completed'
    progress_bar.save()

    # Artifact cleanup
    artifacts = Artifact.objects.exclude(pk__in=ContentArtifact.objects.values_list('artifact_id',
                                                                                    flat=True))
    progress_bar = ProgressBar(message='Clean up orphan Artifacts', total=artifacts.count(),
                               done=0, state='running')
    progress_bar.save()
    for artifact in artifacts:
github pulp / pulp / pulpcore / app / tasks / repository.py View on Github external
should be added to the previous Repository Version for this Repository.
        remove_content_units (list): List of PKs for:class:`~pulpcore.app.models.Content` that
            should be removed from the previous Repository Version for this Repository.
        base_version_pk (int): the primary key for a RepositoryVersion whose content will be used
            as the initial set of content for our new RepositoryVersion
    """
    repository = models.Repository.objects.get(pk=repository_pk)

    if base_version_pk:
        base_version = models.RepositoryVersion.objects.get(pk=base_version_pk)
    else:
        base_version = None

    with models.RepositoryVersion.create(repository, base_version=base_version) as new_version:
        new_version.add_content(models.Content.objects.filter(pk__in=add_content_units))
        new_version.remove_content(models.Content.objects.filter(pk__in=remove_content_units))