Пример #1
0
        def deferred(inforequest):
            branch = Branch(
                obligee=self.cleaned_data[u'obligee'],
                inforequest=inforequest,
            )
            branch.save()

            subject = self.fields[u'subject'].finalize(
                self.cleaned_data[u'subject'])
            content = self.fields[u'content'].finalize(
                self.cleaned_data[u'content'],
                dict(
                    unique_email=inforequest.unique_email,
                    obligee=self.cleaned_data[u'obligee'],
                ))
            action = Action(
                branch=branch,
                subject=subject,
                content=content,
                effective_date=inforequest.submission_date,
                type=Action.TYPES.REQUEST,
            )
            action.save()

            action.attachment_set = self.cleaned_data[u'attachments']
Пример #2
0
 def deferred(branch):
     action = Action(
             branch=branch,
             effective_date=self.cleaned_data[u'advanced_by'].effective_date,
             type=Action.TYPES.ADVANCED_REQUEST,
             )
     action.save()
Пример #3
0
def _add_smail(request, inforequest_pk, form_class):
    assert form_class.action_type in Action.OBLIGEE_ACTION_TYPES

    inforequest = (Inforequest.objects
            .not_closed()
            .owned_by(request.user)
            .prefetch_related(Inforequest.prefetch_branches(None, Branch.objects.select_related(u'historicalobligee')))
            .prefetch_related(Branch.prefetch_last_action(u'branches'))
            .get_or_404(pk=inforequest_pk)
            )

    if request.method != u'POST': # The user can save a draft even if he may not submit.
        if inforequest.has_undecided_emails:
            return HttpResponseNotFound()
        if not inforequest.can_add_action(form_class.action_type):
            return HttpResponseNotFound()

    draft = inforequest.actiondraft_set.filter(type=form_class.action_type).order_by_pk().first()
    session = Session.objects.get(session_key=request.session.session_key)
    attached_to = (session, draft) if draft else (session,)

    if request.method != u'POST':
        form = form_class(inforequest=inforequest, attached_to=attached_to)
        if draft:
            form.load_from_draft(draft)
        return render_form(request, form, inforequest=inforequest)

    button = clean_button(request.POST, [u'add', u'draft'])

    if button == u'draft':
        form = form_class(request.POST, inforequest=inforequest, attached_to=attached_to, draft=True)
        if not form.is_valid():
            return json_form(request, form, inforequest=inforequest)
        if not draft:
            draft = ActionDraft(inforequest=inforequest, type=form_class.action_type)
        form.save_to_draft(draft)
        draft.save()
        return json_draft()

    if button == u'add':
        form = form_class(request.POST, inforequest=inforequest, attached_to=attached_to)
        if not form.is_valid():
            return json_form(request, form, inforequest=inforequest)
        action = Action(type=form_class.action_type)
        form.save(action)
        action.save()
        if draft:
            draft.delete()
        # The inforequest was changed, we need to refetch it
        inforequest = Inforequest.objects.prefetch_detail().get(pk=inforequest.pk)
        return json_success(request, inforequest, action)

    return HttpResponseBadRequest()
Пример #4
0
def _decide_email(request, inforequest_pk, email_pk, form_class):
    assert form_class.action_type in Action.OBLIGEE_EMAIL_ACTION_TYPES

    inforequest = (Inforequest.objects
            .not_closed()
            .owned_by(request.user)
            .prefetch_related(Inforequest.prefetch_branches(None, Branch.objects.select_related(u'historicalobligee')))
            .prefetch_related(Branch.prefetch_last_action(u'branches'))
            .get_or_404(pk=inforequest_pk)
            )
    inforequestemail = (inforequest.inforequestemail_set
            .undecided()
            .oldest()
            .select_related(u'email')
            .get_or_404()
            )
    email = inforequestemail.email

    if email.pk != Message._meta.pk.to_python(email_pk):
        return HttpResponseNotFound()
    if not inforequest.can_add_action(form_class.action_type):
        return HttpResponseNotFound()

    if request.method != u'POST':
        form = form_class(inforequest=inforequest)
        return render_form(request, form, inforequest=inforequest, email=email)

    form = form_class(request.POST, inforequest=inforequest)
    if not form.is_valid():
        return json_form(request, form, inforequest=inforequest, email=email)

    action = Action(
            subject=email.subject,
            content=email.text,
            effective_date=local_date(email.processed),
            email=email,
            type=form_class.action_type,
            )
    form.save(action)
    action.save()

    for attch in email.attachments:
        attachment = attch.clone(action)
        attachment.save()

    inforequestemail.type = InforequestEmail.TYPES.OBLIGEE_ACTION
    inforequestemail.save(update_fields=[u'type'])

    # The inforequest was changed, we need to refetch it
    inforequest = Inforequest.objects.prefetch_detail().get(pk=inforequest.pk)
    return json_success(request, inforequest, action)
Пример #5
0
    def save_action(self):
        assert self.values[u'result'] == u'action'
        assert self.values[u'result_action'] in Action.OBLIGEE_ACTION_TYPES
        assert not self.email or self.values[
            u'result_action'] in Action.OBLIGEE_EMAIL_ACTION_TYPES
        assert self.values[u'result_branch'].can_add_action(
            self.values[u'result_action'])

        action = Action(type=self.values[u'result_action'])
        action.branch = self.values[u'result_branch']
        action.email = self.email if self.email else None
        action.subject = self.email.subject if self.email else u''
        action.content = self.email.text if self.email else u''
        action.effective_date = (local_date(self.email.processed) if self.email
                                 else self.values[u'result_effective_date'])
        action.file_number = self.values.get(u'result_file_number', u'')
        action.deadline = self.values.get(u'result_deadline', None)
        action.disclosure_level = self.values.get(u'result_disclosure_level',
                                                  None)
        action.refusal_reason = self.values.get(u'result_refusal_reason', None)
        action.save()

        if self.email:
            for attch in self.email.attachments:
                attachment = attch.clone(action)
                attachment.save()
        else:
            action.attachment_set = self.values[u'result_attachments']

        for obligee in self.values.get(u'result_advanced_to', []):
            if obligee:
                sub_branch = Branch(
                    obligee=obligee,
                    inforequest=action.branch.inforequest,
                    advanced_by=action,
                )
                sub_branch.save()

                sub_action = Action(
                    branch=sub_branch,
                    effective_date=action.effective_date,
                    type=Action.TYPES.ADVANCED_REQUEST,
                )
                sub_action.save()

        if self.email:
            self.inforequestemail.type = InforequestEmail.TYPES.OBLIGEE_ACTION
            self.inforequestemail.save(update_fields=[u'type'])

        return action
Пример #6
0
        def deferred(action):
            for field in self.ADVANCED_TO_FIELDS:
                obligee = self.cleaned_data[field]
                if obligee:
                    sub_branch = Branch(
                            obligee=obligee,
                            inforequest=action.branch.inforequest,
                            advanced_by=action,
                            )
                    sub_branch.save()

                    sub_action = Action(
                            branch=sub_branch,
                            effective_date=action.effective_date,
                            type=Action.TYPES.ADVANCED_REQUEST,
                            )
                    sub_action.save()
Пример #7
0
    def save(self, commit=True):
        assert self.is_valid()

        action = Action(
            branch=self.cleaned_data[u'branch'],
            email=self.instance.email,
            type=self.cleaned_data[u'type'],
            subject=self.cleaned_data[u'subject'],
            content=self.cleaned_data[u'content'],
            effective_date=self.cleaned_data[u'effective_date'],
            deadline=self.cleaned_data[u'deadline'],
            extension=self.cleaned_data[u'extension'],
            disclosure_level=self.cleaned_data[u'disclosure_level'],
            refusal_reason=self.cleaned_data[u'refusal_reason'],
        )

        @after_saved(action)
        def deferred(action):
            session_type = ContentType.objects.get_for_model(Session)
            for attachment in self.cleaned_data[u'attachments']:
                # We don't want to steal attachments owned by the email, so we clone them.
                if attachment.generic_type_id != session_type.pk:
                    attachment = attachment.clone(action)
                else:
                    attachment.generic_object = action
                attachment.save()

            for obligee in self.cleaned_data[u'obligee_set']:
                sub_branch = Branch(
                    inforequest=action.branch.inforequest,
                    obligee=obligee,
                    advanced_by=action,
                )
                sub_branch.save()

                sub_action = Action(
                    branch=sub_branch,
                    type=Action.TYPES.ADVANCED_REQUEST,
                    effective_date=action.effective_date,
                )
                sub_action.save()

        if commit:
            action.save()
        return action
Пример #8
0
    def save(self, commit=True):
        assert self.is_valid()

        action = Action(
                branch=self.cleaned_data[u'branch'],
                email=self.instance.email,
                type=self.cleaned_data[u'type'],
                subject=self.cleaned_data[u'subject'],
                content=self.cleaned_data[u'content'],
                effective_date=self.cleaned_data[u'effective_date'],
                deadline=self.cleaned_data[u'deadline'],
                extension=self.cleaned_data[u'extension'],
                disclosure_level=self.cleaned_data[u'disclosure_level'],
                refusal_reason=self.cleaned_data[u'refusal_reason'],
                )

        @after_saved(action)
        def deferred(action):
            session_type = ContentType.objects.get_for_model(Session)
            for attachment in self.cleaned_data[u'attachments']:
                # We don't want to steal attachments owned by the email, so we clone them.
                if attachment.generic_type_id != session_type.pk:
                    attachment = attachment.clone(action)
                else:
                    attachment.generic_object = action
                attachment.save()

            for obligee in self.cleaned_data[u'obligee_set']:
                sub_branch = Branch(
                        inforequest=action.branch.inforequest,
                        obligee=obligee,
                        advanced_by=action,
                        )
                sub_branch.save()

                sub_action = Action(
                        branch=sub_branch,
                        type=Action.TYPES.ADVANCED_REQUEST,
                        effective_date=action.effective_date,
                        )
                sub_action.save()

        if commit:
            action.save()
        return action
Пример #9
0
def new_action_appeal2(request, inforequest_pk, branch_pk):
    inforequest = Inforequest.objects.not_closed().owned_by(request.user).get_or_404(pk=inforequest_pk)
    branch = inforequest.branch_set.get_or_404(pk=branch_pk)

    if not branch.can_add_appeal:
        return HttpResponseNotFound()
    if inforequest.has_undecided_emails:
        return HttpResponseNotFound()

    wizard = AppealWizards.find_applicable(branch)

    if request.method != u"POST":
        wizard.start()
        return render_step(request, wizard.current_step)

    try:
        wizard.step(request.POST)
    except WizzardRollback as e:
        return json_step(request, e.step)

    button = clean_button(request.POST, [u"save", u"prev", u"next"])

    if button == u"save":
        wizard.commit()
        return json_step(request, wizard.current_step)

    if button == u"prev":
        return json_step(request, wizard.prev_step())

    if button == u"next":
        if not wizard.current_step.is_valid():
            return json_step(request, wizard.current_step)
        wizard.commit()
        if not wizard.current_step.is_last():
            return json_step(request, wizard.next_step())
        branch.add_expiration_if_expired()
        action = Action(type=Action.TYPES.APPEAL)
        wizard.save(action)
        action.save()
        # The inforequest was changed, we need to refetch it.
        inforequest = Inforequest.objects.prefetch_detail().get(pk=inforequest.pk)
        return json_success(request, inforequest, action)

    return HttpResponseBadRequest()
Пример #10
0
        def deferred(inforequest):
            branch = Branch(
                    inforequest=inforequest,
                    obligee=self.cleaned_data[u'obligee'],
                    )
            branch.save()

            action = Action(
                    branch=branch,
                    type=Action.TYPES.REQUEST,
                    subject=self.cleaned_data[u'subject'],
                    content=self.cleaned_data[u'content'],
                    effective_date=inforequest.submission_date,
                    )
            action.save()
            action.attachment_set = self.cleaned_data[u'attachments']

            if self.cleaned_data[u'send_email']:
                action.send_by_email()
Пример #11
0
        def deferred(inforequest):
            branch = Branch(
                inforequest=inforequest,
                obligee=self.cleaned_data[u'obligee'],
            )
            branch.save()

            action = Action(
                branch=branch,
                type=Action.TYPES.REQUEST,
                subject=self.cleaned_data[u'subject'],
                content=self.cleaned_data[u'content'],
                effective_date=inforequest.submission_date,
            )
            action.save()
            action.attachment_set = self.cleaned_data[u'attachments']

            if self.cleaned_data[u'send_email']:
                action.send_by_email()
Пример #12
0
        def deferred(action):
            action.attachment_set = self.cleaned_data[u'attachments']

            for obligee in self.cleaned_data[u'obligee_set']:
                sub_branch = Branch(
                        inforequest=action.branch.inforequest,
                        obligee=obligee,
                        advanced_by=action,
                        )
                sub_branch.save()

                sub_action = Action(
                        branch=sub_branch,
                        type=Action.TYPES.ADVANCED_REQUEST,
                        effective_date=action.effective_date,
                        )
                sub_action.save()

            if self.cleaned_data[u'send_email']:
                action.send_by_email()
Пример #13
0
        def deferred(action):
            action.attachment_set = self.cleaned_data[u'attachments']

            for obligee in self.cleaned_data[u'obligee_set']:
                sub_branch = Branch(
                    inforequest=action.branch.inforequest,
                    obligee=obligee,
                    advanced_by=action,
                )
                sub_branch.save()

                sub_action = Action(
                    branch=sub_branch,
                    type=Action.TYPES.ADVANCED_REQUEST,
                    effective_date=action.effective_date,
                )
                sub_action.save()

            if self.cleaned_data[u'send_email']:
                action.send_by_email()
Пример #14
0
    def save(self, commit=True):
        assert self.is_valid()

        action = Action(
                branch=self.cleaned_data[u'branch'],
                type=self.cleaned_data[u'type'],
                subject=self.cleaned_data[u'subject'],
                content=self.cleaned_data[u'content'],
                effective_date=self.cleaned_data[u'effective_date'],
                deadline=self.cleaned_data[u'deadline'],
                extension=self.cleaned_data[u'extension'],
                disclosure_level=self.cleaned_data[u'disclosure_level'],
                refusal_reason=self.cleaned_data[u'refusal_reason'],
                )

        @after_saved(action)
        def deferred(action):
            action.attachment_set = self.cleaned_data[u'attachments']

            for obligee in self.cleaned_data[u'obligee_set']:
                sub_branch = Branch(
                        inforequest=action.branch.inforequest,
                        obligee=obligee,
                        advanced_by=action,
                        )
                sub_branch.save()

                sub_action = Action(
                        branch=sub_branch,
                        type=Action.TYPES.ADVANCED_REQUEST,
                        effective_date=action.effective_date,
                        )
                sub_action.save()

            if self.cleaned_data[u'send_email']:
                action.send_by_email()

        if commit:
            action.save()
        return action
Пример #15
0
    def save(self, commit=True):
        assert self.is_valid()

        action = Action(
            branch=self.cleaned_data[u'branch'],
            type=self.cleaned_data[u'type'],
            subject=self.cleaned_data[u'subject'],
            content=self.cleaned_data[u'content'],
            effective_date=self.cleaned_data[u'effective_date'],
            deadline=self.cleaned_data[u'deadline'],
            extension=self.cleaned_data[u'extension'],
            disclosure_level=self.cleaned_data[u'disclosure_level'],
            refusal_reason=self.cleaned_data[u'refusal_reason'],
        )

        @after_saved(action)
        def deferred(action):
            action.attachment_set = self.cleaned_data[u'attachments']

            for obligee in self.cleaned_data[u'obligee_set']:
                sub_branch = Branch(
                    inforequest=action.branch.inforequest,
                    obligee=obligee,
                    advanced_by=action,
                )
                sub_branch.save()

                sub_action = Action(
                    branch=sub_branch,
                    type=Action.TYPES.ADVANCED_REQUEST,
                    effective_date=action.effective_date,
                )
                sub_action.save()

            if self.cleaned_data[u'send_email']:
                action.send_by_email()

        if commit:
            action.save()
        return action
Пример #16
0
        def deferred(action):
            session_type = ContentType.objects.get_for_model(Session)
            for attachment in self.cleaned_data[u'attachments']:
                # We don't want to steal attachments owned by the email, so we clone them.
                if attachment.generic_type_id != session_type.pk:
                    attachment = attachment.clone(action)
                else:
                    attachment.generic_object = action
                attachment.save()

            for obligee in self.cleaned_data[u'obligee_set']:
                sub_branch = Branch(
                    inforequest=action.branch.inforequest,
                    obligee=obligee,
                    advanced_by=action,
                )
                sub_branch.save()

                sub_action = Action(
                    branch=sub_branch,
                    type=Action.TYPES.ADVANCED_REQUEST,
                    effective_date=action.effective_date,
                )
                sub_action.save()
Пример #17
0
        def deferred(action):
            session_type = ContentType.objects.get_for_model(Session)
            for attachment in self.cleaned_data[u'attachments']:
                # We don't want to steal attachments owned by the email, so we clone them.
                if attachment.generic_type_id != session_type.pk:
                    attachment = attachment.clone(action)
                else:
                    attachment.generic_object = action
                attachment.save()

            for obligee in self.cleaned_data[u'obligee_set']:
                sub_branch = Branch(
                        inforequest=action.branch.inforequest,
                        obligee=obligee,
                        advanced_by=action,
                        )
                sub_branch.save()

                sub_action = Action(
                        branch=sub_branch,
                        type=Action.TYPES.ADVANCED_REQUEST,
                        effective_date=action.effective_date,
                        )
                sub_action.save()
Пример #18
0
def _new_action(request, inforequest_pk, form_class):
    assert form_class.action_type in Action.APPLICANT_ACTION_TYPES

    inforequest = (
        Inforequest.objects.not_closed()
        .owned_by(request.user)
        .prefetch_related(Inforequest.prefetch_branches(None, Branch.objects.select_related(u"historicalobligee")))
        .prefetch_related(Branch.prefetch_last_action(u"branches"))
        .get_or_404(pk=inforequest_pk)
    )

    if request.method != u"POST":  # The user can save a draft even if he may not submit.
        if inforequest.has_undecided_emails:
            return HttpResponseNotFound()
        if not inforequest.can_add_action(form_class.action_type):
            return HttpResponseNotFound()

    draft = inforequest.actiondraft_set.filter(type=form_class.action_type).order_by_pk().first()
    session = Session.objects.get(session_key=request.session.session_key)
    attached_to = (session, draft) if draft else (session,)

    if request.method != u"POST":
        form = form_class(inforequest=inforequest, attached_to=attached_to)
        if draft:
            form.load_from_draft(draft)
        return render_form(request, form, inforequest=inforequest)

    if form_class.action_type in Action.APPLICANT_EMAIL_ACTION_TYPES:
        button = clean_button(request.POST, [u"email", u"print", u"draft"])
    else:
        button = clean_button(request.POST, [u"print", u"draft"])

    if button == u"draft":
        form = form_class(request.POST, inforequest=inforequest, attached_to=attached_to, draft=True)
        if not form.is_valid():
            return json_form(request, form, inforequest=inforequest)
        if not draft:
            draft = ActionDraft(inforequest=inforequest, type=form_class.action_type)
        form.save_to_draft(draft)
        draft.save()
        return json_draft()

    if button in [u"email", u"print"]:
        form = form_class(request.POST, inforequest=inforequest, attached_to=attached_to)
        if not form.is_valid():
            return json_form(request, form, inforequest=inforequest)
        if form_class.action_type == Action.TYPES.APPEAL:
            form.cleaned_data[u"branch"].add_expiration_if_expired()
        action = Action(effective_date=local_today(), type=form_class.action_type)
        form.save(action)
        action.save()
        if draft:
            draft.delete()
        # The inforequest was changed, we need to refetch it.
        inforequest = Inforequest.objects.prefetch_detail().get(pk=inforequest.pk)
        action.branch = inforequest.branch_by_pk(action.branch_id)
        if button == u"email":
            action.send_by_email()
        return json_success(request, inforequest, action, button == u"print")

    return HttpResponseBadRequest()