def show_list_to_review()

in invoice-processing-pipeline/reviewer/main.py [0:0]


def show_list_to_review():
    # Query the DB for all "Not Approved" invoices
    db = firestore.Client()
    colref = db.collection("invoices")
    query = colref.where("state", "==", "Not Approved")

    # Build data list to work with and then render in a template
    invoices = [rec.to_dict() for rec in query.stream()]

    # Will need signed URLs in web page so users can see the PDFs
    # Prepare storage client to create those
    gcs = storage.Client()
    bucket = gcs.get_bucket(BUCKET_NAME)

    # Will need credentials to generate signed URLs
    credentials, _ = auth.default()
    if credentials.token is None:
        credentials.refresh(requests.Request())

    # Update the data list with signed URLs
    for invoice in invoices:
        full_name = f"{PROCESSED_PREFIX}{invoice['blob_name']}"
        print(f"Blob full name is {full_name}")
        blob = bucket.get_blob(full_name)

        # Add the URLs to the list
        url = "None"    # Fallback that should never be needed

        if blob is not None:
            url = blob.generate_signed_url(
                version="v4", expiration=timedelta(hours=1), 
                service_account_email=credentials.service_account_email,
                access_token=credentials.token, method="get", scheme="https")
            print(f"url is {url}")

        invoice["url"] = url

    # Populate the template with the invoice data and return the page
    return render_template("list.html", invoices=invoices), 200