|
| 1 | +from widgets import * |
| 2 | +from tkinter import messagebox |
| 3 | +from functions import generate_password |
| 4 | + |
| 5 | + |
| 6 | +class AddWindow(Window): |
| 7 | + |
| 8 | + def __init__(self, parent, controller): |
| 9 | + super().__init__(parent, controller) |
| 10 | + |
| 11 | + self.nav_bar.add_nav_menu( |
| 12 | + label="Add", |
| 13 | + action=lambda: self.controller.show_window("AddWindow"), |
| 14 | + is_active=True |
| 15 | + ) |
| 16 | + self.nav_bar.add_nav_menu( |
| 17 | + label="Retrieve", |
| 18 | + action=lambda: self.controller.show_window("RetrieveWindow"), |
| 19 | + ) |
| 20 | + self.nav_bar.add_nav_menu( |
| 21 | + label="Logout", |
| 22 | + action=lambda: self.logout(), |
| 23 | + ) |
| 24 | + |
| 25 | + self.password_saved_successfully = False |
| 26 | + |
| 27 | + self.website_name = StringVar() |
| 28 | + self.website_url = StringVar() |
| 29 | + self.username = StringVar() |
| 30 | + self.password = StringVar() |
| 31 | + |
| 32 | + self.add_pages(PageOne) |
| 33 | + |
| 34 | + def logout(self): |
| 35 | + """Logout the user""" |
| 36 | + |
| 37 | + # Take user confirmation |
| 38 | + user_response = messagebox.askyesno( |
| 39 | + title="Confirm Logout", |
| 40 | + message="DO YOU WANT TO LOG OUT OF YOUR ACCOUNT?" |
| 41 | + ) |
| 42 | + |
| 43 | + # If the user confirmed logout, then log him out of the account |
| 44 | + if user_response: |
| 45 | + self.controller.logout_of_the_account() |
| 46 | + |
| 47 | + |
| 48 | +class PageOne(Body): |
| 49 | + |
| 50 | + def __init__(self, parent, controller, **kw): |
| 51 | + super().__init__(parent, controller, **kw) |
| 52 | + |
| 53 | + self.config(pady=110, padx=70) |
| 54 | + |
| 55 | + self.website_name_entry = SingleRowInputBox( |
| 56 | + self, |
| 57 | + label="Website / Application name:", |
| 58 | + var=self.parent_window.website_name |
| 59 | + ) |
| 60 | + self.website_url_entry = SingleRowInputBox( |
| 61 | + self, |
| 62 | + label="Website URL (optional):", |
| 63 | + var=self.parent_window.website_url |
| 64 | + ) |
| 65 | + self.username_entry = SingleRowInputBox( |
| 66 | + self, |
| 67 | + label="Username:", |
| 68 | + var=self.parent_window.username |
| 69 | + ) |
| 70 | + self.password_entry = SingleRowInputBox( |
| 71 | + self, |
| 72 | + label="Password:", |
| 73 | + var=self.parent_window.password |
| 74 | + ) |
| 75 | + |
| 76 | + self.add_inputs( |
| 77 | + [self.website_name_entry, self.website_url_entry, self.username_entry, self.password_entry], |
| 78 | + from_row=1 |
| 79 | + ) |
| 80 | + |
| 81 | + self.website_name_entry.entry.config(width=48) |
| 82 | + self.website_url_entry.entry.config(width=52) |
| 83 | + self.username_entry.entry.config(width=64) |
| 84 | + self.password_entry.entry.config(width=47) |
| 85 | + |
| 86 | + self.generate_password_btn = ttk.Button( |
| 87 | + self, |
| 88 | + text="Generate password", |
| 89 | + command=lambda: self.show_random_password(), |
| 90 | + ) |
| 91 | + self.generate_password_btn.config(width=20, style="ToggleButton") |
| 92 | + self.generate_password_btn.grid(row=4, column=1, pady=(2, 0), sticky=E) |
| 93 | + |
| 94 | + self.add_btn = MyButton( |
| 95 | + self, |
| 96 | + text="Save Password", |
| 97 | + command=lambda: self.save_password(), |
| 98 | + ) |
| 99 | + self.add_btn.grid(row=5, column=0, columnspan=2, pady=(15, 30)) |
| 100 | + self.add_btn.config(width=20) |
| 101 | + |
| 102 | + self.website_name_entry.entry.bind("<Return>", lambda e: self.website_url_entry.entry.focus_set()) |
| 103 | + self.website_url_entry.entry.bind("<Return>", lambda e: self.username_entry.entry.focus_set()) |
| 104 | + self.username_entry.entry.bind("<Return>", lambda e: self.password_entry.entry.focus_set()) |
| 105 | + self.password_entry.entry.bind("<Return>", lambda e: self.save_password()) |
| 106 | + |
| 107 | + def show_random_password(self): |
| 108 | + """Shows a random password""" |
| 109 | + random_password = generate_password() |
| 110 | + self.parent_window.password.set(random_password) |
| 111 | + |
| 112 | + def save_password(self): |
| 113 | + """Saves the password""" |
| 114 | + |
| 115 | + user_email = self.root_controller.currently_logged_in_account.get() |
| 116 | + web_name = self.parent_window.website_name.get() |
| 117 | + web_url = self.parent_window.website_url.get() |
| 118 | + username = self.parent_window.username.get() |
| 119 | + password = self.parent_window.password.get() |
| 120 | + |
| 121 | + if user_email is None: |
| 122 | + messagebox.showerror( |
| 123 | + title="Error", |
| 124 | + message="SOME FISHY LOGIN ACTIVITY DETECTED!\nLOGOUT FROM YOUR ACCOUNT AND LOGIN AGAIN TO CONTINUE" |
| 125 | + "SAVING PASSWORDS!" |
| 126 | + ) |
| 127 | + return |
| 128 | + |
| 129 | + if web_name.strip() == "" or username.strip() == "" or password.strip() == "": |
| 130 | + messagebox.showerror( |
| 131 | + title="Error!", |
| 132 | + message="FIELDS CANNOT BE EMPTY!" |
| 133 | + ) |
| 134 | + return |
| 135 | + |
| 136 | + self.root_controller.add_new_password( |
| 137 | + for_user=user_email, web_name=web_name, web_url=web_url, username=username, password=password |
| 138 | + ) |
| 139 | + |
| 140 | + # If the user saved a password successfully |
| 141 | + if self.parent_window.password_saved_successfully: |
| 142 | + |
| 143 | + self.parent_window.website_name.set("") |
| 144 | + self.parent_window.website_url.set("") |
| 145 | + self.parent_window.username.set(user_email) |
| 146 | + self.parent_window.password.set("") |
| 147 | + |
| 148 | + # Resetting the variable |
| 149 | + self.parent_window.password_saved_successfully = False |
0 commit comments