remove orphaned authors

This commit is contained in:
Cervinko Cera 2016-04-03 00:31:30 +02:00
parent c6500b7eca
commit a395e9f301

View File

@ -665,11 +665,16 @@ def edit_book(book_id):
to_save = request.form.to_dict() to_save = request.form.to_dict()
book.title = to_save["book_title"] book.title = to_save["book_title"]
author_id = book.authors[0].id
is_author = db.session.query(db.Authors).filter(db.Authors.name.like('%' + to_save["author_name"].strip() + '%')).first() is_author = db.session.query(db.Authors).filter(db.Authors.name.like('%' + to_save["author_name"].strip() + '%')).first()
if book.authors[0].name not in ("Unknown", "Unbekannt", "", " "): if book.authors[0].name not in ("Unknown", "Unbekannt", "", " "):
if is_author: if is_author:
book.authors.append(is_author) book.authors.append(is_author)
book.authors.remove(db.session.query(db.Authors).get(book.authors[0].id)) book.authors.remove(db.session.query(db.Authors).get(book.authors[0].id))
authors_books_count = db.session.query(db.Books).filter(db.Books.authors.any(db.Authors.id.is_(author_id))).count()
if authors_books_count == 0:
db.session.query(db.Authors).filter(db.Authors.id == author_id).delete()
else: else:
book.authors[0].name = to_save["author_name"].strip() book.authors[0].name = to_save["author_name"].strip()
else: else: