示例#1
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()
示例#2
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()
示例#3
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()
示例#4
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()
示例#5
0
def save_book(book):
    b = Book(name = book)
    db_session.add(b)
    db_session.commit()
示例#6
0
def save_author(author):
    a = Author(name = author)
    db_session.add(a)
    db_session.commit()
示例#7
0
def add_user(username, password, is_admin = False):
    new_user = User(username, password, is_admin)
    db_session.add(new_user)
    db_session.commit()
示例#8
0
def save_book(book):
    b = Book(name=book)
    db_session.add(b)
    db_session.commit()
示例#9
0
def save_author(author):
    a = Author(name=author)
    db_session.add(a)
    db_session.commit()
示例#10
0
def add_user(username, password, is_admin=False):
    new_user = User(username, password, is_admin)
    db_session.add(new_user)
    db_session.commit()