|
147 | 147 | PHASES_MAPPING, |
148 | 148 | STAGE_CHANGE_ACTIONS, |
149 | 149 | active_statuses, |
| 150 | + get_withdraw_action_for_stage, |
150 | 151 | review_statuses, |
151 | 152 | ) |
152 | 153 |
|
@@ -1796,43 +1797,39 @@ def form_valid(self, form): |
1796 | 1797 | return super().form_valid(form) |
1797 | 1798 |
|
1798 | 1799 |
|
1799 | | -class SubmissionWithdrawView(SingleObjectTemplateResponseMixin, BaseDetailView): |
| 1800 | +@method_decorator(login_required, name="dispatch") |
| 1801 | +class SubmissionWithdrawView( |
| 1802 | + SingleObjectTemplateResponseMixin, UserPassesTestMixin, BaseDetailView |
| 1803 | +): |
1800 | 1804 | model = ApplicationSubmission |
1801 | 1805 | success_url = reverse_lazy("funds:submissions:list") |
1802 | 1806 | template_name_suffix = "_confirm_withdraw" |
1803 | 1807 |
|
| 1808 | + def dispatch(self, *args, **kwargs): |
| 1809 | + self.submission = self.get_object() |
| 1810 | + return super().dispatch(*args, **kwargs) |
| 1811 | + |
1804 | 1812 | def post(self, request, *args, **kwargs): |
1805 | 1813 | return self.withdraw(request, *args, **kwargs) |
1806 | 1814 |
|
1807 | 1815 | def withdraw(self, request, *args, **kwargs): |
1808 | | - if not settings.ENABLE_SUBMISSION_WITHDRAWAL: |
1809 | | - raise PermissionDenied |
| 1816 | + withdraw_action = get_withdraw_action_for_stage(self.submission.stage) |
1810 | 1817 |
|
1811 | | - if not request.user.is_applicant: |
1812 | | - raise PermissionDenied |
1813 | | - |
1814 | | - obj = self.get_object() |
1815 | | - |
1816 | | - withdraw_actions = [ |
1817 | | - action for action in obj.workflow.keys() if "withdraw" in action |
1818 | | - ] |
1819 | | - |
1820 | | - if len(withdraw_actions) == 1: |
1821 | | - action = withdraw_actions[0] |
1822 | | - obj.perform_transition( |
1823 | | - action, self.request.user, request=self.request, notify=False |
1824 | | - ) |
1825 | | - elif len(withdraw_actions) > 1: |
1826 | | - raise ImproperlyConfigured( |
1827 | | - f'In workflow "{obj.workflow}" too many withdraw actions: "{withdraw_actions}"' |
| 1818 | + if withdraw_action: |
| 1819 | + self.submission.perform_transition( |
| 1820 | + withdraw_action, self.request.user, request=self.request, notify=False |
1828 | 1821 | ) |
1829 | | - elif len(withdraw_actions) < 1: |
| 1822 | + else: |
1830 | 1823 | raise ImproperlyConfigured( |
1831 | | - f'No withdraw actions found in workflow "{obj.workflow}"' |
| 1824 | + f'No withdraw actions found in workflow "{self.submission.workflow}"' |
1832 | 1825 | ) |
1833 | 1826 |
|
1834 | | - success_url = obj.get_absolute_url() |
1835 | | - return HttpResponseRedirect(success_url) |
| 1827 | + return HttpResponseRedirect(self.submission.get_absolute_url()) |
| 1828 | + |
| 1829 | + def test_func(self): |
| 1830 | + can_withdraw = self.submission.phase.permissions.can_withdraw(self.request.user) |
| 1831 | + |
| 1832 | + return settings.ENABLE_SUBMISSION_WITHDRAWAL and can_withdraw |
1836 | 1833 |
|
1837 | 1834 |
|
1838 | 1835 | @method_decorator(login_required, name="dispatch") |
|
0 commit comments