Prechádzať zdrojové kódy

added seperate db_path and some exceptions

Nico Ruhnke 2 rokov pred
rodič
commit
9f6ce65e25

+ 12 - 9
time_recoder/time_recoder_gui/time_recorder_gui_main.py

@@ -78,15 +78,18 @@ def event_loop(window):
         if stop_button_is_pushed(event):
             worked_time_in_minutes = timer.get_passed_time() / 60
             selected_task_name = window.Element(TASK_TREE_COMBO_NAME).get()
-            current_task = taskHandler.get_task_by_id(taskHandler.get_task_id(selected_task_name))
-            current_branch = taskHandler.get_task_branch_as_string(current_task)
-            try:
-                save_recorded_time_in_table.save_recorded_time_in_table(worked_time_in_minutes, current_branch)
-                timer.stop()
-                print('Timer stopped recording for task: ' + selected_task_name.strip())
-            except PermissionError:
-                print("Error: couldn't write into table file. Please close any program that uses the table" +
-                      " and press 'Stop Timer' again")
+            if selected_task_name:
+                current_task = taskHandler.get_task_by_id(taskHandler.get_task_id(selected_task_name))
+                current_branch = taskHandler.get_task_branch_as_string(current_task)
+                try:
+                    save_recorded_time_in_table.save_recorded_time_in_table(worked_time_in_minutes, current_branch)
+                    timer.stop()
+                    print('Timer stopped recording for task: ' + selected_task_name.strip())
+                except PermissionError:
+                    print("Error: couldn't write into table file. Please close any program that uses the table" +
+                          " and press 'Stop Timer' again")
+            else:
+                print('Please select a Task before stopping the timer')
 
         if add_sub_task_button_is_pushed(event):
             sub_task_name = window.Element(NEW_TASK_TEXT_KEY).get()

+ 6 - 3
time_recoder/time_recoder_gui/time_recorder_task_handler.py

@@ -23,9 +23,12 @@ class TaskHandler:
         return task
 
     def add_task(self, parent_task_id: int, name: str):
-        new_task = Task(parent_task_id=parent_task_id, name=name)
-        self.session.add(new_task)
-        self.session.commit()
+        try:
+            new_task = Task(parent_task_id=parent_task_id, name=name)
+            self.session.add(new_task)
+            self.session.commit()
+        except:
+            print("Error: Couln't commit new Task to DB. Please close any other DB connections")
 
     """ fancy recursion tree ♥ """
     def get_task_tree(self, parent_task: Task):

+ 3 - 2
time_recoder/time_recorder_database/create_db.py

@@ -1,16 +1,17 @@
 
 from sqlalchemy import create_engine
 from sqlalchemy.orm import sessionmaker
-from time_recoder.time_recoder_config import PATH
+from time_recoder.time_recoder_config import DB_PATH
 from time_recoder.time_recorder_database.db_models import BASE, Task
 
 DATABASE_NAME = "time_recorder_database.db"
-ENGINE = create_engine("sqlite+pysqlite:///" + PATH + DATABASE_NAME, echo=False, future=True)
+ENGINE = create_engine("sqlite+pysqlite:///" + DB_PATH + "/" + DATABASE_NAME, echo=False, future=True)
 Session = sessionmaker(bind=ENGINE)
 
 
 def main():
     BASE.metadata.create_all(ENGINE)
+    create_root_task()
 
 
 def create_root_task():