Exemple #1
0
    def test_update_not_accepts_other_values(self):

        db = ModelDataset(user=os.environ.get('MYSQL_USER'),
                          host=os.environ.get('MYSQL_HOST'),
                          password=os.environ.get('MYSQL_PASSWORD'),
                          db='test_key_database')

        with self.assertRaises(pymysql.IntegrityError):
            db.update(species="Anopheles (Anopheles) walkeri",
                      value='not-valid-value',
                      couplet='cp_Mesopostnotum')

        db.connection.close()
Exemple #2
0
    def test_update_accepts_None(self):

        db = ModelDataset(user=os.environ.get('MYSQL_USER'),
                          host=os.environ.get('MYSQL_HOST'),
                          password=os.environ.get('MYSQL_PASSWORD'),
                          db='test_key_database')

        try:
            db.update(species="Anopheles (Anopheles) walkeri",
                      value=None,
                      couplet='cp_Mesopostnotum')
        except pymysql.Error as e:
            self.fail(e)

        db.connection.close()
def main(term):

    # define tabspace (x)
    tab = 8

    # hide cursor
    curses.curs_set(0)

    # init color pairs
    curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
    curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK)

    # keybindings
    CHANGE_PASSWORD = ord('P')
    BULK_UPDATE = ord('B')
    NEXT_C = ord('s')
    PREV_C = ord('a')
    NEXT_S = ord('x')
    PREV_S = ord('z')
    UPDATE = ord('\n')
    CONFIRM = ord('\n')
    QUIT = ord('q')

    # get login vars
    current_login = login(term)

    # clear terminal
    term.clear()

    y, x = wrapstr(term, 2, tab, 'connecting to the database, please wait',
                   curses.A_DIM)
    term.refresh()  # force update screen

    try:  # try to connect to the database (timeout = 10s)
        db = ModelDataset(user=current_login[1].username,
                          host=current_login[1].hostname,
                          password=current_login[1].password,
                          db=current_login[1].path[1:])

        sig = Signal(db)

        # setup signal handler (avoids exiting without closing the connection)
        signal.signal(signal.SIGINT, sig.signal_handler)

    except Exception as e:
        connection_error_handler(term, e)

    # set internal variables
    sp_index = 0
    cp_index = 0

    # main loop
    while True:

        term.clear()

        # read db info for current index
        try:  # try to query the data
            couplet = db.db_couplets[cp_index]
            zero_text, one_text = db.show_couplet(couplet)
            species = db.db_species[sp_index]
            status = db.show_state(species, couplet)
        except Exception as e:
            connection_error_handler(term, e)

        # display actions
        y, x = wrapstr(
            term, 2, tab,
            'actions: ({}) change password    ({}) bulk update'.format(
                readkey(CHANGE_PASSWORD), readkey(BULK_UPDATE)), curses.A_DIM)

        # display db info
        y, x = wrapstr(term, y + 2, tab, 'current couplet: {}'.format(couplet))
        y, x = wrapstr(term, y + 2, tab + 4, '0. {}'.format(zero_text))
        y, x = wrapstr(term, y + 2, tab + 4, '1. {}'.format(one_text))
        y, x = wrapstr(term, y + 2, tab, 'current species: {}'.format(species))
        y, x = wrapstr(term, y + 2, tab + 4, 'status: {}'.format(status))

        # freeze input y pos
        input_y = y + 2

        # display helper text
        y, x = wrapstr(term, y + 7, tab, 'keybindings:', curses.A_DIM)
        y, x = wrapstr(
            term, y + 1, tab,
            '({}) previous couplet  ({}) next couplet      ({}) update'.format(
                readkey(PREV_C), readkey(NEXT_C),
                readkey(UPDATE)), curses.A_DIM)
        y, x = wrapstr(
            term, y + 1, tab,
            '({}) previous species  ({}) next species      ({}) quit'.format(
                readkey(PREV_S), readkey(NEXT_S), readkey(QUIT)), curses.A_DIM)

        # await user input
        curses.flushinp()
        key = term.getch()

        if key == CHANGE_PASSWORD:
            change_current_user_password(term, db, login_name)

        elif key == BULK_UPDATE:
            bulk_update_file = get_bulk_update_file(term, db)
            if os.path.isfile(bulk_update_file) and bulk_update_file.endswith(
                    '.csv'):
                bulk_update(term, db, bulk_update_file)

        elif key == NEXT_C:
            if cp_index < len(db.db_couplets) - 1:
                cp_index += 1

        elif key == PREV_C:
            if cp_index > 0:
                cp_index -= 1

        elif key == NEXT_S:
            if sp_index < len(db.db_species) - 1:
                sp_index += 1

        elif key == PREV_S:
            if sp_index > 0:
                sp_index -= 1

        elif key == UPDATE:
            y, x = wrapstr(term, input_y, tab,
                           'type a new value to edit the database:')

            curses.curs_set(1)
            curses.echo()
            curses.flushinp()
            new_status = term.getstr(y, x + 1).decode('utf-8').upper()
            curses.noecho()
            curses.curs_set(0)

            if new_status.upper() == 'NULL_VALUE':
                new_status = None

            elif new_status == '3':
                new_status = 'NA'

            if new_status in ['0', '1', '01', '10', 'NA', None]:
                y, x = wrapstr(term, y + 1, tab,
                               'new status: {}'.format(new_status),
                               curses.A_BOLD)
                y, x = wrapstr(
                    term, y + 1, tab,
                    "press '{}' to confirm, or any key to cancel".format(
                        readkey(CONFIRM)))

                curses.flushinp()
                confirm = term.getch()
                if confirm == CONFIRM:
                    try:  # try to perform an update:
                        db.update(species, new_status, couplet)
                        y, x = wrapstr(
                            term, y + 1, tab,
                            'change confirmed, press any key to continue',
                            curses.color_pair(2))

                    except Exception as e:
                        connection_error_handler(term, e)

                else:
                    y, x = wrapstr(
                        term, y + 1, tab,
                        'action cancelled, press any key to continue',
                        curses.color_pair(3))

            else:
                y, x = wrapstr(term, y + 1, tab,
                               'illegal status: {}'.format(new_status),
                               curses.color_pair(3))
                y, x = wrapstr(term, y + 1, tab,
                               "please type '0', '1', '01' or 'NA'")

            curses.flushinp()
            term.getch()

        elif key == QUIT:
            break

        elif key == curses.KEY_RESIZE:
            pass  # this deals with resizing the terminal window

    try:  # try to close the connection nicely
        db.connection.close()
    except Exception as e:
        connection_error_handler(term, e)