redir_path = Path('/')
if self.pagename.startswith('edit_'):
id_ = int(self.path_toks[2])
- redir_path = Path('/').joinpath('booking').joinpath(str(id_))
+ redir_path = Path('/').joinpath(self.pagename).joinpath(str(id_))
if self.pagename == 'file':
if 'reload' in self.postvars.as_dict:
self.server.load()
elif 'save' in self.postvars.as_dict:
self.server.save()
elif self.pagename == 'edit_structured':
- 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]]
- new_dat_lines = []
- 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}')
- 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_dat_lines += [
+ if self.postvars.first('apply'):
+ 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]]
+ new_dat_lines = []
+ 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}')
+ 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_dat_lines += [
DatLine(f'{data} ; {comment}' if comment else data)]
- self.server.rewrite_booking(id_, new_dat_lines)
+ self.server.rewrite_booking(id_, new_dat_lines)
elif self.pagename == 'edit_raw':
- new_dat_lines = [DatLine(line) for line
- in self.postvars.first('booking').splitlines()]
- self.server.rewrite_booking(id_, new_dat_lines)
+ if self.postvars.first('apply'):
+ new_dat_lines = [
+ DatLine(line) for line
+ in self.postvars.first('booking').splitlines()]
+ self.server.rewrite_booking(id_, new_dat_lines)
self.redirect(redir_path)
def do_GET(self) -> None:
{% endfor %}
</table>
{% endmacro %}
+
+{% macro taint_js() %}
+function taint() {
+ const els = document.getElementsByClassName("enable_on_change");
+ for (let i = 0; i < els.length; i++) {
+ els[i].disabled = false;
+ }
+ const a = document.getElementById("switch_link");
+ const link_text = a.textContent;
+ const span = a.parentNode;
+ span.innerHTML = "";
+ const del = document.createElement("del");
+ span.appendChild(del);
+ del.textContent = link_text;
+}
+{% endmacro %}
+
+{% macro edit_bar(target, id) %}
+<input class="enable_on_change" type="submit" name="apply" value="apply" disabled />
+<input class="enable_on_change" type="submit" name="revert" value="revert" disabled />
+<span><a id="switch_link" href="/edit_{{target}}/{{id}}">switch to {{target}}</a></span>
+<hr />
+{% endmacro %}
{% block script %}
var dat_lines = {{dat_lines|tojson|safe}};
+{{ macros.taint_js() }}
+
function update_form() {
const table = document.getElementById("dat_lines");
table.innerHTML = "";
btn.textContent = text;
btn.type = "button"; // otherwise will act as form submit
btn.disabled = disable;
- btn.onclick = function() {f(); update_form();};
+ btn.onclick = function() {f(); update_form(); taint(); };
}
function add_td(tr, colspan=1) {
const td = document.createElement("td");
td.appendChild(input);
input.name = `line_${i}_${name}`
input.value = value.trim();
+ input.onchange = taint;
if (dat_line.error) {
td.classList.add("invalid");
}
{% block content %}
-<a href="/edit_raw/{{id}}">edit raw</a>
-<hr />
<form action="/edit_structured/{{id}}" method="POST">
+{{ macros.edit_bar("raw", id) }}
<table id="dat_lines">
</table>
-<input type="submit" value="update" />
</form>
{% endblock %}