home · contact · privacy
Properly store Booking.amount rather than just an ellipsized string.
authorChristian Heller <c.heller@plomlompom.de>
Mon, 20 Jan 2025 21:58:08 +0000 (22:58 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 20 Jan 2025 21:58:08 +0000 (22:58 +0100)
ledger.py
templates/_macros.tmpl

index 6085397393d9dbb835d434e108f941c1ff1c3669..aa0619b5e8fc4dad75fb74eb2b1865bfc3ba0378 100755 (executable)
--- 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):
index 7ba713f6356468bb88546ac3f89fb3548a086923..b3cb64a5b683aa94c2a7e0de77b41373b547c441 100644 (file)
@@ -28,9 +28,9 @@ td.curr { text-align: center; }
     {% if dat_line.is_intro %}
       <td class="code{% if dat_line.error %} invalid{% endif %}" colspan=3><a href="/booking/{{dat_line.booking_id}}">{{dat_line.code}}</a></td>
     {% elif not dat_line.error %}
-      <td class="amt">{{dat_line.booking_line.amt}}</td>
-      <td class="curr">{{dat_line.booking_line.curr|truncate(4,true,"…")}}</td>
-      <td>{{dat_line.booking_line.acc}}</td>
+      <td class="amt">{{dat_line.booking_line.amount_short}}</td>
+      <td class="curr">{{dat_line.booking_line.currency|truncate(4,true,"…")}}</td>
+      <td>{{dat_line.booking_line.account}}</td>
     {% else %}
       <td class="invalid" colspan=3>{{dat_line.code}}</td>
     {% endif %}