def parse_lines(lines, validate_bookings=True):
- import datetime
inside_booking = False
date_string, description = None, None
booking_lines = []
f.write(text);
os.remove(self.lock_file)
- def replace(self, start, end, lines):
- total_lines = self.real_lines[:start] + lines + self.real_lines[end:]
+ def insert_at_date(self, lines, date):
+ start_at = len(self.real_lines)
+ for b in self.bookings:
+ if b.date_string == date:
+ start_at = b.start_line
+ break
+ elif b.date_string > date:
+ break
+ if start_at == len(self.real_lines):
+ lines = [''] + lines
+ return self.write_lines_in_total_lines_at(self.real_lines, start_at, lines)
+
+ def update(self, start, end, lines, date):
+ total_lines = self.real_lines[:start] + self.real_lines[end:]
+ n_original_lines = end - start
+ start_at = len(total_lines)
+ for b in self.bookings:
+ if b.date_string == date:
+ if start_at == len(total_lines) or b.start_line == start:
+ start_at = b.start_line
+ if b.start_line > start:
+ start_at -= n_original_lines
+ elif b.date_string > date:
+ break
+ if start_at == len(total_lines):
+ lines = [''] + lines
+ return self.write_lines_in_total_lines_at(total_lines, start_at, lines)
+
+ def write_lines_in_total_lines_at(self, total_lines, start_at, lines):
+ total_lines = total_lines[:start_at] + lines + [''] + total_lines[start_at:]
+ _, _ = parse_lines(lines)
text = '\n'.join(total_lines)
self.write_db(text)
-
- def append(self, lines):
- text = '\n\n' + '\n'.join(lines) + '\n\n'
- self.write_db(text, 'a')
+ return start_at
def get_nth_for_booking_of_start_line(self, start_line):
nth = 0
for b in self.bookings:
- if b.start_line == start_line:
+ if b.start_line >= start_line:
break
nth += 1
return nth
/ <a href="/add_free?start={{start}}&end={{end}}">free</a>
| copy:<a href="/copy_structured?start={{start}}&end={{end}}">structured</a>
/ <a href="/copy_free?start={{start}}&end={{end}}">free</a>
+| move {% if move_up %}<a href="/move_up?start={{start}}&end={{end}}">up</a>{% else %}up{% endif %}/{% if move_down %}<a href="/move_down?start={{start}}&end={{end}}">down</a>{% else %}down{% endif %}
| <a href="/balance?stop={{nth+1}}">balance after</a>
]</span>
<table>
_, _ = parse_lines(lines)
# if saving, process where to and where to redirect after
if 'save' in postvars.keys():
+ last_date = str(datetime.datetime.now())[:10]
+ if len(db.bookings) > 0:
+ last_date = db.bookings[-1].date_string
+ target_date = last_date[:]
+ first_line_tokens = lines[0].split() if len(lines) > 0 else ''
+ first_token = first_line_tokens[0] if len(first_line_tokens) > 0 else ''
+ try:
+ datetime.datetime.strptime(first_token, '%Y-%m-%d')
+ target_date = first_token
+ except ValueError:
+ pass
if start == end == 0:
- db.append(lines)
- redir_url = f'/#last'
+ start = db.insert_at_date(lines, target_date)
+ nth = db.get_nth_for_booking_of_start_line(start)
else:
- db.replace(start, end, lines)
- nth = db.get_nth_for_booking_of_start_line(start)
- redir_url = f'/#{nth}'
- self.send_code_and_headers(301, [('Location', redir_url)])
+ new_start = db.update(start, end, lines, target_date)
+ nth = db.get_nth_for_booking_of_start_line(new_start)
+ if new_start > start:
+ nth -= 1
+ redir_url = f'/#{nth}'
+ self.send_code_and_headers(302, [('Location', redir_url)])
# otherwise just re-build editing form
else:
if '/add_structured' == parsed_url.path:
page += self.add_free(db, start, end, copy=True)
elif parsed_url.path == '/copy_structured':
page += self.add_structured(db, start, end, copy=True)
+ elif parsed_url.path == '/move_up':
+ nth = self.move_up(db, start, end)
+ self.send_code_and_headers(302, [('Location', f'/#{nth}')])
+ return
+ elif parsed_url.path == '/move_down':
+ nth = self.move_down(db, start, end)
+ self.send_code_and_headers(302, [('Location', f'/#{nth}')])
+ return
else:
page += self.ledger_as_html(db)
page += self.footer
elements_to_write = []
last_i = i = 0 ##
for nth, booking in enumerate(db.bookings):
+ move_up = nth > 0 and db.bookings[nth - 1].date_string == booking.date_string
+ move_down = nth < len(db.bookings) - 1 and db.bookings[nth + 1].date_string == booking.date_string
booking_end = last_i = booking.start_line + len(booking.lines)
booking_lines = []
i = booking.start_line ##
date=booking.date_string,
desc=booking.description,
head_comment=db.comments[booking.start_line],
+ move_up=move_up,
+ move_down=move_down,
booking_lines = booking_lines)]
elements_to_write += [single_c_tmpl.render(c=c) for c in db.comments[last_i:] if c != ''] #
return '\n'.join(elements_to_write)
end=end)
return content
+ def move_up(self, db, start, end):
+ prev_booking = None
+ for redir_nth, b in enumerate(db.bookings):
+ if b.start_line >= start:
+ break
+ prev_booking = b
+ start_at = prev_booking.start_line
+ self.make_move(db, start, end, start_at)
+ return redir_nth - 1
+
+ def move_down(self, db, start, end):
+ next_booking = None
+ for redir_nth, b in enumerate(db.bookings):
+ if b.start_line > start:
+ next_booking = b
+ break
+ start_at = next_booking.start_line + len(next_booking.lines) - (end - start) + 1
+ self.make_move(db, start, end, start_at)
+ return redir_nth
+
+ def make_move(self, db, start, end, start_at):
+ lines = db.get_lines(start, end)
+ total_lines = db.real_lines[:start] + db.real_lines[end:]
+ db.write_lines_in_total_lines_at(total_lines, start_at, lines)
+
if __name__ == "__main__":
webServer = HTTPServer((hostName, serverPort), MyServer)