Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix starting multiple async transitions through API #2753

Merged
merged 2 commits into from
Aug 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/ralph/lib/transitions/api/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# -*- coding: utf-8 -*-
import itertools

from django import forms
from django.core.urlresolvers import reverse
from rest_framework import serializers, status
from rest_framework.exceptions import ValidationError as DRFValidationError
from rest_framework.response import Response
from rest_framework.settings import api_settings
from rest_framework.views import APIView

from ralph.api import RalphReadOnlyAPIViewSet
Expand All @@ -16,7 +19,9 @@
TransitionModelSerializer,
TransitionSerializer
)
from ralph.lib.transitions.exceptions import TransitionNotAllowedError
from ralph.lib.transitions.models import (
_check_instances_for_transition,
_transition_data_validation,
Action,
run_transition,
Expand Down Expand Up @@ -158,9 +163,20 @@ def _run_additional_validation(self, data):
]
raise DRFValidationError(api_errors)

def _check_instances(self):
try:
_check_instances_for_transition(self.objects, self.transition)
except TransitionNotAllowedError as e:
raise DRFValidationError({
api_settings.NON_FIELD_ERRORS_KEY: list(itertools.chain(
*e.errors.values()
))
})

def post(self, request, *args, **kwargs):
serializer = self.get_serializer_class()(data=request.data)
serializer.is_valid(raise_exception=True)
self._check_instances()
self._run_additional_validation(serializer.validated_data)
result = {}
data = self.add_function_name_to_data(serializer.validated_data)
Expand Down
2 changes: 1 addition & 1 deletion src/ralph/lib/transitions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def _check_instances_for_transition(
)
)
errors[instance].append(
'Another async transition for this object is already stared'
'Another async transition for this object is already started' # noqa
)

if errors:
Expand Down
24 changes: 23 additions & 1 deletion src/ralph/lib/transitions/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,32 @@ def test_async_gui_running_new_when_another_in_progress_should_return_error(self
)
)
self.assertIn(
'Another async transition for this object is already stared',
'Another async transition for this object is already started',
str(list(request.context['messages'])[1])
)

def test_async_api_running_new_when_another_in_progress_should_return_error(self): # noqa
# mock another async job running
TransitionJob.objects.create(
obj=self.bo,
transition=self.transition_2,
status=JobStatus.STARTED,
service_name='ASYNC',
)
response = self.api_client.post(
reverse(
'transition-view',
args=(self.transition_2.id, self.bo.pk)
),
self.prepare_api_data()
)
self.assertEqual(response.status_code, 400)
self.assertEqual(response.data, {
'non_field_errors': [
'Another async transition for this object is already started'
]
})

def test_api_options(self):
request = self.api_client.options(
reverse(
Expand Down