def select_pdf_document(request, ctx, obj): """ Views to select pdf files to download. :type obj: :class:`.DocumentController` """ if request.GET.get("Download"): formset = get_pdf_formset(obj, request.GET) if formset.is_valid(): files = [] for form in formset.forms: selected = form.cleaned_data["selected"] if selected: df = form.cleaned_data["id"] files.append(df) return download_merged_pdf(obj, files) else: formset = get_pdf_formset(obj) ctx["pdf_formset"] = formset return r2r("select_pdf_doc.html", ctx, request)
def select_pdf_part(request, ctx, obj): """ View helper to select pdf files to download. :type obj: :class:`.PartController` """ data = request.GET if request.GET.get("Download") else None # retrieve all pdf files (from documents attached to children parts) children = obj.get_children(-1) formsets = [] self_link = FakeLink("self", child=obj.object) for level, link in [(0, self_link)] + list(children): link.formsets = [] for l in link.child.documentpartlink_part.now(): doc = l.document ctrl = get_controller(doc.type)(doc, request.user) if ctrl.check_readable(raise_=False): formset = get_pdf_formset(doc, data, prefix="pdf_%s_%d" % (link.id, doc.id)) link.formsets.append(formset) formsets.append((level, link)) if data: # returns a pdf file if all formsets are valid valid = True files = [] for level, link in formsets: for formset in link.formsets: if formset.is_valid(): for form in formset.forms: selected = form.cleaned_data["selected"] if selected: df = form.cleaned_data["id"] files.append(df) else: valid = False if valid: return download_merged_pdf(obj, files) ctx["children"] = formsets return r2r("select_pdf_part.html", ctx, request)