示例#1
0
 def test_success_redirects_if_exists(self):
     test_path = '/a/path/?with=query&a=sting'
     view = BatchDeterminationCreateView()
     view.request = self.dummy_request('?next=' +
                                       urllib.parse.quote_plus(test_path))
     redirect_url = view.get_success_url()
     self.assertEqual(redirect_url, test_path)
示例#2
0
    def form_valid(self, form):
        submissions = form.cleaned_data['submissions']
        transitions = form.cleaned_data.get('action')

        try:
            redirect = BatchDeterminationCreateView.should_redirect(self.request, submissions, transitions)
        except ValueError as e:
            messages.warning(self.request, 'Could not determine: ' + str(e))
            return self.form_invalid(form)
        else:
            if redirect:
                return redirect

        failed = []
        phase_changes = {}
        for submission in submissions:
            valid_actions = {action for action, _ in submission.get_actions_for_user(self.request.user)}
            old_phase = submission.phase
            try:
                transition = (valid_actions & set(transitions)).pop()
                submission.perform_transition(
                    transition,
                    self.request.user,
                    request=self.request,
                    notify=False,
                )
            except (PermissionDenied, KeyError):
                failed.append(submission)
            else:
                phase_changes[submission.id] = old_phase

        if failed:
            messages.warning(
                self.request,
                _('Failed to update: ') +
                ', '.join(str(submission) for submission in failed)
            )

        succeeded_submissions = submissions.exclude(id__in=[submission.id for submission in failed])
        messenger(
            MESSAGES.BATCH_TRANSITION,
            user=self.request.user,
            request=self.request,
            sources=succeeded_submissions,
            related=phase_changes,
        )

        ready_for_review = [
            phase for phase in transitions
            if phase in review_statuses
        ]
        if ready_for_review:
            messenger(
                MESSAGES.BATCH_READY_FOR_REVIEW,
                user=self.request.user,
                request=self.request,
                sources=succeeded_submissions.filter(status__in=ready_for_review),
            )

        return super().form_valid(form)
示例#3
0
 def test_sets_next_on_redirect(self):
     test_path = '/a/path/?with=query&a=sting'
     request = RequestFactory().get('', PATH_INFO=test_path)
     redirect = BatchDeterminationCreateView.should_redirect(
         request,
         ApplicationSubmission.objects.none(),
         ['rejected'],
     )
     url = urllib.parse.urlparse(redirect.url)
     query = urllib.parse.parse_qs(url.query)
     next_path = urllib.parse.unquote_plus(query['next'][0])
     self.assertEqual(next_path, test_path)
示例#4
0
 def test_success_if_no_next(self):
     view = BatchDeterminationCreateView()
     view.request = self.dummy_request('')
     redirect_url = view.get_success_url()
     self.assertEqual(redirect_url, reverse_lazy('apply:submissions:list'))