Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/fava/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,14 @@ def statement_path(self, entry_hash: str, metadata_key: str) -> str:
"""
entry = self.get_entry(entry_hash)
value = entry.meta.get(metadata_key, None)
if not isinstance(value, str):
for posting in getattr(entry, "postings", []):
if posting.meta is None:
continue
v = posting.meta.get(metadata_key, None)
if isinstance(v, str):
value = v
break
if not isinstance(value, str):
raise StatementMetadataInvalidError(metadata_key)

Expand Down
2 changes: 1 addition & 1 deletion src/fava/templates/_journal_table.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
{{- ', "{}"'.format(posting.cost.label) if posting.cost.label else '' }}</span>
{{ render_amount(ledger, posting.price) }}
</p>
{{ _render_metadata(posting.meta|meta_items) }}
{{ _render_metadata(posting.meta|meta_items, entry_hash) }}
</li>
{% endfor %}
</ul>
Expand Down
69 changes: 69 additions & 0 deletions tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,75 @@ def test_statement_download(
assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR


def test_statement_download_posting_metadata(
app: Flask, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Document metadata on a posting (not the transaction) can be served."""
path = Path(__file__)
date = datetime.date(2022, 1, 1)
txn = create.transaction(
{"filename": str(path), "lineno": 1},
date,
"*",
"payee",
"narration",
postings=[
create.posting(
"Assets:Cash",
create.amount("10 EUR"),
meta={"document": path.name},
)
],
)
txn_hash = hash_entry(txn)
entries = [create.document({}, date, "Assets", str(path)), txn]

with app.test_request_context("/long-example/"):
app.preprocess_request()
monkeypatch.setattr(g.ledger, "all_entries", entries)
monkeypatch.setattr(
g.ledger, "all_entries_by_type", group_entries_by_type(entries)
)
assert Path(g.ledger.statement_path(txn_hash, "document")) == path


def test_statement_download_posting_metadata_skips_non_string(
app: Flask, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Non-string posting metadata values are skipped; first string wins."""
path = Path(__file__)
date = datetime.date(2022, 1, 1)
txn = create.transaction(
{"filename": str(path), "lineno": 1},
date,
"*",
"payee",
"narration",
postings=[
create.posting(
"Assets:Cash",
create.amount("10 EUR"),
meta={"document": 42},
),
create.posting(
"Assets:Cash",
create.amount("10 EUR"),
meta={"document": path.name},
),
],
)
txn_hash = hash_entry(txn)
entries = [create.document({}, date, "Assets", str(path)), txn]

with app.test_request_context("/long-example/"):
app.preprocess_request()
monkeypatch.setattr(g.ledger, "all_entries", entries)
monkeypatch.setattr(
g.ledger, "all_entries_by_type", group_entries_by_type(entries)
)
assert Path(g.ledger.statement_path(txn_hash, "document")) == path


def test_incognito(test_data_dir: Path) -> None:
"""Numbers get obfuscated in incognito mode."""
app = create_app([test_data_dir / "example.beancount"], incognito=True)
Expand Down
Loading