def getUser(search=None, confirm=True): search = str(search) if search else None while True: if search == None: search = getInput("Enter E-mail or userID") elif search.isdigit(): user = backend.getUserByUserID(search) break elif validateEmail(search): user = backend.getUserByEmail(search) break else: putMessage("Invalid search criteria.", level=severity.ERROR) search = None if user == None: putMessage("User not found. Confirm info and try again.", level=severity.WARNING) else: putMessage("Found user [{:d}] '{:s} {:s}'".format( user['userID'], user['firstName'], user['lastName'])) if confirm: confirmUser = getInput("Use this person?", options=['y', 'n']) if not confirmUser == 'y': return None return user
def getUser(search=None, confirm=True): search = str(search) if search else None while True: if search == None: search = getInput("Enter E-mail or userID") elif search.isdigit(): user = backend.getUserByUserID(search) break elif validateEmail(search): user = backend.getUserByEmail(search) break else: putMessage("Invalid search criteria.", level=severity.ERROR) search = None if user == None: putMessage("User not found. Confirm info and try again.", level=severity.WARNING) else: putMessage("Found user [{:d}] '{:s} {:s}'".format(user['userID'], user['firstName'], user['lastName'])) if confirm: confirmUser = getInput("Use this person?", options=['y','n'] ) if not confirmUser == 'y': return None return user
def editUser(userID=None, email=None, firstName=None, lastName=None, status=None, tags=None, password=None): if userID or email: userSearch = userID if userID else email user = getUser(userSearch, confirm=False) else: user = None if user: putMessage("Editing user [{:d}] '{:s} {:s}'".format( user['userID'], user['firstName'], user['lastName']), level=severity.WARNING) putMessage("Enter new information to change.") putMessage("Leave blank to leave stored info unchanged.") mode = 'edit' elif email or (not email and not userID): putMessage("Adding new user") mode = 'add' else: return while email == None: email = getInput("e-mail", user['email'] if user else '') if email == None and mode == "edit": break if email == '-': message = ("Not a valid e-mail address" if mode == "add" else "Cannot delete e-mail; edit instead.") putMessage(message, level=severity.ERROR) email = None elif not validateEmail(email): putMessage("Not a valid e-mail address", level=severity.ERROR) email = None else: if backend.getUserByEmail(email): putMessage("e-mail address is already in use!", level=severity.ERROR) email = None while firstName == None: firstName = firstName if firstName else getInput( "First Name", user['firstName'] if user else '') if firstName == None and mode == 'edit': break elif firstName == None and mode == 'add': putMessage("This field is required", level=severity.ERROR) while lastName == None: lastName = lastName if lastName else getInput( "Last Name", user['lastName'] if user else '') if lastName == None and mode == 'edit': break elif lastName == None and mode == 'add': putMessage("This field is required", level=severity.ERROR) while status == None: status = getInput("Status", user['status'] if user else '') if status == None: break if status == '-': putMessage("Cannot delete status; edit instead.", level=severity.ERROR) status = None elif status not in validStatuses: putMessage("Invalid status '{:s}'".format(status), level=severity.ERROR) status = None while tags == None: putMessage("Enter '-' to delete stored tags.") userInput = getInput("Tags", ", ".join(user['tags']) if user else '') if userInput == None: break if userInput == '-': tags = '' break tags = [x.strip() for x in userInput.split(',') if not x == ''] if not tags: tags = None continue for tag in tags: if tag not in validTags: putMessage("Invalid tag '{:s}'".format(tag), level=severity.ERROR) tags = None if not password: putMessage("Enter '-' to delete stored password.") password = getInput("Password", password=True) if password: confirmPassword = getInput("Confirm password", password=True) while password != confirmPassword: putMessage("Passwords do not match", level=severity.ERROR) password = getInput("Password", password=True) confirmPassword = getInput("Confirm password", password=True) password = '' if password == '-' else password if password == '': putMessage("Password will be removed!", level=severity.WARNING) if mode == 'add': userID = backend.addUser(email, firstName, lastName, password, tags) user = backend.getUserByUserID(userID) if userID != None: putMessage("User [{:d}] added to the database".format(userID), level=severity.OK) return 0 else: putMessage("Failed to add user", level=severity.ERROR) return 1 else: backend.updateUser(user['userID'], email=email, firstName=firstName, lastName=lastName, status=status, tags=tags, password=password) putMessage("Information for user [{:d}] has been updated".format( user['userID']), level=severity.OK)
def editUser(userID=None, email=None, firstName=None, lastName=None, status=None, tags=None, password=None): if userID or email: userSearch = userID if userID else email user = getUser(userSearch, confirm=False) else: user = None if user: putMessage("Editing user [{:d}] '{:s} {:s}'".format( user['userID'], user['firstName'], user['lastName']), level=severity.WARNING) putMessage("Enter new information to change.") putMessage("Leave blank to leave stored info unchanged.") mode = 'edit' elif email or (not email and not userID): putMessage("Adding new user") mode = 'add' else: return while email == None: email = getInput("e-mail", user['email'] if user else '') if email == None and mode == "edit": break if email == '-': message = ("Not a valid e-mail address" if mode == "add" else "Cannot delete e-mail; edit instead.") putMessage(message, level=severity.ERROR) email = None elif not validateEmail(email): putMessage("Not a valid e-mail address", level=severity.ERROR) email = None else: if backend.getUserByEmail(email): putMessage("e-mail address is already in use!", level=severity.ERROR) email = None while firstName == None: firstName = firstName if firstName else getInput("First Name", user['firstName'] if user else '') if firstName == None and mode == 'edit': break elif firstName == None and mode == 'add': putMessage("This field is required", level=severity.ERROR) while lastName == None: lastName = lastName if lastName else getInput("Last Name", user['lastName'] if user else '') if lastName == None and mode == 'edit': break elif lastName == None and mode == 'add': putMessage("This field is required", level=severity.ERROR) while status == None: status = getInput("Status", user['status'] if user else '') if status == None: break if status == '-': putMessage("Cannot delete status; edit instead.", level=severity.ERROR) status = None elif status not in validStatuses: putMessage("Invalid status '{:s}'".format(status), level=severity.ERROR) status = None while tags == None: putMessage("Enter '-' to delete stored tags.") userInput = getInput("Tags", ", ".join(user['tags']) if user else '') if userInput == None: break if userInput == '-': tags = '' break tags = [x.strip() for x in userInput.split(',') if not x == ''] if not tags: tags = None continue for tag in tags: if tag not in validTags: putMessage("Invalid tag '{:s}'".format(tag), level=severity.ERROR) tags = None if not password: putMessage("Enter '-' to delete stored password.") password = getInput("Password", password=True) if password: confirmPassword = getInput("Confirm password", password = True) while password != confirmPassword: putMessage("Passwords do not match", level=severity.ERROR) password = getInput("Password", password =True) confirmPassword = getInput("Confirm password", password = True) password = '' if password == '-' else password if password == '': putMessage("Password will be removed!", level=severity.WARNING) if mode == 'add': userID = backend.addUser(email, firstName, lastName, password,tags) user = backend.getUserByUserID(userID) if userID != None: putMessage("User [{:d}] added to the database".format(userID), level=severity.OK) return 0 else: putMessage("Failed to add user", level=severity.ERROR) return 1 else: backend.updateUser(user['userID'], email=email, firstName=firstName, lastName=lastName, status=status, tags=tags, password=password) putMessage("Information for user [{:d}] has been updated".format(user['userID']), level= severity.OK)