Skip to content
Snippets Groups Projects
views.py 7.03 KiB
Newer Older
  • Learn to ignore specific revisions
  • from django.contrib import messages
    
    from django.contrib.auth.decorators import login_required
    
    from django.core.exceptions import PermissionDenied
    
    Dan Braghis's avatar
    Dan Braghis committed
    from django.http import HttpResponseRedirect
    from django.shortcuts import get_object_or_404
    from django.urls import reverse_lazy
    from django.utils.decorators import method_decorator
    
    from django.utils.translation import ugettext_lazy as _
    
    Dan Braghis's avatar
    Dan Braghis committed
    from django.views.generic import DetailView
    
    
    from opentech.apply.activity.models import Activity
    
    from opentech.apply.activity.messaging import messenger, MESSAGES
    
    Dan Braghis's avatar
    Dan Braghis committed
    from opentech.apply.funds.models import ApplicationSubmission
    
    from opentech.apply.funds.workflow import DETERMINATION_OUTCOMES
    from opentech.apply.utils.views import CreateOrUpdateView, ViewDispatcher
    from opentech.apply.users.decorators import staff_required
    
    Dan Braghis's avatar
    Dan Braghis committed
    
    from .forms import ConceptDeterminationForm, ProposalDeterminationForm
    
    from .models import Determination, TRANSITION_DETERMINATION, DeterminationMessageSettings, NEEDS_MORE_INFO
    
    Dan Braghis's avatar
    Dan Braghis committed
    
    
    def get_form_for_stage(submission):
        forms = [ConceptDeterminationForm, ProposalDeterminationForm]
    
        index = submission.workflow.stages.index(submission.stage)
    
    Dan Braghis's avatar
    Dan Braghis committed
        return forms[index]
    
    
    
    def determination_actions(user, submission):
        return [action[0] for action in submission.get_actions_for_user(user)]
    
    
    def transition_from_outcome(outcome, submission):
        for transition_name in submission.phase.transitions:
            try:
                transition_type = TRANSITION_DETERMINATION[transition_name]
            except KeyError:
                pass
            else:
                if transition_type == outcome:
                    return transition_name
    
    
    def can_edit_determination(user, determination, submission):
        outcome = transition_from_outcome(determination.outcome, submission)
        valid_outcomes = determination_actions(user, submission)
    
        return outcome in valid_outcomes
    
    def can_create_determination(user, submission):
    
        actions = determination_actions(user, submission)
        return any(action in DETERMINATION_OUTCOMES for action in actions)
    
    
    
    @method_decorator(staff_required, name='dispatch')
    
    Dan Braghis's avatar
    Dan Braghis committed
    class DeterminationCreateOrUpdateView(CreateOrUpdateView):
        model = Determination
        template_name = 'determinations/determination_form.html'
    
        def get_object(self, queryset=None):
    
    Dan Braghis's avatar
    Dan Braghis committed
            return self.model.objects.get(submission=self.submission)
    
    Dan Braghis's avatar
    Dan Braghis committed
    
        def dispatch(self, request, *args, **kwargs):
            self.submission = get_object_or_404(ApplicationSubmission, id=self.kwargs['submission_pk'])
    
    
            if not can_create_determination(request.user, self.submission):
    
    Dan Braghis's avatar
    Dan Braghis committed
                raise PermissionDenied()
    
    
            if self.submission.has_determination:
    
                messages.warning(request, _('A determination has already been submitted for that submission.'))
    
                return HttpResponseRedirect(reverse_lazy('apply:submissions:determinations:detail', args=(self.submission.id,)))
    
            try:
                determination = self.get_object()
            except Determination.DoesNotExist:
                pass
            else:
                if not can_edit_determination(request.user, determination, self.submission):
                    messages.warning(request, _('There is a draft determination you do not have permission to edit.'))
                    return HttpResponseRedirect(reverse_lazy('apply:submissions:determinations:detail', args=(self.submission.id,)))
    
    
    Dan Braghis's avatar
    Dan Braghis committed
            return super().dispatch(request, *args, **kwargs)
    
        def get_context_data(self, **kwargs):
    
            determination_messages = DeterminationMessageSettings.for_site(self.request.site)
    
    Dan Braghis's avatar
    Dan Braghis committed
            return super().get_context_data(
                submission=self.submission,
    
                message_templates=determination_messages.get_for_stage(self.submission.stage.name),
    
    Dan Braghis's avatar
    Dan Braghis committed
                **kwargs
            )
    
        def get_form_class(self):
            return get_form_for_stage(self.submission)
    
        def get_form_kwargs(self):
            kwargs = super().get_form_kwargs()
    
            kwargs['user'] = self.request.user
    
    Dan Braghis's avatar
    Dan Braghis committed
            kwargs['submission'] = self.submission
    
            kwargs['action'] = self.request.GET.get('action')
    
    Dan Braghis's avatar
    Dan Braghis committed
            return kwargs
    
        def get_success_url(self):
            return self.submission.get_absolute_url()
    
    
        def form_valid(self, form):
    
            response = super().form_valid(form)
    
            if not self.object.is_draft:
    
                messenger(
                    MESSAGES.DETERMINATION_OUTCOME,
                    request=self.request,
                    user=self.object.author,
                    submission=self.object.submission,
                )
    
                transition = transition_from_outcome(int(form.cleaned_data.get('outcome')), self.submission)
    
    
                if self.object.outcome == NEEDS_MORE_INFO:
                    # We keep a record of the message sent to the user in the comment
    
                        message=self.object.stripped_message,
    
                        user=self.request.user,
                        submission=self.submission,
                    )
    
    
                self.submission.perform_transition(transition, self.request.user, request=self.request)
    
            return self.progress_stage(self.submission) or response
    
        def progress_stage(self, instance):
    
                instance.perform_transition('draft_proposal', self.request.user, request=self.request)
    
            except PermissionDenied:
                pass
            else:
    
                messenger(
                    MESSAGES.INVITED_TO_PROPOSAL,
    
                    user=self.request.user,
                    submission=instance,
                )
    
                return HttpResponseRedirect(instance.get_absolute_url())
    
    
    @method_decorator(staff_required, name='dispatch')
    class AdminDeterminationDetailView(DetailView):
        model = Determination
    
        def get_object(self, queryset=None):
            return self.model.objects.get(submission=self.submission)
    
        def dispatch(self, request, *args, **kwargs):
            self.submission = get_object_or_404(ApplicationSubmission, id=self.kwargs['submission_pk'])
            determination = self.get_object()
    
    
            if can_edit_determination(request.user, determination, self.submission) and determination.is_draft:
    
                return HttpResponseRedirect(reverse_lazy('apply:submissions:determinations:form', args=(self.submission.id,)))
    
            return super().dispatch(request, *args, **kwargs)
    
    
    
    @method_decorator(login_required, name='dispatch')
    
    class ApplicantDeterminationDetailView(DetailView):
    
    Dan Braghis's avatar
    Dan Braghis committed
        model = Determination
    
    
    Dan Braghis's avatar
    Dan Braghis committed
        def get_object(self, queryset=None):
            return self.model.objects.get(submission=self.submission)
    
    
    Dan Braghis's avatar
    Dan Braghis committed
        def dispatch(self, request, *args, **kwargs):
    
    Dan Braghis's avatar
    Dan Braghis committed
            self.submission = get_object_or_404(ApplicationSubmission, id=self.kwargs['submission_pk'])
    
    Dan Braghis's avatar
    Dan Braghis committed
            determination = self.get_object()
    
    
            if request.user != self.submission.user:
    
    Dan Braghis's avatar
    Dan Braghis committed
                raise PermissionDenied
    
            if determination.is_draft:
    
                return HttpResponseRedirect(reverse_lazy('apply:submissions:determinations:detail', args=(self.submission.id,)))
    
    Dan Braghis's avatar
    Dan Braghis committed
    
            return super().dispatch(request, *args, **kwargs)
    
    
    
    class DeterminationDetailView(ViewDispatcher):
        admin_view = AdminDeterminationDetailView
        applicant_view = ApplicantDeterminationDetailView