- try:
- prefix = self.apps['ledger'] if hasattr(self, 'apps') else ''
- parsed_url = urlparse(self.path)
- length = int(self.headers['content-length'])
- postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
- start = int(postvars['start'][0])
- end = int(postvars['end'][0])
- db = LedgerDB(prefix)
- add_empty_line = None
- lines = []
- # get inputs
- if prefix + '/add_structured' == parsed_url.path and not 'revert' in postvars.keys():
- lines, add_empty_line = db.booking_lines_from_postvars(postvars)
- elif prefix + '/add_free' == parsed_url.path and not 'revert' in postvars.keys():
- lines = postvars['booking'][0].splitlines()
- # validate where appropriate
- if ('save' in postvars.keys()) or ('check' in postvars.keys()):
- _, _ = parse_lines(lines)
- # if saving, process where to and where to redirect after
- if 'save' in postvars.keys():
- last_date = str(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.strptime(first_token, '%Y-%m-%d')
- target_date = first_token
- except ValueError:
- pass
- if start == end == 0:
- start = db.insert_at_date(lines, target_date)
- nth = db.get_nth_for_booking_of_start_line(start)
- else:
- 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
- self.redirect(prefix + f'/#{nth}')
- # otherwise just re-build editing form
+ prefix = self.apps['ledger'] if hasattr(self, 'apps') else ''
+ parsed_url = urlparse(self.path)
+ length = int(self.headers['content-length'])
+ postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
+ start = int(postvars['start'][0])
+ end = int(postvars['end'][0])
+ db = LedgerDB(prefix)
+ add_empty_line = None
+ lines = []
+ # get inputs
+ if prefix + '/add_structured' == parsed_url.path and not 'revert' in postvars.keys():
+ lines, add_empty_line = db.booking_lines_from_postvars(postvars)
+ elif prefix + '/add_free' == parsed_url.path and not 'revert' in postvars.keys():
+ lines = postvars['booking'][0].splitlines()
+ # validate where appropriate
+ if ('save' in postvars.keys()) or ('check' in postvars.keys()):
+ _, _ = parse_lines(lines)
+ # if saving, process where to and where to redirect after
+ if 'save' in postvars.keys():
+ last_date = str(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.strptime(first_token, '%Y-%m-%d')
+ target_date = first_token
+ except ValueError:
+ pass
+ if start == end == 0:
+ start = db.insert_at_date(lines, target_date)
+ nth = db.get_nth_for_booking_of_start_line(start)
+ else:
+ 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
+ self.redirect(prefix + f'/#{nth}')
+ # otherwise just re-build editing form
+ else:
+ if prefix + '/add_structured' == parsed_url.path:
+ edit_content = db.add_structured(start, end, temp_lines=lines, add_empty_line=add_empty_line)