Exemple #1
0
    def form_valid(self, form):
        if self.submission.is_determination_form_attached:
            form.instance.form_fields = self.get_defined_fields()

        super().form_valid(form)
        if self.object.is_draft:
            return HttpResponseRedirect(self.submission.get_absolute_url())

        with transaction.atomic():
            messenger(
                MESSAGES.DETERMINATION_OUTCOME,
                request=self.request,
                user=self.object.author,
                submission=self.object.submission,
                related=self.object,
            )
            proposal_form = form.cleaned_data.get('proposal_form')
            transition = transition_from_outcome(int(self.object.outcome),
                                                 self.submission)

            if self.object.outcome == NEEDS_MORE_INFO:
                # We keep a record of the message sent to the user in the comment
                Activity.comments.create(
                    message=self.object.stripped_message,
                    timestamp=timezone.now(),
                    user=self.request.user,
                    source=self.submission,
                    related_object=self.object,
                )
            self.submission.perform_transition(
                transition,
                self.request.user,
                request=self.request,
                notify=False,
                proposal_form=proposal_form,
            )

            if self.submission.accepted_for_funding and settings.PROJECTS_AUTO_CREATE:
                Project.create_from_submission(self.submission)

        messenger(
            MESSAGES.DETERMINATION_OUTCOME,
            request=self.request,
            user=self.object.author,
            source=self.object.submission,
            related=self.object,
        )

        return HttpResponseRedirect(self.submission.get_absolute_url())
Exemple #2
0
    def create(self, request, *args, **kwargs):
        """
        Create a determination on a submission.

        Accept a post data in form of `{field_id: value}`.
        `field_id` is same id which you get from the `/fields` api.
        `value` should be submitted with html tags, so that response can
        be displayed with correct formatting, e.g. in case of rich text field,
        we need to show the data with same formatting user has submitted.

        Accepts optional parameter `is_draft` when a determination is to be saved as draft.

        Raise ValidationError if a determination is already submitted by the user.
        """
        submission = self.get_submission_object()
        ser = self.get_serializer(data=request.data)
        ser.is_valid(raise_exception=True)
        if has_final_determination(submission):
            return ValidationError({
                'detail':
                'A final determination has already been submitted.'
            })
        determination = self.get_draft_determination()
        if determination is None:
            determination = Determination.objects.create(submission=submission,
                                                         author=request.user)
        determination.form_fields = self.get_defined_fields()
        determination.save()
        ser.update(determination, ser.validated_data)
        if determination.is_draft:
            ser = self.get_serializer(
                self.get_determination_data(determination))
            return Response(ser.data, status=status.HTTP_201_CREATED)
        with transaction.atomic():
            messenger(
                MESSAGES.DETERMINATION_OUTCOME,
                request=self.request,
                user=determination.author,
                submission=submission,
                related=determination,
            )
            proposal_form = ser.validated_data.get('proposal_form')
            transition = transition_from_outcome(int(determination.outcome),
                                                 submission)

            if determination.outcome == NEEDS_MORE_INFO:
                # We keep a record of the message sent to the user in the comment
                Activity.comments.create(
                    message=determination.stripped_message,
                    timestamp=timezone.now(),
                    user=self.request.user,
                    source=submission,
                    related_object=determination,
                )
            submission.perform_transition(
                transition,
                self.request.user,
                request=self.request,
                notify=False,
                proposal_form=proposal_form,
            )

            if submission.accepted_for_funding and settings.PROJECTS_AUTO_CREATE:
                Project.create_from_submission(submission)

        messenger(
            MESSAGES.DETERMINATION_OUTCOME,
            request=self.request,
            user=determination.author,
            source=submission,
            related=determination,
        )
        ser = self.get_serializer(self.get_determination_data(determination))
        return Response(ser.data, status=status.HTTP_201_CREATED)