diff --git a/passman/database.py b/passman/database.py index fa5ecc0..9d7176e 100644 --- a/passman/database.py +++ b/passman/database.py @@ -1,53 +1,71 @@ -import tkinter as tk -import sqlite3 +import models as md class DatabaseConnection: def __init__(self) -> None: - - self.connection = sqlite3.connect("passman.db") + # durectory should be C:\Users\\AppData\Roaming\ + md.db.connect() - self.cursor = self.connection.cursor() + def create_passwords_table(self): - def fetch_data(self): - - SQL_query = "SELECT password_name, password_address FROM passman" + if not md.db_key: - try: - self.cursor.execute("CREATE TABLE passman (id INTEGER PRIMARY KEY AUTOINCREMENT, password_address TEXT, password_name TEXT, password TEXT, password_description TEXT DEFAULT NULL, password_update DATETIME DEFAULT CURRENT_TIMESTAMP)") # How to add timestamp? + pass - self.connection.commit() - for row in self.cursor.execute("SELECT * FROM passman;"): + def check_db_password(self): - print(row) + pass - except: - print("Such table already exists") + def add_password(self, new_p_address, new_p_name, new_p, new_p_description): - db_data = self.cursor.execute(SQL_query) + new_password = md.Password.create( + password_address=new_p_address, + password_name=new_p_name, + password=new_p, + password_description=new_p_description + ) + + new_password.save() + + + def fetch_data(self): + + db_data = md.Password.select(md.Password.password_name, md.Password.password_address) return db_data - def fetch_password_data(self, sql_query, password_data, description_field): + def fetch_password_data(self, sql_data): + + password_data = sql_data.split() + + result = md.Password.select().where((md.Password.password_name == password_data[0]) & (md.Password.password_address == password_data[1])) - data = self.cursor.execute(sql_query) + return result + - fetched_data = data.fetchall() + def update_password_data(self, password_data, password_value): + + splitted_data = password_data.split() + + row = md.Password.get((md.Password.password_name == splitted_data[0]) & (md.Password.password_address == splitted_data[1])) + + row.password = password_value - password_data.set(fetched_data[0][0]) + row.save() - description_field.delete("1.0", tk.END) - description_field.insert(tk.END, fetched_data[0][1]) + def delete_password(self, p_name, p_address): + password_info = md.Password.get(md.Password.password_name == p_name & md.Password.password_address == p_address) + password_info.delete_instance() def __del__(self) -> None: - self.connection.close() \ No newline at end of file + md.db.close() \ No newline at end of file diff --git a/passman/models.py b/passman/models.py new file mode 100644 index 0000000..e03b6ff --- /dev/null +++ b/passman/models.py @@ -0,0 +1,26 @@ +from peewee import * +from datetime import datetime +from playhouse.sqlite_ext import SqliteExtDatabase + +db_key = "" + +def create_passwords_table(): + + db = SqliteExtDatabase("passman.db", passphrase=db_key) + + class Password(Model): + + password_address = CharField() + + password_name = CharField() + + password = CharField() + + password_description = TextField() + + password_update = DateTimeField(default=datetime.now()) + + + class Meta: + + database = db diff --git a/passman/password.py b/passman/password.py index cce820a..6263491 100644 --- a/passman/password.py +++ b/passman/password.py @@ -1,11 +1,47 @@ import tkinter as tk -import sqlite3 import style from widget_generators import * from database import DatabaseConnection import helper_functions +class WindowIntro(tk.Toplevel): + + def __init__(self, parent): + + super().__init__(parent) + + self.parent = parent + + self.welcome_label = LabelGenerator( + master=self, + text="Meet Passman!", + x=style.x_margin, + y=style.y_label_password_field, + + ) + self.set_password_button = ButtonGenerator( + master=self, + text="Enter", + command=self.__close_window, + x=style.x_margin, + y=style.y_password_description, + width=style.width_button, + height=style.height_button + ) + + + def __close_window(self): + + self.parent.attributes("-alpha", 1) + + self.destroy() + + # It should check if Database password has already been set + + + + class WindowNewPassword(tk.Toplevel): def __init__(self, parent): @@ -156,17 +192,7 @@ def __save_new_password(self): password_description_value = self.new_password_field.get() - new_password_data = [(password_address_value, password_name_value, password_field_value, password_description_value)] - - connection = sqlite3.connect("passman.db") - - cursor = connection.cursor() - - cursor.executemany("INSERT INTO passman (password_address, password_name, password, password_description) VALUES (?,?,?,?)", new_password_data) - - connection.commit() - - connection.close() + self.parent.dc.add_password(password_address_value, password_name_value, password_field_value, password_description_value) self.parent.password_list.insert(tk.END, password_name_value + " " + password_address_value) @@ -233,21 +259,13 @@ def confirm_password_deletion(self): password_values_list = self.parent.password_list_value.split() - connection = sqlite3.connect("passman.db") - - cursor = connection.cursor() - - cursor.execute(f"DELETE FROM passman WHERE password_name = '{password_values_list[0]}' AND password_address = '{password_values_list[1]}';") - - connection.commit() + self.parent.dc.delete_password(password_values_list[0], password_values_list[1]) - listbox_data = cursor.execute("SELECT password_name, password_address FROM passman;") + listbox_data = self.parent.dc.fetch_data() for row in listbox_data: self.parent.password_list.insert(tk.END, f"{row[0]} {row[1]}") - - connection.close() self.notification_window = NotificationGenerator(text="Password successfully deleted!") diff --git a/passman/root.py b/passman/root.py index c00c337..a8da82f 100644 --- a/passman/root.py +++ b/passman/root.py @@ -1,8 +1,8 @@ import tkinter as tk +import password as ps import style -from database import * +from database import DatabaseConnection from widget_generators import * -from password import WindowNewPassword, WindowDeletePassword from helper_functions import toggle_password class App(tk.Tk): @@ -12,12 +12,13 @@ def __init__(self): #running the app super().__init__() + self.__check_db_existence() self.__initialize_main_window() - self.__initialize_password_list_frame() + self.dc = DatabaseConnection() self.__fill_listbox() @@ -31,13 +32,21 @@ def __init__(self): self.mainloop() + def __check_db_existence(self): + + # If db exists - nothing, else - show WindowIntro + self.attributes("-alpha", 0) + + ps.WindowIntro(self) + + def __initialize_main_window(self): self.title("Passman") self.geometry(f"{style.x}x{style.y}") - self.minsize(style.x, style.y) + self.resizable(False, False) self.eval("tk::PlaceWindow . center") @@ -77,14 +86,12 @@ def __initialize_password_list_frame(self): def __fill_listbox(self): self.password_list.delete(0, tk.END) - - db = DatabaseConnection() - listbox_data = db.fetch_data() + listbox_data = self.dc.fetch_data() for row in listbox_data: - self.password_list.insert(tk.END, f"{row[0]} {row[1]}") + self.password_list.insert(tk.END, f"{row.password_name} {row.password_address}") def __initialize_password_handling_frame(self): @@ -192,25 +199,25 @@ def __initialize_password_handling_frame(self): def __delete_password(self): - return WindowDeletePassword(self) + return ps.WindowDeletePassword(self) def __create_new_password_window(self): - return WindowNewPassword(self) + return ps.WindowNewPassword(self) def __get_password(self, *args): self.password_list_value = self.password_list.get(self.password_list.curselection()) - current_password = self.password_list_value.split() + password_data = self.dc.fetch_password_data(self.password_list_value)[0] - SQL_query_password_data = f"SELECT password, password_description FROM passman WHERE password_name = '{current_password[0]}' AND password_address = '{current_password[1]}';" + self.password_value.set(password_data.password) - db = DatabaseConnection() + self.password_description.delete("1.0", tk.END) - db.fetch_password_data(SQL_query_password_data, self.password_value, self.password_description) + self.password_description.insert(tk.END, password_data.password_description) self.button_update_password.config(state="normal") @@ -231,17 +238,9 @@ def __update_password(self): else: - password_list_value = self.password_list.get(self.password_list.curselection()).split(" ") - - db_connection = sqlite3.connect("passman.db") - - cursor = db_connection.cursor() + new_password_data = self.password_list.get(self.password_list.curselection()) - cursor.execute(f"UPDATE passman SET password = '{self.password_value.get()}' WHERE password_name = '{password_list_value[0]}' AND password_address = '{password_list_value[1]}';") - - db_connection.commit() - - del db_connection + self.dc.update_password_data(new_password_data, self.password_value.get()) self.password_field.config(state="disabled") diff --git a/passman/tempCodeRunnerFile.py b/passman/tempCodeRunnerFile.py deleted file mode 100644 index 833c3de..0000000 --- a/passman/tempCodeRunnerFile.py +++ /dev/null @@ -1,2 +0,0 @@ - - def __initialize_main_window(self): \ No newline at end of file diff --git a/tests/db_tests.py b/tests/db_tests.py index d7ab486..affb006 100644 --- a/tests/db_tests.py +++ b/tests/db_tests.py @@ -27,7 +27,7 @@ def test_data_presence(): cursor = connection.cursor() - for row in cursor.execute("SELECT * FROM passman;"): + for row in cursor.execute("SELECT * FROM password;"): print(row)