From: Christian Heller Date: Sun, 26 Jan 2025 21:39:19 +0000 (+0100) Subject: To structured editing view, add addition/deletion/movement of lines. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/static/do_tasks?a=commitdiff_plain;h=1f8e6c2d5ca7214e40fdfa3c4d428b9916a9c633;p=plomledger To structured editing view, add addition/deletion/movement of lines. --- diff --git a/ledger.py b/ledger.py index 364ca1f..a3bc7af 100755 --- a/ledger.py +++ b/ledger.py @@ -79,6 +79,14 @@ class DatLine: self.code = halves[0] self.booking_line: Optional[BookingLine] = None + @property + def as_dict(self) -> dict: + """Return as JSON-ready dict.""" + assert isinstance(self.booking_line, (IntroLine, TransferLine)) + return {'comment': self.comment, 'code': self.code, + 'is_intro': self.is_intro, 'error': self.error, + 'booking_line': self.booking_line.as_dict} + @property def is_intro(self) -> bool: """Return if intro line of a Booking.""" @@ -144,20 +152,24 @@ class IntroLine(BookingLine): else: self.date = toks[0] + @property + def as_dict(self) -> dict: + """Return as JSON-ready dict.""" + return {'date': self.date, 'target': self.target} + class TransferLine(BookingLine): """Non-first Booking line, expected to carry value movement.""" def __init__(self, booking: 'Booking', code: str) -> None: super().__init__(booking) + self.currency = '' if not code[0].isspace(): self.errors += ['transfer line not indented'] toks = code.lstrip().split() self.account = toks[0] self.amount: Optional[Decimal] = None - if 1 == len(toks): - self.currency = '' - elif 3 <= len(toks): + if 3 <= len(toks): self.currency = toks[2] try: self.amount = Decimal(toks[1]) @@ -175,6 +187,12 @@ class TransferLine(BookingLine): return f'{self.amount:.1f}…' if exp < -2 else f'{self.amount:.2f}' return '' + @property + def as_dict(self) -> dict: + """Return as JSON-ready dict.""" + return {'account': self.account, 'currency': self.currency, + 'amount': str(self.amount)} + class Booking: """Represents lines of individual booking.""" @@ -273,15 +291,17 @@ class Handler(PlomHttpHandler): ctx = {'tainted': self.server.tainted} if self.pagename == 'booking' or self.pagename.startswith('edit_'): ctx['id'] = int(self.path_toks[2]) - ctx['dat_lines'] = self.server.bookings[ctx['id']].dat_lines if self.pagename == 'balance': valid, balance_roots = self.server.balance_roots( int(self.params.first('cutoff') or '0')) self.send_rendered(Path('balance.tmpl'), ctx | {'roots': balance_roots, 'valid': valid}) elif self.pagename in {'booking', 'edit_structured'}: + ctx['dat_lines'] = [dl.as_dict for dl + in self.server.bookings[ctx['id']].dat_lines] self.send_rendered(Path('edit_structured.tmpl'), ctx) elif self.pagename == 'edit_raw': + ctx['dat_lines'] = self.server.bookings[ctx['id']].dat_lines self.send_rendered(Path('edit_raw.tmpl'), ctx) elif self.pagename == 'ledger_raw': self.send_rendered(Path('ledger_raw.tmpl'), diff --git a/templates/_base.tmpl b/templates/_base.tmpl index d6881d2..c90f1ab 100644 --- a/templates/_base.tmpl +++ b/templates/_base.tmpl @@ -3,6 +3,9 @@ +