def redact_attachment(request, slug, attachment_id): foirequest = get_object_or_404(FoiRequest, slug=slug) if not request.user.is_staff and not request.user == foirequest.user: return render_403(request) attachment = get_object_or_404(FoiAttachment, pk=int(attachment_id), belongs_to__request=foirequest) if not attachment.can_approve and not request.user.is_staff: return render_403(request) already = None if attachment.redacted: already = attachment.redacted elif attachment.is_redacted: already = attachment if already is not None and not already.can_approve and not request.user.is_staff: return render_403(request) if request.method == 'POST': path = convert_to_pdf(request.POST) if path is None: return render_400(request) name = attachment.name.rsplit('.', 1)[0] name = re.sub('[^\w\.\-]', '', name) pdf_file = File(open(path, 'rb')) if already: att = already else: att = FoiAttachment( belongs_to=attachment.belongs_to, name=_('%s_redacted.pdf') % name, is_redacted=True, filetype='application/pdf', approved=True, can_approve=True ) att.file = pdf_file att.size = pdf_file.size att.approve_and_save() if not attachment.is_redacted: attachment.redacted = att attachment.can_approve = False attachment.approved = False attachment.save() return redirect(att.get_anchor_url()) return render(request, 'foirequest/redact.html', { 'foirequest': foirequest, 'attachment': attachment })
def redact_attachment(request, slug, attachment_id): foirequest = get_object_or_404(FoiRequest, slug=slug) if not request.user.is_staff and not request.user == foirequest.user: return render_403(request) attachment = get_object_or_404(FoiAttachment, pk=int(attachment_id), belongs_to__request=foirequest) if not attachment.can_approve and not request.user.is_staff: return render_403(request) already = None if attachment.redacted: already = attachment.redacted elif attachment.is_redacted: already = attachment if already is not None and not already.can_approve and not request.user.is_staff: return render_403(request) if request.method == "POST": path = convert_to_pdf(request.POST) if path is None: return render_400(request) name = attachment.name.rsplit(".", 1)[0] name = re.sub("[^\w\.\-]", "", name) pdf_file = File(open(path, "rb")) if already: att = already else: att = FoiAttachment( belongs_to=attachment.belongs_to, name=_("%s_redacted.pdf") % name, is_redacted=True, filetype="application/pdf", approved=True, can_approve=True, ) att.file = pdf_file att.size = pdf_file.size att.approve() att.save() if not attachment.is_redacted: attachment.redacted = att attachment.can_approve = False attachment.approved = False attachment.save() return redirect(att.get_anchor_url()) return render(request, "foirequest/redact.html", {"foirequest": foirequest, "attachment": attachment})