Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Problem: async update and delete code is duplicated
Solution: add general update and delete tasks and viewsets This patch creates two new tasks that are used by both the publisher viewset and the importer viewset. This patch also introduces a new base viewset. Both the importer and publisher viewset inherit from it. closes #3038 https://pulp.plan.io/issues/3038
- Loading branch information
Showing
8 changed files
with
124 additions
and
137 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
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 |
---|---|---|
@@ -1 +1 @@ | ||
from pulpcore.app.tasks import importer, orphan, publisher, repository # noqa | ||
from pulpcore.app.tasks import base, importer, orphan, publisher, repository # noqa |
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,56 @@ | ||
from celery import shared_task | ||
from django.http import QueryDict | ||
|
||
from pulpcore.tasking.tasks import UserFacingTask | ||
from pulpcore.app.apps import get_plugin_config | ||
|
||
|
||
@shared_task(base=UserFacingTask) | ||
def general_update(instance_id, app_label, serializer_name, *args, **kwargs): | ||
""" | ||
Update a model | ||
The model instance is identified using the app_label, id, and serializer name. The serializer is | ||
used to perform validation. | ||
Args: | ||
id (str): the id of the model | ||
app_label (str): the Django app label of the plugin that provides the model | ||
serializer_name (str): name of the serializer class for the model | ||
data (dict): dictionary whose keys represent the fields of the model and their corresponding | ||
values. | ||
partial (bool): When true, only the fields specified in the data dictionary are updated. | ||
When false, any fields missing from the data dictionary are assumed to be None and | ||
their values are updated as such. | ||
Raises: | ||
:class:`rest_framework.exceptions.ValidationError`: When serializer instance can't be saved | ||
due to validation error. This theoretically should never occur since validation is | ||
performed before the task is dispatched. | ||
""" | ||
data = kwargs.pop('data', None) | ||
partial = kwargs.pop('partial', False) | ||
serializer_class = get_plugin_config(app_label).named_serializers[serializer_name] | ||
instance = serializer_class.Meta.model.objects.get(id=instance_id).cast() | ||
data_querydict = QueryDict('', mutable=True) | ||
data_querydict.update(data) | ||
serializer = serializer_class(instance, data=data_querydict, partial=partial) | ||
serializer.is_valid(raise_exception=True) | ||
serializer.save() | ||
|
||
|
||
@shared_task(base=UserFacingTask) | ||
def general_delete(instance_id, app_label, serializer_name): | ||
""" | ||
Delete a model | ||
The model instance is identified using the app_label, id, and serializer name. | ||
Args: | ||
id (str): the id of the model | ||
app_label (str): the Django app label of the plugin that provides the model | ||
serializer_name (str): name of the serializer class for the model | ||
""" | ||
serializer_class = get_plugin_config(app_label).named_serializers[serializer_name] | ||
instance = serializer_class.Meta.model.objects.get(id=instance_id).cast() | ||
instance.delete() |
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