From: Christian Heller Date: Mon, 20 Jan 2025 21:58:08 +0000 (+0100) Subject: Properly store Booking.amount rather than just an ellipsized string. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/%7B%7Bdb.prefix%7D%7D/calendar_export?a=commitdiff_plain;h=2b5025fa7b07f791b198b0bca76fca3589ec6e71;p=plomledger Properly store Booking.amount rather than just an ellipsized string. --- diff --git a/ledger.py b/ledger.py index 6085397..aa0619b 100755 --- a/ledger.py +++ b/ledger.py @@ -78,31 +78,37 @@ class IntroLine(BookingLine): self.error = 'not starting with properly formatted legal date' -class TransactionLine(BookingLine): +class TransferLine(BookingLine): """Non-first Booking line, expected to carry value movement.""" def __init__(self, booking_id: int, code: str) -> None: super().__init__(booking_id) - self.acc, self.amt, self.curr = '', '', '' + self.account, self.currency = '', '' + self.amount: Optional[Decimal] = None if not code[0].isspace(): self.error = 'non-intro line not indented' return toks = code.lstrip().split() - self.acc = toks[0] + self.account = toks[0] if len(toks) not in {1, 3}: self.error = 'illegal number of tokens' return if 3 == len(toks): + self.currency = toks[2] try: - amt_dec = Decimal(toks[1]) + self.amount = Decimal(toks[1]) except DecimalInvalidOperation: self.error = 'improper amount value' return - exp = amt_dec.as_tuple().exponent + + @property + def amount_short(self) -> str: + """If no .amount, '', else printed – but if too long, ellipsized.""" + if self.amount: + exp = self.amount.as_tuple().exponent assert isinstance(exp, int) - self.amt = (f'{amt_dec:.1f}…' if exp < -2 - else f'{amt_dec:.2f}') - self.curr = toks[2] + return f'{self.amount:.1f}…' if exp < -2 else f'{self.amount:.2f}' + return '' class Booking: @@ -115,7 +121,7 @@ class Booking: self.dat_lines[0].booking_line = IntroLine(self.id_, self.dat_lines[0].code) for dat_line in self.dat_lines[1:]: - dat_line.booking_line = TransactionLine(self.id_, dat_line.code) + dat_line.booking_line = TransferLine(self.id_, dat_line.code) class Handler(PlomHttpHandler): diff --git a/templates/_macros.tmpl b/templates/_macros.tmpl index 7ba713f..b3cb64a 100644 --- a/templates/_macros.tmpl +++ b/templates/_macros.tmpl @@ -28,9 +28,9 @@ td.curr { text-align: center; } {% if dat_line.is_intro %} {{dat_line.code}} {% elif not dat_line.error %} - {{dat_line.booking_line.amt}} - {{dat_line.booking_line.curr|truncate(4,true,"…")}} - {{dat_line.booking_line.acc}} + {{dat_line.booking_line.amount_short}} + {{dat_line.booking_line.currency|truncate(4,true,"…")}} + {{dat_line.booking_line.account}} {% else %} {{dat_line.code}} {% endif %}