Пример #1
0
def del_author(author_name):
    #SELECT ALL BOOK_ID FROM MATCHES WHERE AUTHOR_ID = HASH(AUTHOR_NAME)
    author_id = db_session.query(Author.id).filter_by(name = author_name).first()
    books_ids = db_session.query(Matches.book_id).filter_by(author_id = author_id[0]).all()
    #FROM LIST OF TUPLES CREATE LIST OF BOOK_IDS
    books_ids = [each[0] for each in books_ids]
    #DELETE FROM MATCHES WHERE BOOK_ID IN BOOK_IDS
    Matches.query.filter(Matches.book_id.in_((books_ids))).delete(synchronize_session='fetch')
    Book.query.filter(Book.id.in_((books_ids))).delete(synchronize_session='fetch')
    Author.query.filter(Author.id == author_id[0]).delete(synchronize_session='fetch')
    db_session.commit()
Пример #2
0
def edit(prev_name, new_name):
    is_book = db_session.query(Book.id).filter(Book.name == prev_name).first()
    is_author = db_session.query(Author.id).filter(Author.name == prev_name).first()
    if is_author:
        is_author = is_author[0]
        check_new_name_id = db_session.query(Author.id).filter(func.lower(Author.name) == func.lower(new_name)).first()
        if check_new_name_id:
            check_new_name_id = check_new_name_id[0]
            prev_author_book_id = db_session.query(Matches.book_id).filter_by(author_id = is_author).all()
            prev_author_book_id = [each[0] for each in prev_author_book_id]
            for each_book in prev_author_book_id:
                m = Matches(author_id=check_new_name_id, book_id = each_book)
                db_session.add(m)
                db_session.commit()
            Matches.query.filter(Matches.author_id == is_author).delete(synchronize_session='fetch')
            Author.query.filter(Author.id == is_author).delete(synchronize_session='fetch')
            db_session.commit()
        else:
            prev_author_id = db_session.query(Author.id).filter_by(name = prev_name).first()
            prev_author_id = prev_author_id[0]
            db_session.query(Author).filter_by(id = prev_author_id).\
            update({"name":new_name}, synchronize_session='fetch')
            db_session.commit()
    elif is_book:
        check_new_name_id = db_session.query(Book.id).filter(func.lower(Book.name) == func.lower(new_name)).first()
        if check_new_name_id:
            return False
        else:
            prev_book_id = db_session.query(Book.id).filter_by(name = prev_name).first()
            prev_book_id = prev_book_id[0]
            db_session.query(Book).filter_by(id = prev_book_id).\
            update({"name":new_name}, synchronize_session='fetch')
            db_session.commit()
Пример #3
0
def del_book(book_name):
    book_id = db_session.query(Book.id).filter_by(name = book_name).first()
    author_id = db_session.query(Matches.author_id).filter(Matches.book_id == book_id[0]).all()
    author_id = [each[0] for each in author_id][0]
    author_books = db_session.query(Matches.book_id).filter_by(author_id = author_id).all()
    author_books = [each[0] for each in author_books]
    if len(author_books) == 1:
        author_name = db_session.query(Author.name).filter(Author.id == author_id).all()
        author_name = [each[0] for each in author_name][0]
        del_author(author_name)
    elif len(author_books) > 1:
        Matches.query.filter(Matches.book_id == book_id[0]).delete(synchronize_session='fetch')
        Book.query.filter(Book.name == book_name).delete(synchronize_session='fetch')
        db_session.commit()
Пример #4
0
def del_author(author_name):
    #SELECT ALL BOOK_ID FROM MATCHES WHERE AUTHOR_ID = HASH(AUTHOR_NAME)
    author_id = db_session.query(Author.id).filter_by(name=author_name).first()
    books_ids = db_session.query(
        Matches.book_id).filter_by(author_id=author_id[0]).all()
    #FROM LIST OF TUPLES CREATE LIST OF BOOK_IDS
    books_ids = [each[0] for each in books_ids]
    #DELETE FROM MATCHES WHERE BOOK_ID IN BOOK_IDS
    Matches.query.filter(Matches.book_id.in_(
        (books_ids))).delete(synchronize_session='fetch')
    Book.query.filter(Book.id.in_(
        (books_ids))).delete(synchronize_session='fetch')
    Author.query.filter(Author.id == author_id[0]).delete(
        synchronize_session='fetch')
    db_session.commit()
Пример #5
0
def save_author_book(author, book):
    author_id = db_session.query(Author.id).filter(func.lower(Author.name) == func.lower(author)).first()
    """check if author is already present"""
    if author_id:
        save_book(book)
        book_id = db_session.query(Book.id).filter(Book.name == book).first()
        m = Matches(author_id=author_id[0], book_id=book_id[0])
        db_session.add(m)
        db_session.commit()
    else:
        save_author(author)
        save_book(book)
        author_id = db_session.query(Author.id).filter(Author.name == author).first()
        book_id = db_session.query(Book.id).filter(Book.name == book).first()
        m = Matches(author_id=author_id[0], book_id=book_id[0] )
        db_session.add(m)
        db_session.commit()
Пример #6
0
def save_author_book(author, book):
    author_id = db_session.query(Author.id).filter(
        func.lower(Author.name) == func.lower(author)).first()
    """check if author is already present"""
    if author_id:
        save_book(book)
        book_id = db_session.query(Book.id).filter(Book.name == book).first()
        m = Matches(author_id=author_id[0], book_id=book_id[0])
        db_session.add(m)
        db_session.commit()
    else:
        save_author(author)
        save_book(book)
        author_id = db_session.query(
            Author.id).filter(Author.name == author).first()
        book_id = db_session.query(Book.id).filter(Book.name == book).first()
        m = Matches(author_id=author_id[0], book_id=book_id[0])
        db_session.add(m)
        db_session.commit()
Пример #7
0
def del_book(book_name):
    book_id = db_session.query(Book.id).filter_by(name=book_name).first()
    author_id = db_session.query(
        Matches.author_id).filter(Matches.book_id == book_id[0]).all()
    author_id = [each[0] for each in author_id][0]
    author_books = db_session.query(
        Matches.book_id).filter_by(author_id=author_id).all()
    author_books = [each[0] for each in author_books]
    if len(author_books) == 1:
        author_name = db_session.query(
            Author.name).filter(Author.id == author_id).all()
        author_name = [each[0] for each in author_name][0]
        del_author(author_name)
    elif len(author_books) > 1:
        Matches.query.filter(Matches.book_id == book_id[0]).delete(
            synchronize_session='fetch')
        Book.query.filter(Book.name == book_name).delete(
            synchronize_session='fetch')
        db_session.commit()
Пример #8
0
def edit(prev_name, new_name):
    is_book = db_session.query(Book.id).filter(Book.name == prev_name).first()
    is_author = db_session.query(
        Author.id).filter(Author.name == prev_name).first()
    if is_author:
        is_author = is_author[0]
        check_new_name_id = db_session.query(Author.id).filter(
            func.lower(Author.name) == func.lower(new_name)).first()
        if check_new_name_id:
            check_new_name_id = check_new_name_id[0]
            prev_author_book_id = db_session.query(
                Matches.book_id).filter_by(author_id=is_author).all()
            prev_author_book_id = [each[0] for each in prev_author_book_id]
            for each_book in prev_author_book_id:
                m = Matches(author_id=check_new_name_id, book_id=each_book)
                db_session.add(m)
                db_session.commit()
            Matches.query.filter(Matches.author_id == is_author).delete(
                synchronize_session='fetch')
            Author.query.filter(Author.id == is_author).delete(
                synchronize_session='fetch')
            db_session.commit()
        else:
            prev_author_id = db_session.query(
                Author.id).filter_by(name=prev_name).first()
            prev_author_id = prev_author_id[0]
            db_session.query(Author).filter_by(id = prev_author_id).\
            update({"name":new_name}, synchronize_session='fetch')
            db_session.commit()
    elif is_book:
        check_new_name_id = db_session.query(Book.id).filter(
            func.lower(Book.name) == func.lower(new_name)).first()
        if check_new_name_id:
            return False
        else:
            prev_book_id = db_session.query(
                Book.id).filter_by(name=prev_name).first()
            prev_book_id = prev_book_id[0]
            db_session.query(Book).filter_by(id = prev_book_id).\
            update({"name":new_name}, synchronize_session='fetch')
            db_session.commit()
Пример #9
0
def save_book(book):
    b = Book(name = book)
    db_session.add(b)
    db_session.commit()
Пример #10
0
def save_author(author):
    a = Author(name = author)
    db_session.add(a)
    db_session.commit()
Пример #11
0
def add_user(username, password, is_admin = False):
    new_user = User(username, password, is_admin)
    db_session.add(new_user)
    db_session.commit()
Пример #12
0
def save_book(book):
    b = Book(name=book)
    db_session.add(b)
    db_session.commit()
Пример #13
0
def save_author(author):
    a = Author(name=author)
    db_session.add(a)
    db_session.commit()
Пример #14
0
def add_user(username, password, is_admin=False):
    new_user = User(username, password, is_admin)
    db_session.add(new_user)
    db_session.commit()