Ejemplo n.º 1
0
def update_one(mydb, item, table_name):
    """Summary of update_one.

    Args:
        mydb
        item
        table_name

    Raises:
        mvc_exc: ItemNotStored
    """
    table_name = scrub(table_name)
    table = mydb.table(table_name)
    if table_name == PLAYER:
        if not table.update(
            {
                'first_name': item.first_name,
                'last_name': item.last_name,
                'birth_date': item.birth_date,
                'gender': item.gender,
                'rank': item.rank,
                'match_score': item.match_score,
                'current_score': item.current_score,
                'opponents': item.opponents
            }, ((where('first_name') == item.first_name) &
                (where('last_name') == item.last_name))):
            raise mvc_exc.ItemNotStored(
                'Can\'t update "{}" because it\'s not stored '
                'in the table "{}"'.format(
                    item.first_name + ' ' + item.last_name, table_name))
    else:
        rounds = []
        for roun in item.rounds:
            rounds.append(json.loads(roun.to_json()))
        players = []
        for play in item.players:
            players.append(json.loads(play.to_json()))
        if not table.update(
            {
                'name': item.name,
                'place': item.place,
                'date': item.date,
                'round_count': item.round_count,
                'rounds': rounds,
                'players': players,
                'time_control': item.time_control,
                'description': item.description
            },
                where('name') == item.name):
            raise mvc_exc.ItemNotStored(
                'Can\'t update "{}" because it\'s not stored '
                'in the table "{}"'.format(item.name, table_name))
Ejemplo n.º 2
0
def select_one(item_name, table_name):
    result = products_collection.find_one({'name': item_name})
    if result is not None:
        return result
    else:
        raise mvc_exc.ItemNotStored(
            f"Can't read '{item_name}' because it's not stored in table '{table_name}'"
        )
Ejemplo n.º 3
0
def delete_one(name, table_name):
    result = products_collection.find_one({'name': name})
    if result is not None:
        products_collection.delete_one({'name': name})
    else:
        raise mvc_exc.ItemNotStored(
            f"Can't delete '{name}' because it's not stored in table '{table_name}'"
        )
Ejemplo n.º 4
0
def select_one(conn, item_name, table_name):
    product = conn.query(Product).filter(Product.name == item_name).first()

    if product is not None:
        return product.to_dict()
    else:
        raise mvc_exc.ItemNotStored(
            f"Can't read '{item_name}' because it's not stored in table '{table_name}'")
Ejemplo n.º 5
0
def delete_one(conn, name, table_name):
    product = conn.query(Product).filter(Product.name == name).first()

    if product is not None:
        conn.delete(product)
        conn.commit()
    else:
        raise mvc_exc.ItemNotStored(
            f"Can't delete '{name}' because it's not stored in table '{table_name}'")
Ejemplo n.º 6
0
def delete_one(conn, item_name, table_name):
    table = conn.load_table(table_name)
    row = table.find_one(name=item_name)
    if row is not None:
        table.delete(name=item_name)
    else:
        raise mvc_exc.ItemNotStored(
            f"Can't delete '{item_name}' because it's not stored in table '{table.table.name}'"
        )
Ejemplo n.º 7
0
def select_one(conn, item_name, table_name):
    result = conn.execute(
        select([products]).where(products.c.name == item_name)).first()
    if result is not None:
        return tuple_to_dict(result)
    else:
        raise mvc_exc.ItemNotStored(
            f"Can't read '{item_name}' because it's not stored in table '{table_name}'"
        )
Ejemplo n.º 8
0
def select_one(conn, name, table_name):
    table = conn.load_table(table_name)
    row = table.find_one(name=name)
    if row is not None:
        return dict(row)
    else:
        raise mvc_exc.ItemNotStored(
            f"Can't read '{name}' because it's not stored in '{table.table.name}'"
        )
Ejemplo n.º 9
0
def delete_one(conn, name, table_name):
    result = conn.execute(select([products
                                  ]).where(products.c.name == name)).first()
    if result is not None:
        conn.execute(delete(products).where(products.c.name == name))
    else:
        raise mvc_exc.ItemNotStored(
            f"Can't delete '{name}' because it's not stored in table '{table_name}'"
        )
Ejemplo n.º 10
0
def update_one(conn, name, price, quantity, table_name):
    table = conn.load_table(table_name)
    row = table.find_one(name=name)
    if row is not None:
        item = {'name': name, 'price': price, 'quantity': quantity}
        table.update(item, keys=['name'])
    else:
        raise mvc_exc.ItemNotStored(
            f"Can't update '{name}' because it's not stored in  table '{table.table.name}'"
        )
Ejemplo n.º 11
0
def update_one(conn, name, price, quantity, table_name):
    product = conn.query(Product).filter(Product.name == name).first()

    if product is not None:
        product.price = price
        product.quantity = quantity
        conn.commit()
    else:
        raise mvc_exc.ItemNotStored(
            f"Can't update '{name}' because it's not stored in table '{table_name}'")
Ejemplo n.º 12
0
def update_one(conn, name, price, quantity, table_name):
    result = conn.execute(select([products
                                  ]).where(products.c.name == name)).first()
    if result is not None:
        conn.execute(
            update(products).where(products.c.name == name).values(
                price=price, quantity=quantity))
    else:
        raise mvc_exc.ItemNotStored(
            f"Can't update '{name}' because it's not stored in table '{table_name}'"
        )
Ejemplo n.º 13
0
def select_one(conn, item_name, table_name):
    table_name = scrub(table_name)
    item_name = scrub(item_name)
    sql = f'SELECT * FROM {table_name} WHERE name="{item_name}"'
    c = conn.execute(sql)
    result = c.fetchone()
    if result is not None:
        return tuple_to_dict(result)
    else:
        raise mvc_exc.ItemNotStored(
            f"Can't read '{item_name}' because it's not stored in table '{table_name}'"
        )
Ejemplo n.º 14
0
def update_one(name, price, quantity, table_name):
    result = products_collection.find_one({'name': name})
    if result is not None:
        products_collection.update(
            {'name': name}, {'$set': {
                'price': price,
                'quantity': quantity
            }})
    else:
        raise mvc_exc.ItemNotStored(
            f"Can't update '{name}' because it's not stored in table '{table_name}'"
        )
Ejemplo n.º 15
0
def delete_one(conn, name, table_name):
    table_name = scrub(table_name)
    sql_check = f'SELECT EXISTS(SELECT 1 FROM {table_name} WHERE name=? LIMIT 1)'
    sql_delete = f'DELETE FROM {table_name} WHERE name=?'
    c = conn.execute(sql_check, (name, ))
    result = c.fetchone()
    if result[0]:
        c.execute(sql_delete, (name, ))
        conn.commit()
    else:
        raise mvc_exc.ItemNotStored(
            f"Can't delete '{name}' because it's not stored in table '{table_name}''"
        )
Ejemplo n.º 16
0
def update_one(conn, name, price, quantity, table_name):
    table_name = scrub(table_name)
    sql_check = f'SELECT EXISTS(SELECT 1 FROM {table_name} WHERE name=? LIMIT 1)'
    sql_update = f'UPDATE {table_name} SET price=?, quantity=? WHERE name=?'
    c = conn.execute(sql_check, (name, ))
    result = c.fetchone()
    if result[0]:
        c.execute(sql_update, (price, quantity, name))
        conn.commit()
    else:
        raise mvc_exc.ItemNotStored(
            f"Can't update '{name}' because it's not stored in table '{table_name}'"
        )
Ejemplo n.º 17
0
def read_item(name):
    global items
    myitems = list(
        filter(
            lambda x: x['name'] == name,
            items
        )
    )
    if myitems:
        return myitems[0]
    else:
        raise mvc_exc.ItemNotStored(
            f"Can't read '{name}' because it's not stored")
Ejemplo n.º 18
0
def delete_item(name):
    global items
    idx_items = list(
        filter(
            lambda i_x: i_x[1]['name'] == name,
            enumerate(items)
        )
    )
    if idx_items:
        i, item_to_delete = idx_items[0][0], idx_items[0][1]
        del items[i]
    else:
        raise mvc_exc.ItemNotStored(
            f"Can't delete '{name}' because it's not stored")
Ejemplo n.º 19
0
def update_item(name, price, quantity):
    global items
    idx_items = list(
        filter(
            lambda i_x: i_x[1]['name'] == name,
            enumerate(items)
        )
    )
    if idx_items:
        i, item_to_update = idx_items[0][0], idx_items[0][1]
        items[i] = {'name': name, 'price': price, 'quantity': quantity}
    else:
        raise mvc_exc.ItemNotStored(
            f"Can't update '{name}' because it's not stored")
Ejemplo n.º 20
0
def select_one_by_rank(mydb, item_rank, table_name):
    """Summary of select_one by rank.

    Args:
        mydb
        item_rank
        table_name

    Raises:
        mvc_exc: ItemNotStored
    """
    table_name = scrub(table_name)
    table = mydb.table(table_name)
    item = table.search(where('rank') == item_rank)
    if item:
        return item[0]
    raise mvc_exc.ItemNotStored(
        'Can\'t read "{}" because it\'s not stored in table "{}"'.format(
            item_rank, table_name))
Ejemplo n.º 21
0
def select_one(mydb, item_name, table_name):
    """Summary of select_one.

    Args:
        mydb
        item_name
        table_name

    Raises:
        mvc_exc: ItemNotStored
    """
    table_name = scrub(table_name)
    table = mydb.table(table_name)
    if table_name == PLAYER:
        item = table.search((where('first_name') == item_name.split(' ')[0])
                            & (where('last_name') == item_name.split(' ')[1]))
    else:
        item = table.search(where('name') == item_name)
    if item:
        return item[0]
    raise mvc_exc.ItemNotStored(
        'Can\'t read "{}" because it\'s not stored in table "{}"'.format(
            item_name, table_name))