From a7cf012d41c26c5ec41057b2affa8c45157b14d5 Mon Sep 17 00:00:00 2001 From: Alexander Malzkuhn Date: Thu, 24 Apr 2025 12:06:58 +0200 Subject: [PATCH] =?UTF-8?q?Paarweise=20Buttons=20f=C3=BCr=20Buchungen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- users.py | 3 +- users/testuser1/2025-4.json | 4 + users/testuser1/2025-4.txt | 14 +-- web_ui.py | 216 ++++++++++++++++++++---------------- 4 files changed, 129 insertions(+), 108 deletions(-) create mode 100644 users/testuser1/2025-4.json diff --git a/users.py b/users.py index 2304326..d534e34 100644 --- a/users.py +++ b/users.py @@ -153,7 +153,8 @@ class user: year_now = int(datetime.datetime.now().year) month_now = int(datetime.datetime.now().month) - if start_year == year: + if start_year == int(year): + print("Start_year is year") if start_year == year_now: for i in range(start_month, month_now + 1): available_months.append(i) diff --git a/users/testuser1/2025-4.json b/users/testuser1/2025-4.json new file mode 100644 index 0000000..b7881be --- /dev/null +++ b/users/testuser1/2025-4.json @@ -0,0 +1,4 @@ +{ + "archived": 0, + "total_hours": 0 +} \ No newline at end of file diff --git a/users/testuser1/2025-4.txt b/users/testuser1/2025-4.txt index ab032c6..5ed0156 100644 --- a/users/testuser1/2025-4.txt +++ b/users/testuser1/2025-4.txt @@ -1,13 +1,9 @@ -1743562800 1743566400 -1743584340 1743606000 1743652800 1743660240 -1743667140 -1743685200 1744889948 -1744889966 +1744890300 1744989797 1744989827 1744989830 @@ -22,15 +18,11 @@ 1744991473 1744991477 1744991770 -1744991777 1745181046 1745181050 -1745240760 +1745215200 +1745229600 1745390818 1745390894 1745390894 1745391029 -1745391037 -1745391056 -1745391058 -1745391059 diff --git a/web_ui.py b/web_ui.py index 419251a..b9324e5 100644 --- a/web_ui.py +++ b/web_ui.py @@ -133,20 +133,15 @@ def page_admin(): month_binder = ValueBinder() def update_months(): - #try: - print('Update Months called') - current_user = user(time_user.value) - available_months = current_user.get_months(year_binder.value) - available_months_dict = {} - for element in available_months: - available_months_dict[element] = calendar.month_name[int(element)] - print(available_months_dict) - select_month.clear() - select_month.set_options(available_months_dict) - select_month.value = list(available_months)[0] - - #except: - # print("select.month existiert noch nicht") + current_user = user(time_user.value) + available_months = current_user.get_months(year_binder.value) + available_months_dict = {} + for element in available_months: + available_months_dict[element] = calendar.month_name[int(element)] + print(available_months_dict) + select_month.clear() + select_month.set_options(available_months_dict) + select_month.value = list(available_months)[0] userlist = list_users() ui.markdown("Benutzer:") @@ -162,21 +157,20 @@ def page_admin(): available_years = current_user.get_years() available_months = current_user.get_months(current_year) available_months_dict = { } + for element in available_months: available_months_dict[element] = calendar.month_name[int(element)] - print('Selektoren konstruiert') - print(f" current_month: {current_month} und available_months: {available_months}") + if current_month in available_months: set_month = current_month else: set_month = available_months[0] - print(set_month) - print(f" current_year: {current_year} und available_years: {available_years}") + if str(current_year) in available_years: set_year = str(current_year) else: set_year = (available_years[0]) - print(set_year) + select_month = ui.select(options=available_months_dict, value=set_month).bind_value_to(month_binder, 'value') select_year = ui.select(options=available_years, value=set_year, on_change=update_months).bind_value_to(year_binder, 'value') @@ -190,78 +184,137 @@ def page_admin(): with ui.grid(columns='auto auto 1fr 1fr 1fr 1fr') as table_grid: ui.markdown("**Datum**") ui.markdown("**Buchungen**") - ui.markdown("**Soll**") ui.markdown("**Ist**") + ui.markdown("**Soll**") ui.markdown("**Saldo**") ui.space() current_user = user(time_user.value) timestamps = current_user.get_timestamps(year=select_year.value, month=select_month.value) + # Dictionary für sortierte Timestamps + timestamps_dict = { } + # Dictionary mit zunächst leeren Tageinträgen befüllen + for day in range(1, monthrange(int(select_year.value), int(select_month.value))[1] + 1): + # Jeder Tag bekommt eine leere Liste + timestamps_dict[day] = [ ] + + # Alle Timestamps durchgehen und sie den Dictionaryeinträgen zuordnen: + for stamp in timestamps: + day_of_month_of_timestamp = int(datetime.datetime.fromtimestamp(int(stamp)).strftime("%-d")) + timestamps_dict[day_of_month_of_timestamp].append(int(stamp)) general_saldo = 0 - for day in range(1, monthrange(int(select_year.value), int(select_month.value))[1] + 1): + for day in list(timestamps_dict): + # Datum für Tabelle konstruieren day_in_list = datetime.datetime(int(select_year.value), int(select_month.value), day) - ui.markdown(f"{day_in_list.strftime('%a')}., {day}. {calendar.month_name[int(select_month.value)]}") # Buchungen with ui.row(): - counter = 0 - for i in timestamps: - actual_timestamp = datetime.datetime.fromtimestamp(int(i)) - timestamp_day = actual_timestamp.strftime('%-d') + if len(timestamps_dict[day]) > 0: + timestamps_dict[day].sort() - if int(timestamp_day) == int(day): - def edit_entry(t_stamp, day): - with ui.dialog() as edit_dialog, ui.card(): - ui.markdown("###Eintrag bearbeiten") - timestamp = datetime.datetime.fromtimestamp(int(t_stamp)) - input_time = ui.time().classes('w-full justify-center') - input_time.value = timestamp.strftime('%H:%M') + def edit_entry(t_stamp, day): - def save_entry(day): + with ui.dialog() as edit_dialog, ui.card(): + ui.markdown("###Eintrag bearbeiten") + timestamp = datetime.datetime.fromtimestamp(int(t_stamp)) + input_time = ui.time().classes('w-full justify-center') - position = timestamps.index(t_stamp) - new_time_stamp = datetime.datetime(int(select_year.value), int(select_month.value), day, int(input_time.value[:2]), int(input_time.value[-2:])) - timestamps[position] = str( - int(new_time_stamp.timestamp())) + "\n" - # print(timestamps) - current_user = user(time_user.value) - current_user.write_edited_timestamps(timestamps, select_year.value, select_month.value) - edit_dialog.close() - calendar_card.clear() - update_month_and_year() - month_header.set_content(f"###Buchungen für {calendar.month_name[int(select_month.value)]} {select_year.value}") - ui.notify("Eintrag gespeichert") + input_time.value = timestamp.strftime('%H:%M') - def del_entry(): - timestamps.remove(t_stamp) - timestamps.sort() - current_user = user(time_user.value) - current_user.write_edited_timestamps(timestamps, select_year.value, select_month.value) - edit_dialog.close() - calendar_card.clear() - update_month_and_year() - month_header.set_content(f"###Buchungen für {calendar.month_name[int(select_month.value)]} {select_year.value}") - ui.notify("Eintrag gelöscht") + def save_entry(day): + nonlocal t_stamp + t_stamp = f"{t_stamp}\n" + position = timestamps.index(t_stamp) + new_time_stamp = datetime.datetime(int(select_year.value), + int(select_month.value), day, + int(input_time.value[:2]), + int(input_time.value[-2:])) + timestamps[position] = str( + int(new_time_stamp.timestamp())) + "\n" + current_user = user(time_user.value) + current_user.write_edited_timestamps(timestamps, + select_year.value, + select_month.value) + edit_dialog.close() + calendar_card.clear() + update_month_and_year() + month_header.set_content( + f"###Buchungen für {calendar.month_name[int(select_month.value)]} {select_year.value}") + ui.notify("Eintrag gespeichert") + + def del_entry(): + nonlocal t_stamp + t_stamp = f"{t_stamp}\n" + timestamps.remove(t_stamp) + timestamps.sort() + current_user = user(time_user.value) + current_user.write_edited_timestamps(timestamps, + select_year.value, + select_month.value) + edit_dialog.close() + calendar_card.clear() + update_month_and_year() + month_header.set_content( + f"###Buchungen für {calendar.month_name[int(select_month.value)]} {select_year.value}") + ui.notify("Eintrag gelöscht") + + with ui.row(): + ui.button("Speichern", + on_click=lambda day=day: save_entry(day)) + ui.button("Löschen", on_click=del_entry) + ui.button("Abbrechen", on_click=edit_dialog.close) + + edit_dialog.open() + + for i in range(len(timestamps_dict[day])): + try: + temp_pair = [ timestamps_dict[day][i] , timestamps_dict[day][i+1] ] + with ui.card(): with ui.row(): - ui.button("Speichern", - on_click=lambda day=day: save_entry(day)) - ui.button("Löschen", on_click=del_entry) - ui.button("Abbrechen", on_click=edit_dialog.close) + for j in temp_pair: + ui.button(datetime.datetime.fromtimestamp(int(j)).strftime('%H:%M'), on_click=lambda t_stamp=j, day=day: edit_entry(t_stamp, day)) + except: + if len(timestamps_dict[day]) % 2 != 0: + with ui.card(): + ui.button(datetime.datetime.fromtimestamp(int(timestamps_dict[day][i])).strftime('%H:%M'), on_click=lambda t_stamp=timestamps_dict[day][i], day=day: edit_entry(t_stamp, day)) - edit_dialog.open() - counter += 1 + # Arbeitszeit Ist bestimmen + timestamps_of_this_day = [] - ui.button(actual_timestamp.strftime('%H:%M'), on_click=lambda t_stamp=i, day=day: edit_entry(t_stamp, day)) - if counter % 2 != 0: - ui.markdown("-") - else: - ui.markdown("|") + # Suche mir alle timestamps für diesen Tag + for i in timestamps: + actual_timestamp = datetime.datetime.fromtimestamp(int(i)) + timestamp_day = actual_timestamp.strftime('%-d') + + if int(timestamp_day) == int(day): + timestamps_of_this_day.append(i) + + timestamps_of_this_day.sort() + time_sum = 0 + if len(timestamps_of_this_day) > 1: + + if len(timestamps_of_this_day) % 2 == 0: + for i in range(0, len(timestamps_of_this_day), 2): + time_delta = int( + timestamps_of_this_day[i + 1]) - int( + timestamps_of_this_day[i]) + time_sum = time_sum + time_delta + else: + for i in range(0, len(timestamps_of_this_day) - 1, 2): + time_delta = int( + timestamps_of_this_day[i + 1]) - int( + timestamps_of_this_day[i]) + time_sum = time_sum + time_delta + + ui.markdown(convert_seconds_to_hours(time_sum)) + else: + ui.markdown("Kein") # Arbeitszeitsoll bestimmen workhour_entries = list(current_user.workhours) @@ -279,35 +332,6 @@ def page_admin(): ui.markdown(f"{current_user.workhours[entry][str(weekday_index)]} h") found_match = True - # Arbeitszeit Ist bestimmen - timestamps_of_this_day = [ ] - - # Suche mir alle timestamps für diesen Tag - for i in timestamps: - actual_timestamp = datetime.datetime.fromtimestamp(int(i)) - timestamp_day = actual_timestamp.strftime('%-d') - - if int(timestamp_day) == int(day): - timestamps_of_this_day.append(i) - - timestamps_of_this_day.sort() - time_sum = 0 - if len(timestamps_of_this_day) > 1: - - - if len(timestamps_of_this_day) % 2 == 0: - for i in range(0, len(timestamps_of_this_day), 2): - time_delta = int(timestamps_of_this_day[i+1]) - int(timestamps_of_this_day[i]) - time_sum = time_sum + time_delta - else: - for i in range(0, len(timestamps_of_this_day) - 1, 2): - time_delta = int(timestamps_of_this_day[i+1]) - int(timestamps_of_this_day[i]) - time_sum = time_sum + time_delta - - ui.markdown(convert_seconds_to_hours(time_sum)) - else: - ui.markdown("Kein") - # Plus und Minuszeit für den Tag berechnen if time.time() > day_in_list.timestamp(): time_duty = int(current_user.workhours[entry][str(weekday_index)]) * 3600