From 1866f61b05baf74414c8280fbb41cf23a3912043 Mon Sep 17 00:00:00 2001 From: mily Date: Fri, 18 Apr 2025 23:42:34 +0200 Subject: [PATCH] Add logging out --- src/pages/settings.py | 23 +++++++++++++++++++++-- src/utils.py | 16 +++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/pages/settings.py b/src/pages/settings.py index 7b5531a..1021952 100644 --- a/src/pages/settings.py +++ b/src/pages/settings.py @@ -1,13 +1,26 @@ import flet as ft from constants import * -from utils import setthemecolor, setlanguage, restart +from utils import setthemecolor, setlanguage, restart, logout from i18n import _, set_language def SettingsPage(page): # Create the main container to hold everything main_container = ft.Container() + + def handle_logout(e): + logout() + restart(e.page) + logout_modal = ft.AlertDialog( + modal=True, + title=ft.Text(_("Logout")), + content=ft.Text(_("Do you want to logout?")), + actions=[ + ft.TextButton("Yes", on_click=handle_logout), + ft.TextButton("No", on_click=lambda e: page.close(logout_modal)), + ], + actions_alignment=ft.MainAxisAlignment.END, + ) - # Create a custom notification that we'll show/hide notification = ft.Container( visible=False, bgcolor=ft.colors.AMBER_100, @@ -103,6 +116,12 @@ def SettingsPage(page): on_change=onthemechange ) ]), + ft.Row([ + ft.ElevatedButton( + _("Logout"), + on_click=lambda e: page.open(logout_modal) + ), + ]), ]), expand=True # Make this container expand to push the notification to the bottom ), diff --git a/src/utils.py b/src/utils.py index 0d3e34e..57b07a8 100644 --- a/src/utils.py +++ b/src/utils.py @@ -106,4 +106,18 @@ def get_current_month_dates(): def getinitials(full_name): initials = ''.join([part[0].upper() for part in full_name.split()]) - return initials \ No newline at end of file + return initials + +def logout(): + config = configparser.ConfigParser() + config.read(f"{getconfigpath()}/config.ini") + config["Settings"]["islogged"] = "False" + config["User"]["fullname"] = "" + config["User"]["grade"] = "" + config["User"]["semester"] = "1" + + if os.path.exists("database.db"): + os.remove("database.db") + + with open(f"{getconfigpath()}/config.ini", "w") as file: + config.write(file) \ No newline at end of file