You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.4KB

  1. # @name: search.py
  2. # @creation_date: 2023-04-04
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: Search route for search results
  6. # @acknowledgements:
  7. # https://medium.com/@joseortizcosta/search-utility-with-flask-and-mysql-60bb8ee83dad
  8. from flask import Blueprint, render_template, request, flash, redirect, url_for
  9. from .models import Resource
  10. from sqlalchemy import or_
  11. search = Blueprint('search', __name__)
  12. #endpoint for search
  13. @search.route('/search', methods=['GET'])
  14. def basic_search():
  15. if request.args.get('query') is not None:
  16. query = request.args.get('query')
  17. results = Resource.query.filter(
  18. or_(
  19. Resource.name.ilike('%' + query + '%'),
  20. Resource.description.ilike('%' + query + '%'),
  21. Resource.developer.ilike('%' + query + '%'),
  22. Resource.longDescription.ilike('%' + query + '%'),
  23. Resource.experimental.ilike('%' + query + '%'),
  24. Resource.considerations.ilike('%' + query + '%'),
  25. Resource.references.ilike('%' + query + '%'),
  26. Resource.author.ilike('%' + query + '%'),
  27. Resource.isbn.ilike('%' + query + '%'),
  28. Resource.typology.ilike('%' + query + '%'),
  29. )).all()
  30. return render_template('search.html', results=results)
  31. else:
  32. return redirect(url_for('main.index'))