def save(self, commit=True): assert self.is_valid() inforequest = Inforequest(applicant=self.cleaned_data[u'applicant'], ) @after_saved(inforequest) 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() if commit: inforequest.save() return inforequest
def inforequest_index(request): inforequests = (Inforequest.objects .not_closed() .owned_by(request.user) .order_by_submission_date() .select_undecided_emails_count() .prefetch_related(Inforequest.prefetch_main_branch(None, Branch.objects.select_related(u'historicalobligee'))) ) drafts = (InforequestDraft.objects .owned_by(request.user) .order_by_pk() .select_related(u'obligee') ) closed_inforequests = (Inforequest.objects .closed() .owned_by(request.user) .order_by_submission_date() .prefetch_related(Inforequest.prefetch_main_branch(None, Branch.objects.select_related(u'historicalobligee'))) ) return render(request, u'inforequests/index.html', { u'inforequests': inforequests, u'drafts': drafts, u'closed_inforequests': closed_inforequests, })
def inforequest_index(request): inforequests = (Inforequest.objects .not_closed() .owned_by(request.user) .order_by_submission_date() .select_undecided_emails_count() .prefetch_related( Inforequest.prefetch_main_branch(None, Branch.objects.select_related(u'historicalobligee'))) ) drafts = (InforequestDraft.objects .owned_by(request.user) .order_by_pk() .select_related(u'obligee') ) closed_inforequests = (Inforequest.objects .closed() .owned_by(request.user) .order_by_submission_date() .prefetch_related( Inforequest.prefetch_main_branch(None, Branch.objects.select_related(u'historicalobligee'))) ) return render(request, u'inforequests/index/index.html', { u'inforequests': inforequests, u'drafts': drafts, u'closed_inforequests': closed_inforequests, })
def save(self, commit=True): assert self.is_valid() inforequest = Inforequest( applicant=self.cleaned_data[u'applicant'], ) @after_saved(inforequest) 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() if commit: inforequest.save() return inforequest
def obligee_action_dispatcher(request): inforequests = (Inforequest.objects .not_closed() .owned_by(request.user) .order_by_submission_date() .select_undecided_emails_count() .prefetch_related( Inforequest.prefetch_main_branch(None, Branch.objects.select_related(u'historicalobligee'))) ) # If the user has an inforequest with a new email, continue with it. If there is no new email # and the user has only one pending inforequest, continue with it. If the user has no pending # inforequests, return to inforequest index. Finally, if the user has at least two pending # inforequests, let him choose with which to continue. for inforequest in inforequests: if inforequest.has_undecided_emails: return HttpResponseRedirect( reverse(u'inforequests:obligee_action', kwargs=dict(inforequest=inforequest))) if len(inforequests) == 1: return HttpResponseRedirect( reverse(u'inforequests:obligee_action', kwargs=dict(inforequest=inforequests[0]))) if len(inforequests) == 0: return HttpResponseRedirect(reverse(u'inforequests:index')) return render(request, u'inforequests/obligee_action_dispatcher/dispatcher.html', { u'inforequests': inforequests, })
def get_queryset(self, request): queryset = super(InforequestAdmin, self).get_queryset(request) queryset = queryset.select_related(u'applicant') queryset = queryset.select_undecided_emails_count() queryset = queryset.prefetch_related( Inforequest.prefetch_main_branch( None, Branch.objects.select_related(u'obligee'))) return queryset
def inforequest_create(request, draft_pk=None): template = u'inforequests/create/create.html' draft = InforequestDraft.objects.owned_by(request.user).get_or_404( pk=draft_pk) if draft_pk else None session = Session.objects.get(session_key=request.session.session_key) attached_to = (session, draft) if draft else (session, ) if request.method != u'POST': form = InforequestForm(attached_to=attached_to, user=request.user) if draft: form.load_from_draft(draft) return render(request, template, dict(form=form)) button = clean_button(request.POST, [u'submit', u'draft']) if button == u'draft': form = InforequestForm(request.POST, draft=True, attached_to=attached_to, user=request.user) if not form.is_valid(): return render(request, template, dict(form=form)) if not draft: draft = InforequestDraft(applicant=request.user) form.save_to_draft(draft) draft.save() return HttpResponseRedirect(reverse(u'inforequests:index')) if button == u'submit': form = InforequestForm(request.POST, attached_to=attached_to, user=request.user) if not form.is_valid(): return render(request, template, dict(form=form)) inforequest = Inforequest(applicant=request.user) form.save(inforequest) inforequest.save() inforequest.main_branch.last_action.send_by_email() if draft: draft.delete() return HttpResponseRedirect(inforequest.get_absolute_url()) return HttpResponseBadRequest()
def inforequest_index(request): inforequests = ( Inforequest.objects.not_closed() .owned_by(request.user) .order_by_submission_date() .select_undecided_emails_count() .prefetch_related(Inforequest.prefetch_main_branch(None, Branch.objects.select_related(u"historicalobligee"))) ) drafts = InforequestDraft.objects.owned_by(request.user).order_by_pk().select_related(u"obligee") closed_inforequests = ( Inforequest.objects.closed() .owned_by(request.user) .order_by_submission_date() .prefetch_related(Inforequest.prefetch_main_branch(None, Branch.objects.select_related(u"historicalobligee"))) ) return render( request, u"inforequests/index/index.html", {u"inforequests": inforequests, u"drafts": drafts, u"closed_inforequests": closed_inforequests}, )
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()
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)
def inforequest_create(request, draft_pk=None): template = u'inforequests/create.html' draft = InforequestDraft.objects.owned_by(request.user).get_or_404(pk=draft_pk) if draft_pk else None session = Session.objects.get(session_key=request.session.session_key) attached_to = (session, draft) if draft else (session,) if request.method != u'POST': form = InforequestForm(attached_to=attached_to, user=request.user) if draft: form.load_from_draft(draft) return render(request, template, dict(form=form)) button = clean_button(request.POST, [u'submit', u'draft']) if button == u'draft': form = InforequestForm(request.POST, draft=True, attached_to=attached_to, user=request.user) if not form.is_valid(): return render(request, template, dict(form=form)) if not draft: draft = InforequestDraft(applicant=request.user) form.save_to_draft(draft) draft.save() return HttpResponseRedirect(reverse(u'inforequests:index')) if button == u'submit': form = InforequestForm(request.POST, attached_to=attached_to, user=request.user) if not form.is_valid(): return render(request, template, dict(form=form)) inforequest = Inforequest(applicant=request.user) form.save(inforequest) inforequest.save() inforequest.main_branch.last_action.send_by_email() if draft: draft.delete() return HttpResponseRedirect(reverse(u'inforequests:detail', args=(inforequest.pk,))) return HttpResponseBadRequest()
def save(self): assert self.is_valid() subject_finalize = lambda inforequest: self.fields[u'subject'].finalize( self.cleaned_data[u'subject']) content_finalize = lambda inforequest: self.fields[u'content'].finalize( self.cleaned_data[u'content'], dict( unique_email=inforequest.unique_email, obligee=self.cleaned_data[u'obligee'], )) inforequest = Inforequest.create( applicant=self.user, subject=self.cleaned_data[u'subject'][0], content=self.cleaned_data[u'content'][0], obligee=self.cleaned_data[u'obligee'], subject_finalize=subject_finalize, content_finalize=content_finalize, attachments=self.cleaned_data[u'attachments'], ) return inforequest
def save(self): assert self.is_valid() subject_finalize = lambda inforequest: self.fields[ u'subject'].finalize(self.cleaned_data[u'subject']) content_finalize = lambda inforequest: self.fields[ u'content'].finalize( self.cleaned_data[u'content'], dict( unique_email=inforequest.unique_email, obligee=self.cleaned_data[u'obligee'], )) inforequest = Inforequest.create( applicant=self.user, subject=self.cleaned_data[u'subject'][0], content=self.cleaned_data[u'content'][0], obligee=self.cleaned_data[u'obligee'], subject_finalize=subject_finalize, content_finalize=content_finalize, attachments=self.cleaned_data[u'attachments'], ) return inforequest
def get_queryset(self, request): queryset = super(UserAdminMixinInforequestInline, self).get_queryset(request) queryset = queryset.select_undecided_emails_count() queryset = queryset.prefetch_related(Inforequest.prefetch_main_branch(None, Branch.objects.select_related(u'obligee'))) return queryset
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()