Skip to content

Commit

Permalink
Dispatch a task to update publishers.
Browse files Browse the repository at this point in the history
closes #2409
  • Loading branch information
asmacdo committed Jul 3, 2017
1 parent 5312ff4 commit e81d0a6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
3 changes: 2 additions & 1 deletion platform/pulpcore/app/serializers/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ class PublisherSerializer(MasterModelSerializer):
)
relative_path = serializers.CharField(
help_text=_('The (relative) path component of the published url'),
required=False
required=False,
allow_blank=True
)
last_published = serializers.DateTimeField(
help_text=_('Timestamp of the most recent successful publish.'),
Expand Down
32 changes: 30 additions & 2 deletions platform/pulpcore/app/tasks/publisher.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
from gettext import gettext as _

from celery import shared_task
from django.http import QueryDict

from pulpcore.app import models
from pulpcore.app.apps import get_plugin_config
from pulpcore.tasking.services import storage
from pulpcore.tasking.tasks import UserFacingTask


@shared_task(base=UserFacingTask)
def update(publisher_id, app_label, serializer_name, data=None, partial=False):
"""
Update an instance of a :class:`~pulpcore.app.models.Publisher`
Args:
publisher_id (str): id of the publisher instance
app_label (str): the Django app label of the plugin that provides the publisher
serializer_name (str): name of the serializer class for this publisher
data (dict): Data to update on the publisher. keys are field names, values are new values.
partial (bool): When true, update only the specified fields. When false, omitted fields
are set to None.
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.
"""
instance = models.Publisher.objects.get(id=publisher_id).cast()
data_querydict = QueryDict("", mutable=True)
data_querydict.update(data)
# The publisher serializer class is different for each plugin
serializer_class = get_plugin_config(app_label).named_serializers[serializer_name]
serializer = serializer_class(instance, data=data_querydict, partial=partial)
serializer.is_valid(raise_exception=True)
serializer.save()


@shared_task(base=UserFacingTask)
def delete(repo_name, publisher_name):
"""
Expand All @@ -19,6 +46,7 @@ def delete(repo_name, publisher_name):
"""
models.Publisher.objects.filter(name=publisher_name, repository__name=repo_name).delete()


@shared_task(base=UserFacingTask)
def publish(repo_name, publisher_name):
"""
Expand Down
12 changes: 12 additions & 0 deletions platform/pulpcore/app/viewsets/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ class PublisherViewSet(NamedModelViewSet):
queryset = Publisher.objects.all()
filter_class = PublisherFilter

def update(self, request, pk, partial=False):
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True)
app_label = instance._meta.app_label
async_result = tasks.publisher.update.apply_async_with_reservation(
tags.RESOURCE_REPOSITORY_TYPE, instance.repository.name,
args=(instance.id, app_label, serializer.__class__.__name__),
kwargs={'data': request.data, 'partial': partial}
)
return OperationPostponedResponse([async_result])

def destroy(self, request, pk):
publisher = self.get_object()
repo_name = publisher.repository.name
Expand Down

0 comments on commit e81d0a6

Please sign in to comment.