Ejemplo n.º 1
0
def delete_items(table_name, **name):
    with session_scope() as s:
        keys = get_all_keys(table_name)
        for key, value in name.items():
            f = False

            for item in keys:
                if key == item:
                    f = True
                    d = getattr(table_name, key).type
                    if type(d) is Integer:
                        d = 0
                    elif type(d) is Boolean:
                        d = False
                    elif type(d) is Date:
                        d = DT.datetime.strptime('2020,1,1', '%Y,%m,%d').date()
                    elif type(d) is String:
                        d = 'str'
                    if type(d) != type(value):
                        raise mvc_exc.ValueTypeError(f"Value: <{value}> must be the same type as key!")

            if f is False:
                raise mvc_exc.KeyNameError(f"Key: <{key}> doesn't exist in table '{table_name.__tablename__}'!")

        try:
            delete_it = s.query(table_name).filter_by(**name).one()
        except Exception:
            raise mvc_exc.ItemNotStored(f"Can't delete {name.items()} because it is not stored")

        s.delete(delete_it)
Ejemplo n.º 2
0
def delete_items(conn, table_name, **name):
    cur = conn.cursor()

    cur.execute(
        f"SELECT column_name, data_type FROM information_schema.columns "
        f"WHERE table_schema='public' AND table_name = '{table_name}'")
    data_type = cur.fetchall()

    type_dat = []
    for item in data_type:
        type_dat += [item[1]]

    j = 0
    for item in type_dat:
        if type_dat[j] == 'integer':
            type_dat[j] = 1
        if type_dat[j] == 'boolean':
            type_dat[j] = False
        if type_dat[j] == 'date':
            type_dat[j] = DT.datetime.strptime('2020,1,1', '%Y,%m,%d').date()
        j = j + 1

    f = False
    for key, value in name.items():
        i = 0
        for item in data_type:
            if item[0] == key:
                f = True
                if type(type_dat[i]) != type(value):
                    raise mvc_exc.ValueTypeError(
                        f"Type of argument {value} is not the same as key type"
                    )
            i = i + 1

        if f is False:
            raise mvc_exc.KeyNameError(f"Key name {key} doesn't exist")

        if type(value) is (int or bool):
            cur.execute(f"SELECT * FROM {table_name} WHERE {key} = {value}")
            if cur.rowcount == 0:
                raise mvc_exc.ItemNotStored(
                    'Can\'t delete "{}" because it\'s not stored'.format(name))
            else:
                cur.execute(f"DELETE FROM {table_name} WHERE {key} = {value}")
        else:
            cur.execute(f"SELECT * FROM {table_name} WHERE {key} = '{value}'")
            if cur.rowcount == 0:
                raise mvc_exc.ItemNotStored(
                    'Can\'t delete "{}" because it\'s not stored'.format(name))
            else:
                cur.execute(
                    f"DELETE FROM {table_name} WHERE {key} = '{value}'")

    conn.commit()
    cur.close()
    return conn