From 45089f16db001c4199a733a192e09bc0b716aefd Mon Sep 17 00:00:00 2001 From: Maximilian Zettler Date: Wed, 22 Jun 2022 14:03:03 +0200 Subject: [PATCH] add simple smart-punch button in gui --- timebot/app.py | 5 ++--- timebot/gui.py | 14 ++++++++++++-- timebot/timebot.py | 6 ++++-- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/timebot/app.py b/timebot/app.py index f843d88..d0d164a 100644 --- a/timebot/app.py +++ b/timebot/app.py @@ -96,10 +96,9 @@ def run(): getattr(tb, args.t)(punch_datetime) elif args.subparser_name == "smart-punch": if args.s == "now": - punch_datetime = datetime.datetime.now() + tb.smart_punch() else: - punch_datetime = parse_user_time_input(args.s) - tb.smart_punch(punch_datetime) + tb.smart_punch(parse_user_time_input(args.s)) elif args.subparser_name == "list-entries": end_date = None if args.end_date: diff --git a/timebot/gui.py b/timebot/gui.py index c63a388..ff545df 100644 --- a/timebot/gui.py +++ b/timebot/gui.py @@ -74,14 +74,17 @@ class TimebotMainWindow(QWidget): self.text_box_status = QLabel() self.text_box_hours_present_area = HoursPresentLabelArea(parent=self) self.btn_update_status = QPushButton("Refresh Status") + self.btn_smart_punch = QPushButton("Smart Punch") layout = QVBoxLayout() layout.addWidget(self.text_box_status) layout.addWidget(self.text_box_hours_present_area) layout.addWidget(self.btn_update_status) + layout.addWidget(self.btn_smart_punch) self.setLayout(layout) self.btn_update_status.clicked.connect(self.update_status) + self.btn_smart_punch.clicked.connect(self.smart_punch) self.status_timer_time = timeparse(refresh_interval) * 1000 self.status_timer = QTimer() @@ -117,7 +120,7 @@ class TimebotMainWindow(QWidget): self.status_thread = QThread() self.status_worker = TimebotApiWorker(self, self.timebot) self.status_worker.moveToThread(self.status_thread) - self.status_thread.started.connect(self.status_worker.run) + self.status_thread.started.connect(self.status_worker.run_status) self.status_worker.finished.connect(self.status_thread.quit) self.status_worker.finished.connect(self.status_worker.deleteLater) self.status_thread.finished.connect(self.status_thread.deleteLater) @@ -127,6 +130,13 @@ class TimebotMainWindow(QWidget): self.status_thread.start() self.update_status_started() + def smart_punch(self): + try: + self.timebot.smart_punch() + self.update_status() + except Exception as e: + self.error_msg_show(str(e)) + def get_password(self, callback: callable = None): if self.timebot.mobatime_api.password is not None: return @@ -182,7 +192,7 @@ class TimebotApiWorker(QObject): self.tmw = tmw self.timebot = timebot - def run(self): + def run_status(self): try: self.tmw.present = self.timebot.present self.tmw.update_hours_present(override=self.timebot.get_hours_present()) diff --git a/timebot/timebot.py b/timebot/timebot.py index e16cc0f..8bc1b22 100644 --- a/timebot/timebot.py +++ b/timebot/timebot.py @@ -307,15 +307,17 @@ class TimeBot: """ self.mobatime_api.save_entry(punch_datetime, PunchCodes.BREAK_END.value, note="pause ende").raise_for_status() - def smart_punch(self, punch_datetime: datetime.datetime) -> None: + def smart_punch(self, punch_datetime: datetime.datetime = None) -> None: """ Perform smart punch for today. This tries to detect your last action and will create entries in to following order: punch_in -> break_start -> break_end -> punch_out - :param datetime.datetime punch_datetime: a datetime object to identify the current day aka. today + :param datetime.datetime punch_datetime: a datetime object to identify the current day aka. today (default: now) """ + if punch_datetime is None: + punch_datetime = datetime.datetime.now() last_punch = self.mobatime_api.get_entries( 1, start_date=punch_datetime.replace(hour=0, minute=0, second=0, microsecond=0),