Ejemplo n.º 1
0
def delete_exercise(user_id, exercise_id):
    """
    Submit a request to have an exercise deleted.
    :param exercise_id: ID of the exercise we're requesting to have deleted
    :return: Nothing.
    """
    conn = eng.connect()

    exercise_parm = bindparam("exercise_id", type_=Integer)
    user_parm = bindparam("user_id", type_=String)

    query = select([exercise_table.c.id]).where(
        and_(exercise_table.c.id == exercise_parm, exercise_table.c.user_id == user_parm)
    )

    is_valid_user = conn.execute(query, exercise_id=exercise_id, user_id=user_id).fetchone()

    if is_valid_user:
        with conn.begin() as trans:
            query = attempt_table.delete().where(attempt_table.c.exercise_id == exercise_parm)
            conn.execute(query, exercise_id=exercise_id)

            query = resource_by_exercise_table.delete().where(resource_by_exercise_table.c.exercise_id == exercise_parm)
            conn.execute(query, exercise_id=exercise_id)

            query = exercise_table.delete().where(exercise_table.c.id == exercise_parm)
            conn.execute(query, exercise_id=exercise_id)
            trans.commit()

        msg = "Executed deleteion query on exercise: {} belonging to user: {}".format(exercise_id, user_id)
    else:
        msg = "User: {} not the owner of exercise: {}".format(user_id, exercise_id)

    conn.close()
    return msg
Ejemplo n.º 2
0
def delete_resource(user_id, resource_id):
    conn = eng.connect()

    user_parm = bindparam("user_id", type_=String)
    resource_parm = bindparam("resource_id", type_=Integer)

    query = select([resource_table.c.id]).where(
        and_(resource_table.c.id == resource_parm, resource_table.c.user_id == user_parm)
    )

    is_valid_user = conn.execute(query, user_id=user_id, resource_id=resource_id).fetchone()

    if is_valid_user:
        with conn.begin() as trans:
            query = resource_by_exercise_table.delete().where(resource_by_exercise_table.c.resource_id == resource_parm)
            conn.execute(query, resource_id=resource_id)

            query = resource_table.delete().where(resource_table.c.id == resource_parm)
            conn.execute(query, resource_id=resource_id)
            trans.commit()

    conn.close()
    return "FINISHED"