def theMainMenu(self): """Takes in the user_type and sends the main menu screen accordingly""" if self.user_type == 'admin': menu = """ 1. User Management 2. Book Shelf Management 3. Book Shelf browse 4. update password 5. Quit """ menuFunc = {"1": self.userManagement, "2": self.bookShelfManagement, "3": "bookShelfBrowse", \ "4": self.updatePassword, "5": sys.exit} else: menu = """ 1 Book Shelf browse 2 update password 3 quit """ menuFunc = {"1": "bookShelfBrowse", "2": self.updatePassword, "3": sys.exit} utils.printMenuHead("Main Menu") print(menu) try: menuFunc[str(input("Enter your choice :")).strip()]() except KeyError: utils.printMenuHead("Invalid option entered") time.sleep(2) self.theMainMenu() self.theMainMenu()
def bookShelfManagement(self): userMenu = """ 1 Add book 2 Remove book 3 Update book 4 Go back to main menu 5 quit """ menuFunc = {"1": self.addBook, "2": "self.removeBook","3": "self.updateBook", "4": self.theMainMenu,\ "5": sys.exit} utils.printMenuHead("Book Shelf Management") print(userMenu) try: choice = str(input("Enter your choice :")).strip() print(choice) print(menuFunc.keys()) menuFunc[choice]() except KeyError: utils.printMenuHead("Invalid option entered") time.sleep(2) self.bookShelfManagement() self.bookShelfManagement()
def addUser(self): """This function will add a new user""" utils.printMenuHead("Add New User") #Load user details userDetails = pickle.load(open(os.getcwd() + "\\user.p", "rb")) #Fetch user and password from the user user = str(input("Enter username :"******"User Already Exists") print("User Already exists !!!!") time.sleep(3) self.logger( "Tried to add user {} but user already exists".format(user)) return None #Ask for password password = str(input("Enter Password :"******"Enter Password again to confirm :")).strip() while password != check_pwd: utils.printMenuHead("Invalid Password") check_pwd = str( input("Enter Password again to confirm :")).strip() user_type = str( input("Enter user_type (admin or user) :")).strip() while user_type not in ['admin', 'user']: utils.printMenuHead("invalid user_type") user_type = str( input("Enter user_type (admin or user) :")).strip() userDetails = userDetails + [{ "user": user, "user_type": user_type, "password": password }] pickle.dump(userDetails, open(os.getcwd() + "\\user.p", "wb")) self.logger("added user {} successfully".format(user)) utils.printMenuHead("User added Successfully") return True, user
def updatePassword(self): """This function will update password for an existing user""" #load user details into memory user = self.user userDetails = pickle.load(open(os.getcwd() + "\\user.p", "rb")) utils.printMenuHead("Update Password") #Fetch user and password from the user #user = str(input("Enter username")).strip().lower() password = str(input("Enter Current Password :"******"Invalid password") tries = "No" if (3 - i) == 0 else 3 - i print("Enter the password again - {} more tries left !!!!".format( tries)) password = str(input("Enter Current Password :"******"password change attempt for user {} failed as the user entered invalid password" \ "for more than 3 times".format(user)) utils.printMenuHead("Invalid password entered") print("Invalid password entered, Quitting") time.sleep(3) return False new_password = str(input("Enter New Password :"******"Enter Password again to confirm :")).strip() while new_password != check_pwd: utils.printMenuHead("Invalid Password") check_pwd = str( input("Enter Password again to confirm :")).strip() getUserInfo[0]['password'] = new_password userDetails = list(filter(lambda x: x['user'] != user, userDetails)) + getUserInfo pickle.dump(userDetails, open(os.getcwd() + "\\user.p", "wb")) utils.printMenuHead("password updated") print("Password for user {} updated successfully!!!!".format(user)) self.logger("password updated for user {} successfully".format(user)) time.sleep(3) return True, user
def main(): """Execution start point for library management""" #send login screen to get valid user name and user type user, user_type = user_class.get_user() if not user: utils.printMenuHead("Invalid User") print("Entered user doesn't exist, quitting") time.sleep(3) sys.exit(1) Session = session(user, user_type) #send Main Menu Session.theMainMenu()
def userManagement(self): userMenu = """ 1 Add User 2 Remove User 3 Go back to main menu 4 quit """ menuFunc = {"1": self.addUser, "2": self.removeUser, "3": self.theMainMenu, "4": sys.exit} utils.printMenuHead("User Management") print(userMenu) try: menuFunc[str(input("Enter your choice :")).strip()]() except KeyError: utils.printMenuHead("Invalid option entered") time.sleep(2) self.userManagement() self.userManagement()
def get_user(): """Fetch the user details and check if user exists, if exists set the privileges""" # Welcome utils.printMenuHead("Login") # Fetch user and password from the user user = str(input("Enter username :"******"Enter Password :"******"\\user.p", "rb")) # Check if user exists userData = [(userDict['user'], userDict['user_type']) for userDict in userDetails if (userDict['user'].lower() == user and \ userDict['password'] == password)] if userData: return userData[0] else: return None, None
def removeUser(self): """This function removes existing users from the system, Only an admin can remove users""" #load user details into memory userDetails = pickle.load(open(os.getcwd() + "\\user.p", "rb")) utils.printMenuHead("Remove User") #get details of the user to be removed user = str(input("Enter username :"******"User doesn't exist") print("User doesn't exist !!!!") self.logger( "tried to remove user {}, the user doesn't exist !!!!".format( user)) time.sleep(3) return None #remove user userDetails = list(filter(lambda x: x['user'] != user, userDetails)) #Confirm user is removed check_existing_user = [ userDict['user'] for userDict in userDetails if userDict['user'] == user ] if not check_existing_user: pickle.dump(userDetails, open(os.getcwd() + "\\user.p", "wb")) utils.printMenuHead("User removed") print("User removed Succesfully !!!!") self.logger("user {} removed successfully".format(user)) time.sleep(3) return True, user
def addBook(self): """This function will add books to the library catalog""" #Initialise the current book book = {"Serial_No": "", "Name": "", "Pages": "", "Category": "", "Author": "", "Price": "", "Publisher": "",\ "Section": "", "Shelf no": "", "is_Here": "", "who has ?": " " } #get the catalog bookCatalog = pickle.load(open(os.getcwd() + "\\books.p", "rb")) #Construct the book details not_finished = True current_book = dict() while not_finished: for key in book: if key not in ["is_Here", "who has ?"]: current_book[key] = str(input("Enter the {} of the book {} :".format(key, "current value is " \ + current_book[key] if current_book[key] else ""))) elif key == "is_Here": incorrect_value = True while incorrect_value: response = str( input( "Is the book here ? (Yes / No) :")).strip() if response.lower() not in ["yes", "no"]: utils.printMenuHead( "Invalid value, please correct") print("Please choose either Yes or No") else: current_book[key] = response incorrect_value = False else: current_book[key] = str( input("Who has the book ? :".format(key))) utils.printMenuHead("Review Details of the book") for key, value in book.items(): print(key, ":", value) try: incorrect_choice = True while incorrect_choice: choice = str( input( "Do you want to update details? Yes / No? :" )).strip() if choice.lower() not in ["yes", "no"]: utils.printMenuHead( "Invalid value, please correct") print("Please choose either Yes or No") time.sleep(2) elif choice.lower() == "no": incorrect_choice = False not_finished = False else: incorrect_choice = False self.addBook() except Exception: print("Exception Occured") return finally: bookCatalog = bookCatalog + current_book pickle.dump(bookCatalog, open(os.getcwd() + "\\books.p", "wb")) utils.printMenuHead("Added book Successfully") self.logger("Added the book {} Successfully".format( current_book["Name"]))