def search(searchq): # Will only fail if some f*cker names their book after their own ISBN. results = BookRecord.base_assembler_query() if ISBN_REGEX.match(searchq): alt_isbn = make_equivalent_isbn(searchq) results = results.filter( or_(Book.isbn == searchq, Book.isbn == alt_isbn)).all() else: contrib_query = (db.session.query( Book.id).filter(BookContribution.book_id == Book.id).filter( BookContribution.contributor_id == Contributor.id).filter( func.concat(Contributor.firstname, ' ', Contributor.lastname).ilike("".join( ("%", searchq, "%")))).all()) contribooks = [bid for bid, in contrib_query] results = (BookRecord.base_assembler_query().filter( or_( Book.title.ilike("".join(("%", searchq, "%"))), and_(Book.publisher_id == BookCompany.id, BookCompany.name.ilike("".join(("%", searchq, "%")))), Book.id.in_(contribooks) if contribooks else False)).all()) book_listing = BookRecord.assembler(results, as_obj=False) return book_listing
def edit_books(): form = EditBookForm() book_id = request.args.get("bid") if not book_id: return flask.abort(400) book_query = BookRecord.base_assembler_query().filter(Book.id == book_id) query_results = book_query.all() assembled = BookRecord.assembler(query_results) if not assembled: return flask.abort(400) elif len(assembled) > 1: raise InvalidRecordState("book id %s" % book_id) book = assembled[0] book_js = "var bookForEditing = JSON.parse(%s)" % json.dumps( json.dumps(book.__dict__)) scripts = [ "jquery.validate.min.js", "jquery.form.min.js", "Queue.js", "types/book-details.js", "edit-book/main.js", "edit-book/controller.js", "utils/visual-queue.js", "utils/misc.js", "utils/isbn-verify.js", "jquery-ui.min.js", "lodash.js", "alertify.min.js", "types/person.js" ] if app.config["DEVEL"]: scripts.insert(0, "add-books/testdata.js") styles = ("add_books.css", "jquery-ui.min.css", "jquery-ui.structure.min.css", "jquery-ui.theme.min.css", "alertify.css", "alertify-default-theme.css") return render_template("edit-book.jinja", form=form, scripts=scripts, stylesheets=styles, misc_js=book_js, book_title=book.title)
def test_assembler(self): # Create the DB records booka = BookFactory() booka_author = BookContributionFactory( role=Role.get_preset_role("Author"), book=booka, creator=self.admin_user) librarian.db.session.add(booka_author) booka_translator = BookContributionFactory( role=Role.get_preset_role("Translator"), book=booka, creator=self.admin_user) librarian.db.session.add(booka_translator) illustrator_id = Role.get_preset_role_id("Illustrator") booka_illus1 = BookContributionFactory( role=Role.query.filter_by(id=illustrator_id).first(), book=booka, creator=self.admin_user) librarian.db.session.add(booka_illus1) librarian.db.session.flush() booka_illus2 = BookContributionFactory( role=Role.query.filter_by(id=illustrator_id).first(), book=booka, creator=self.admin_user) librarian.db.session.add(booka_illus2) librarian.db.session.commit() bookb = BookFactory() bookb_author = BookContributionFactory( role=Role.get_preset_role("Author"), book=bookb, creator=self.admin_user) bookb_translator = BookContributionFactory( role=Role.get_preset_role("Translator"), book=bookb, creator=self.admin_user) bookb_illus = BookContributionFactory( role=Role.get_preset_role("Illustrator"), book=bookb, creator=self.admin_user) self.session_add_all((bookb_author, bookb_translator, bookb_illus)) librarian.db.session.flush() # Create the BookRecord objects booka_authors = [booka_author.contributor.make_plain_person()] booka_translators = [booka_translator.contributor.make_plain_person()] booka_illustrators = [ booka_illus1.contributor.make_plain_person(), booka_illus2.contributor.make_plain_person() ] booka_record = BookRecord(isbn=booka.isbn, title=booka.title, publisher=booka.publisher.name, author=booka_authors, translator=booka_translators, illustrator=booka_illustrators, id=booka.id, genre=booka.genre.name) bookb_authors = [bookb_author.contributor.make_plain_person()] bookb_translators = [bookb_translator.contributor.make_plain_person()] bookb_illustrators = [bookb_illus.contributor.make_plain_person()] bookb_record = BookRecord(isbn=bookb.isbn, title=bookb.title, publisher=bookb.publisher.name, author=bookb_authors, translator=bookb_translators, illustrator=bookb_illustrators, id=bookb.id, genre=bookb.genre.name) expected_records = [booka_record, bookb_record] librarian.db.session.flush() books = BookRecord.base_assembler_query().all() assembled = BookRecord.assembler(books) self.assertEqual(set(expected_records), set(BookRecord.assembler(books)))