PREFIX_LEDGER = 'ledger_'
PREFIX_EDIT = 'edit_'
+PREFIX_FILE = 'file_'
TOK_STRUCT = 'structured'
TOK_RAW = 'raw'
EDIT_STRUCT = f'{PREFIX_EDIT}{TOK_STRUCT}'
def do_POST(self) -> None:
# pylint: disable=invalid-name,missing-function-docstring
- prefix_file = 'file_'
- if (file_prefixed := self.postvars.keys_prefixed(prefix_file)):
- getattr(self.server, file_prefixed[0][len(prefix_file):])()
+ redir_target = Path(self.path)
+ if (file_prefixed := self.postvars.keys_prefixed(PREFIX_FILE)):
+ self.post_file_action(file_prefixed[0])
elif (self.pagename.startswith(PREFIX_EDIT)
and self.postvars.first('apply')):
- booking = self.server.bookings[int(self.path_toks[2])]
- new_lines = []
- if self.pagename == EDIT_STRUCT:
- line_keys = self.postvars.keys_prefixed('line_')
- lineno_to_inputs: dict[int, list[str]] = {}
- for key in line_keys:
- toks = key.split('_', maxsplit=2)
- lineno = int(toks[1])
- if lineno not in lineno_to_inputs:
- lineno_to_inputs[lineno] = []
- lineno_to_inputs[lineno] += [toks[2]]
- indent = ' '
- for lineno, input_names in lineno_to_inputs.items():
- data = ''
- comment = self.postvars.first(f'line_{lineno}_comment')
- for name in input_names:
- input_ = self.postvars.first(f'line_{lineno}_{name}'
- ).strip()
- if name == 'date':
- data = input_
- elif name == 'target':
- data += f' {input_}'
- elif name == 'error':
- data = f'{indent}{input_}'
- elif name == 'account':
- data = f'{indent}{input_}'
- elif name in {'amount', 'currency'}:
- data += f' {input_}'
- new_lines += [
- DatLine(f'{data} ; {comment}' if comment else data)]
- else: # edit_raw
- new_lines += [DatLine(line) for line
- in self.postvars.first('booking').splitlines()]
- new_id = self.server.rewrite_booking(booking.id_, new_lines)
- self.redirect(Path('/bookings').joinpath(f'{new_id}'))
- return
+ redir_target = self.post_edit()
elif self.pagename.startswith(PREFIX_LEDGER):
- action, id_str, dir_ =\
- self.postvars.keys_prefixed(PREFIX_LEDGER)[0].split('_')[1:]
- new_id = getattr(self.server, f'{action}_booking')(
- int(id_str), dir_ == 'up' if action == 'move' else 'to_end')
- self.redirect(Path(self.path).joinpath(f'#{new_id}'))
- return
- self.redirect(Path(self.path))
+ redir_target = self.post_ledger_action()
+ self.redirect(redir_target)
+
+ def post_file_action(self, file_prefixed: str) -> None:
+ """Based on file_prefixed name, trigger .server.load or .save."""
+ if file_prefixed == f'{PREFIX_FILE}load':
+ self.server.load()
+ elif file_prefixed == f'{PREFIX_FILE}save':
+ self.server.save()
+
+ def post_edit(self) -> Path:
+ """Based on postvars, edit targeted Booking."""
+ booking = self.server.bookings[int(self.path_toks[2])]
+ new_lines = []
+ if self.pagename == EDIT_STRUCT:
+ line_keys = self.postvars.keys_prefixed('line_')
+ lineno_to_inputs: dict[int, list[str]] = {}
+ for key in line_keys:
+ toks = key.split('_', maxsplit=2)
+ lineno = int(toks[1])
+ if lineno not in lineno_to_inputs:
+ lineno_to_inputs[lineno] = []
+ lineno_to_inputs[lineno] += [toks[2]]
+ indent = ' '
+ for lineno, input_names in lineno_to_inputs.items():
+ data = ''
+ comment = self.postvars.first(f'line_{lineno}_comment')
+ for name in input_names:
+ input_ = self.postvars.first(f'line_{lineno}_{name}'
+ ).strip()
+ if name == 'date':
+ data = input_
+ elif name == 'target':
+ data += f' {input_}'
+ elif name == 'error':
+ data = f'{indent}{input_}'
+ elif name == 'account':
+ data = f'{indent}{input_}'
+ elif name in {'amount', 'currency'}:
+ data += f' {input_}'
+ new_lines += [
+ DatLine(f'{data} ; {comment}' if comment else data)]
+ else: # edit_raw
+ new_lines += [DatLine(line) for line
+ in self.postvars.first('booking').splitlines()]
+ new_id = self.server.rewrite_booking(booking.id_, new_lines)
+ return Path('/bookings').joinpath(f'{new_id}')
+
+ def post_ledger_action(self) -> Path:
+ """Based on trigger postvar call .server.(move|copy)_booking."""
+ keys_prefixed = self.postvars.keys_prefixed(PREFIX_LEDGER)
+ action, id_str, dir_ = keys_prefixed[0].split('_', maxsplit=3)[1:]
+ id_ = int(id_str)
+ if action == 'move':
+ id_ = self.server.move_booking(id_, dir_ == 'up')
+ elif action == 'copy':
+ id_ = self.server.copy_booking(id_, dir_ == 'to_end')
+ return Path(self.path).joinpath(f'#{id}')
def do_GET(self) -> None:
# pylint: disable=invalid-name,missing-function-docstring
- ctx = {'tainted': self.server.tainted, 'path': self.path}
if self.pagename == 'bookings':
self.redirect(
Path('/').joinpath(EDIT_STRUCT).joinpath(self.path_toks[2]))
- elif self.pagename == 'balance':
+ return
+ ctx = {'tainted': self.server.tainted, 'path': self.path}
+ if self.pagename == 'balance':
self.get_balance(ctx)
elif self.pagename.startswith(PREFIX_EDIT):
self.get_edit(ctx, self.pagename == EDIT_RAW)
def get_balance(self, ctx) -> None:
"""Display tree of calculated Accounts over .bookings[:up_incl+1]."""
id_ = int(self.params.first('up_incl') or '-1')
- valid = True
- account_names = set()
+ valid, account_names = True, set()
to_balance = (self.server.bookings[:id_ + 1] if id_ >= 0
else self.server.bookings)
for booking in to_balance:
intro_comment = copied.booked_lines[0].comment
intro = DatLine(
f'{dt_date.today().isoformat()} {copied.target}'
- + ' ; {intro_comment}' if intro_comment else '')
+ + (f' ; {intro_comment}' if intro_comment else ''))
new_booking = Booking(new_id,
- [intro] + copied.booked_lines_copied[:1],
+ [intro] + copied.booked_lines_copied[1:],
copied.gap_lines_copied)
self.bookings += [new_booking]
else: