From a395e9f301f2dffc15b0d600b3c17f19c277a2cc Mon Sep 17 00:00:00 2001 From: Cervinko Cera Date: Sun, 3 Apr 2016 00:31:30 +0200 Subject: [PATCH] remove orphaned authors --- cps/web.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cps/web.py b/cps/web.py index 082eb3ca..5b459608 100755 --- a/cps/web.py +++ b/cps/web.py @@ -665,11 +665,16 @@ def edit_book(book_id): to_save = request.form.to_dict() 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() if book.authors[0].name not in ("Unknown", "Unbekannt", "", " "): if is_author: book.authors.append(is_author) 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: book.authors[0].name = to_save["author_name"].strip() else: