forked from pulp/pulp_ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] Add CollectionVersionMark as a content.
fixes: pulp#1325
- Loading branch information
1 parent
208b6c3
commit 225298b
Showing
11 changed files
with
356 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add CollectionVersionMark as a content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Generated by Django 3.2.16 on 2023-01-17 15:01 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('core', '0098_pulp_labels'), | ||
('ansible', '0046_add_fulltext_search_fix'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='CollectionVersionMark', | ||
fields=[ | ||
('content_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, related_name='ansible_collectionversionmark', serialize=False, to='core.content')), | ||
('value', models.SlugField()), | ||
('marked_collection', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='marks', to='ansible.collectionversion')), | ||
], | ||
options={ | ||
'default_related_name': '%(app_label)s_%(model_name)s', | ||
'unique_together': {('value', 'marked_collection')}, | ||
}, | ||
bases=('core.content',), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"""Tasks for CollectionVersionMark""" | ||
from pulpcore.plugin.tasking import add_and_remove | ||
from pulp_ansible.app.models import AnsibleRepository, CollectionVersion, CollectionVersionMark | ||
|
||
|
||
def mark(repository_href, content_hrefs, value): | ||
"""The mark task.""" | ||
repository = AnsibleRepository.objects.get(pk=repository_href) | ||
content = _get_content_queryset(repository, content_hrefs) | ||
marks_in_repo = _get_list_of_marks_pk_in_repo(repository, content, value) | ||
|
||
marks_to_add = [] | ||
for collection_version in content: | ||
# A mark for the same collection and value may exist in another repo. | ||
mark, _created = CollectionVersionMark.objects.get_or_create( | ||
marked_collection=collection_version, value=value | ||
) | ||
if mark.pk not in marks_in_repo: | ||
marks_to_add.append(mark.pk) | ||
|
||
add_and_remove( | ||
repository_pk=repository.pk, | ||
add_content_units=marks_to_add, | ||
remove_content_units=[], | ||
) | ||
|
||
|
||
def unmark(repository_href, content_hrefs, value): | ||
"""The unmark task.""" | ||
repository = AnsibleRepository.objects.get(pk=repository_href) | ||
content = _get_content_queryset(repository, content_hrefs) | ||
marks_in_repo = _get_list_of_marks_pk_in_repo(repository, content, value) | ||
add_and_remove( | ||
repository_pk=repository.pk, | ||
add_content_units=[], | ||
remove_content_units=marks_in_repo, | ||
) | ||
|
||
|
||
def _get_list_of_marks_pk_in_repo(repository, content, value): | ||
"""Get all the marks from repo having value and content""" | ||
return ( | ||
repository.latest_version() | ||
.content.filter( | ||
pulp_type=CollectionVersionMark.get_pulp_type(), | ||
ansible_collectionversionmark__value=value, | ||
ansible_collectionversionmark__marked_collection__in=content, | ||
) | ||
.values_list("pk", flat=True) | ||
) | ||
|
||
|
||
def _get_content_queryset(repository, content_hrefs): | ||
"""Return content queryset based on a repo and content hrefs""" | ||
if content_hrefs == ["*"]: | ||
content_in_latest_version = repository.latest_version().content.filter( | ||
pulp_type=CollectionVersion.get_pulp_type() | ||
) | ||
return CollectionVersion.objects.filter(pk__in=content_in_latest_version) | ||
return CollectionVersion.objects.filter(pk__in=content_hrefs) |
Oops, something went wrong.