示例#1
0
def add_contact_to_list():

    name = ""
    name_check = False
    email = None
    phone_number = None
    group_to_add = ""
    groups = []
    groups_finished = False

    print("-------------------------------------------")
    print("\t Add a contact")
    print("-------------------------------------------")

    while not name_check:
        print("\t Please enter a contact name and hit ENTER")
        print("\t", end="")
        name = input('')

        if len(name) == 0:
            print("\t No contact name entered, please try again")
        else:
            name_check = True

    print("\t Please enter a contact email and hit ENTER")
    print("\t [To leave email empty, just hit ENTER]")
    print("\t", end="")
    email = input('')

    print("\t Please enter a contact phone number and hit ENTER")
    print("\t", end="")
    phone_number = input('')

    while not groups_finished:
        print("\t Please enter a group name and hit ENTER")
        print("\t", end="")
        group_to_add = input('')

        if len(group_to_add) == 0:
            print("\t No group name entered, please try again")
        else:
            groups.append(group_to_add)
            print("\t Group added successfully")
            print("\t To add another group, type '1' and hit ENTER, otherwise hit ENTER")
            print("\t", end="")

            add_more = input('')
            if add_more == "1":
                groups_finished = False
            else:
                groups_finished = True

    contact = Contact(name, email, phone_number)
    contact.set_groups(groups)
    add_status = ContactDAO.add_a_contact(contact)

    if add_status:
        return name
    else:
        return ""
    def search_by_name(search_name):
        searchContacts = []
        db = MySqlDb()

        rows = db.query(
            'SELECT c.*, g.group_name FROM contact AS c, contact_group AS g, group_link AS gl WHERE c.contact_id = gl.contact_id AND g.group_id = gl.group_id AND c.name LIKE %s;',
            [search_name + "%"])
        db.close_connection()

        prev_name = ""
        new_contact = None
        groups = None

        for entry in rows:
            if len(prev_name) == 0 or prev_name != entry['name']:
                if len(prev_name) != 0:
                    new_contact.set_groups(groups)
                    searchContacts.append(new_contact)

                name = entry['name']
                email = entry['email']
                phone_number = entry['phone_number']
                group = entry['group_name']

                groups = []
                groups.append(group)

                prev_name = name

                new_contact = Contact(name, email, phone_number)
            else:
                group = entry['group_name']
                groups.append(group)

        if new_contact is not None:
            new_contact.set_groups(groups)
            searchContacts.append(new_contact)

        if len(searchContacts) > 0:
            return searchContacts
        else:
            return None
示例#3
0
def update_a_contact():
    print("-------------------------------------------")
    print("\t Update a contact")
    print("-------------------------------------------")

    contacts = print_all_contacts()

    print("\t Please enter the number of the contact you would like to update and hit ENTER")
    print("\t", end="")

    input_string = input('')

    try:
        selection = int(input_string)
        if selection <= len(contacts) and selection > 0:
            edit_contact = contacts[selection -1]
            print("\t Updating contact: " + edit_contact.get_name())
            print("\t", end="")

            name = ""
            name_check = False
            email = None
            phone_number = None
            group_to_add = ""
            groups = []
            groups_finished = False

            while not name_check:
                print("\t Please enter a contact name and hit ENTER")
                print("\t Just hit ENTER to leave name unchanged")
                print("\t", end="")
                name = input('')

                if len(name) == 0 or name == edit_contact.get_name():
                    print()
                    print("\t\t Name will not be changed")
                    print()
                    name_check = True
                    name = edit_contact.get_name()
                else:
                    print()
                    print("\t\t Name will be changed to: " + name)
                    print()
                    name_check = True

            print("\t Please enter a contact email and hit ENTER")
            print("\t [To leave unchanged, just hit ENTER]")
            print("\t", end="")
            email = input('')

            if len(email) == 0:
                print()
                print("\t\t Email will not be changed")
                print()
                email = edit_contact.get_email()
            else:
                print()
                print("\t\t Email will be changed to: " + email)
                print()

            print("\t Please enter a contact phone number and hit ENTER")
            print("\t [To leave unchanged, just hit ENTER]")
            print("\t", end="")
            phone_number = input('')

            if len(phone_number) == 0:
                print()
                print("\t\t Phone number will not be changed")
                print()
                phone_number = edit_contact.get_phone_number()
            else:
                print()
                print("\t\t Phone number will be changed to: " + phone_number)
                print()

            while not groups_finished:
                print("Current groups are: ")
                print("\t\t", end="")

                for group in edit_contact.get_groups():
                    print("| " + group + " |", end="")
                print()
                print()

                print("\t To add to these, press 1 and hit ENTER")
                print("\t To start with no groups, press 2 and hit ENTER")
                print("\t", end="")

                add_group_input = input("")

                try:
                    add_group_selection = int(add_group_input)
                    if add_group_selection < 3 and add_group_selection > 0:
                        if add_group_selection == 1:
                            groups = edit_contact.get_groups()
                            while not groups_finished:
                                print("\t Please enter a group name and hit ENTER")
                                print("\t", end="")
                                group_to_add = input('')
                                if len(group_to_add) == 0:
                                    print("\t No group name entered, please try again")
                                else:
                                    groups.append(group_to_add)
                                    print("\t Group added successfully")
                                    print("\t To add another group, type '1' and hit ENTER, otherwise hit ENTER")
                                    print("\t", end="")
                                    add_more = input('')
                                    if add_more == "1":
                                        groups_finished = False
                                    else:
                                        groups_finished = True
                        elif add_group_selection == 2:
                            while not groups_finished:
                                print("\t Please enter a group name and hit ENTER")
                                print("\t", end="")
                                group_to_add = input('')
                                if len(group_to_add) == 0:
                                    print("\t No group name entered, please try again")
                                else:
                                    groups.append(group_to_add)
                                    print("\t Group added successfully")
                                    print("\t To add another group, type '1' and hit ENTER, otherwise hit ENTER")
                                    print("\t", end="")
                                    add_more = input('')
                                    if add_more == "1":
                                        groups_finished = False
                                    else:
                                        groups_finished = True
                except:
                    print("\t Invalid input, please select a menu item number. Please try again...")

                contact = Contact(name, email, phone_number)
                contact.set_groups(groups)
                edited_contact_name = ContactDAO.update_a_contact(contact, edit_contact.get_name())

                if edited_contact_name is not None:
                    return edited_contact_name
                else:
                    return None
        else:
            return None
    except:
        return None