|
136 | 136 | PHASES_MAPPING, |
137 | 137 | STAGE_CHANGE_ACTIONS, |
138 | 138 | active_statuses, |
| 139 | + get_withdraw_action_for_stage, |
139 | 140 | review_statuses, |
140 | 141 | ) |
141 | 142 |
|
@@ -1583,43 +1584,39 @@ def form_valid(self, form): |
1583 | 1584 | return super().form_valid(form) |
1584 | 1585 |
|
1585 | 1586 |
|
1586 | | -class SubmissionWithdrawView(SingleObjectTemplateResponseMixin, BaseDetailView): |
| 1587 | +@method_decorator(login_required, name="dispatch") |
| 1588 | +class SubmissionWithdrawView( |
| 1589 | + SingleObjectTemplateResponseMixin, UserPassesTestMixin, BaseDetailView |
| 1590 | +): |
1587 | 1591 | model = ApplicationSubmission |
1588 | 1592 | success_url = reverse_lazy("funds:submissions:list") |
1589 | 1593 | template_name_suffix = "_confirm_withdraw" |
1590 | 1594 |
|
| 1595 | + def dispatch(self, *args, **kwargs): |
| 1596 | + self.submission = self.get_object() |
| 1597 | + return super().dispatch(*args, **kwargs) |
| 1598 | + |
1591 | 1599 | def post(self, request, *args, **kwargs): |
1592 | 1600 | return self.withdraw(request, *args, **kwargs) |
1593 | 1601 |
|
1594 | 1602 | def withdraw(self, request, *args, **kwargs): |
1595 | | - if not settings.ENABLE_SUBMISSION_WITHDRAWAL: |
1596 | | - raise PermissionDenied |
1597 | | - |
1598 | | - if not request.user.is_applicant: |
1599 | | - raise PermissionDenied |
1600 | | - |
1601 | | - obj = self.get_object() |
| 1603 | + withdraw_action = get_withdraw_action_for_stage(self.submission.stage) |
1602 | 1604 |
|
1603 | | - withdraw_actions = [ |
1604 | | - action for action in obj.workflow.keys() if "withdraw" in action |
1605 | | - ] |
1606 | | - |
1607 | | - if len(withdraw_actions) == 1: |
1608 | | - action = withdraw_actions[0] |
1609 | | - obj.perform_transition( |
1610 | | - action, self.request.user, request=self.request, notify=False |
1611 | | - ) |
1612 | | - elif len(withdraw_actions) > 1: |
1613 | | - raise ImproperlyConfigured( |
1614 | | - f'In workflow "{obj.workflow}" too many withdraw actions: "{withdraw_actions}"' |
| 1605 | + if withdraw_action: |
| 1606 | + self.submission.perform_transition( |
| 1607 | + withdraw_action, self.request.user, request=self.request, notify=False |
1615 | 1608 | ) |
1616 | | - elif len(withdraw_actions) < 1: |
| 1609 | + else: |
1617 | 1610 | raise ImproperlyConfigured( |
1618 | | - f'No withdraw actions found in workflow "{obj.workflow}"' |
| 1611 | + f'No withdraw actions found in workflow "{self.submission.workflow}"' |
1619 | 1612 | ) |
1620 | 1613 |
|
1621 | | - success_url = obj.get_absolute_url() |
1622 | | - return HttpResponseRedirect(success_url) |
| 1614 | + return HttpResponseRedirect(self.submission.get_absolute_url()) |
| 1615 | + |
| 1616 | + def test_func(self): |
| 1617 | + can_withdraw = self.submission.phase.permissions.can_withdraw(self.request.user) |
| 1618 | + |
| 1619 | + return settings.ENABLE_SUBMISSION_WITHDRAWAL and can_withdraw |
1623 | 1620 |
|
1624 | 1621 |
|
1625 | 1622 | @method_decorator(login_required, name="dispatch") |
|
0 commit comments