Skip to content

Commit

Permalink
Custom redirect for transition (#3230)
Browse files Browse the repository at this point in the history
Add success_url to transition
  • Loading branch information
ar4s committed Feb 12, 2018
1 parent ef1df95 commit a4ea511
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/ralph/lib/transitions/forms.py
Expand Up @@ -130,5 +130,5 @@ class Meta:
model = Transition
fields = [
'name', 'source', 'target', 'run_asynchronously',
'async_service_name', 'template_name', 'actions',
'async_service_name', 'template_name', 'success_url', 'actions',
]
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import ralph.lib.mixins.fields


class Migration(migrations.Migration):

dependencies = [
('transitions', '0008_auto_20171211_1300'),
]

operations = [
migrations.AddField(
model_name='transition',
name='success_url',
field=ralph.lib.mixins.fields.NullableCharField(null=True, max_length=255, default=None, blank=True),
),
]
4 changes: 4 additions & 0 deletions src/ralph/lib/transitions/models.py
Expand Up @@ -37,6 +37,7 @@
JobQuerySet
)
from ralph.lib.metrics import statsd
from ralph.lib.mixins.fields import NullableCharField
from ralph.lib.mixins.models import AdminAbsoluteUrlMixin, TimeStampMixin
from ralph.lib.transitions.conf import (
DEFAULT_ASYNC_TRANSITION_SERVICE_NAME,
Expand Down Expand Up @@ -526,6 +527,9 @@ class Transition(models.Model):
target = models.CharField(max_length=50)
actions = models.ManyToManyField('Action')
template_name = models.CharField(max_length=255, blank=True, default='')
success_url = NullableCharField(
max_length=255, blank=True, null=True, default=None
)

class Meta:
unique_together = ('name', 'model')
Expand Down
16 changes: 16 additions & 0 deletions src/ralph/lib/transitions/tests/test_admin.py
Expand Up @@ -39,3 +39,19 @@ def test_custom_template_for_transition(self):
Order, 'custom template test',
template_name='test.html'
)

def test_success_url_should_redirect(self):
success_url = 'http://test.com/foo/bar/'
_, transition, _ = self._create_transition(
Order, 'packing', ['pack'], source=[OrderStatus.new.id],
target=OrderStatus.to_send.id,
success_url=success_url,
)
order = Order.objects.create()
transition_url = reverse(
'admin:tests_order_transition',
args=(order.pk, transition.pk,)
)
self.login_as_user()
response = self.client.post(transition_url)
self.assertEqual(response.url, success_url)
24 changes: 17 additions & 7 deletions src/ralph/lib/transitions/views.py
@@ -1,4 +1,5 @@
from copy import deepcopy
from itertools import repeat

from django import forms
from django.contrib import messages
Expand Down Expand Up @@ -44,6 +45,10 @@ def collect_actions(obj, transition):
return actions, any(return_attachment)


def build_params_url_for_redirect(ids):
return urlencode(list(zip(repeat('select', len(ids)), ids)))


class NonAtomicView(object):
@classonlymethod
def as_view(cls, **initkwargs):
Expand Down Expand Up @@ -278,19 +283,24 @@ def dispatch(
):
self.model = model
self.transition = get_object_or_404(Transition, pk=transition_pk)
ids = [int(i) for i in self.request.GET.getlist('select')]
self.objects = list(self.model.objects.filter(id__in=ids))
# TODO: self.obj is unnecessary - self.model is enough
self.ids = [int(i) for i in self.request.GET.getlist('select')]
self.objects = list(self.model.objects.filter(id__in=self.ids))
self.obj = self.objects[0]
return super().dispatch(request, *args, **kwargs)

def get_success_url(self):
transition_success_url = self.transition.success_url
if transition_success_url:
transition_success_url += '?' + build_params_url_for_redirect(
self.ids
)
transition_back_url = self.request.GET.get('back_url', None)
if not transition_back_url:
info = self.model._meta.app_label, self.model._meta.model_name
return reverse('admin:{}_{}_changelist'.format(*info))

return transition_back_url
transition_back_url = reverse(
'admin:{}_{}_changelist'.format(*info)
)
return transition_success_url or transition_back_url


class RunTransitionView(TransitionViewMixin, RalphTemplateView):
Expand All @@ -305,7 +315,7 @@ def dispatch(
return super().dispatch(request, *args, **kwargs)

def get_success_url(self):
return self.objects[0].get_absolute_url()
return self.transition.success_url or self.objects[0].get_absolute_url()

def get_async_transitions_awaiter_url(self, job_ids):
return get_admin_url(self.objects[0], 'current_transitions')
Expand Down

0 comments on commit a4ea511

Please sign in to comment.