def search(): form = SearchForm() if request.method == 'POST' and form.validate_on_submit(): # call helper function to check if the user put ISBN into search user_provides_isbn = is_isbn_code(form.search.data) if user_provides_isbn: return redirect(url_for('book', book_isbn=user_provides_isbn)) # search for authors, and books else: s_q_authors = db.execute( 'SELECT *, ' 'public.author_gr_map.author_id_gr, ' 'public.author_gr_map.author_ph_gr ' 'FROM public.author ' 'LEFT JOIN public.author_gr_map ' 'ON public.author.id = public.author_gr_map.author_id ' 'WHERE LOWER(name) LIKE LOWER(:search_like) ', {'search_like': '%'+form.search.data+'%'} ).fetchall() s_q_books = db.execute( 'SELECT public.book.*, ' 'array_agg(public.author.name) ' 'FROM public.book ' 'JOIN public.book_author ' 'ON public.book.id = public.book_author.book_id ' 'JOIN public.author ' 'ON public.book_author.author_id = public.author.id ' 'WHERE isbn LIKE :search_like ' 'OR to_tsvector(title) @@ to_tsquery(:search) ' 'GROUP BY public.book.id', {'search': form.search.data.strip().replace(' ', ' & '), 'search_like': '%'+form.search.data+'%'} ).fetchall() results = (s_q_books, s_q_authors) return render_template('search.html', form=form, results=results) if not form.validate_on_submit(): for field in form.errors: for err in form.errors[field]: flash(f'{field}: {err}', 'alert') return render_template('search.html', form=form, results=None)
def test_isbn10_is_isbn(self): check = is_isbn_code('0380795272') self.assertEqual('0380795272', check)
def test_title_not_isbn(self): check = is_isbn_code('Krondor: The Betrayal') self.assertEqual(False, check)
def test_author_not_isbn(self): check = is_isbn_code('Raymond E. Feist') self.assertEqual(False, check)
def test_empty_not_isbn(self): check = is_isbn_code('') self.assertEqual(False, check)
def test_isbn_cleanup(self): check = is_isbn_code('#0-380-79527-2/') self.assertEqual('0380795272', check)