Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

39 lines
1.6KB

  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. import markdown
  12. search = Blueprint('search', __name__)
  13. #endpoint for search
  14. @search.route('/search', methods=['GET'])
  15. def basic_search():
  16. if request.args.get('query') is not None:
  17. query = request.args.get('query')
  18. results = Resource.query.filter(
  19. or_(
  20. Resource.name.ilike('%' + query + '%'),
  21. Resource.description.ilike('%' + query + '%'),
  22. Resource.developer.ilike('%' + query + '%'),
  23. Resource.longDescription.ilike('%' + query + '%'),
  24. Resource.experimental.ilike('%' + query + '%'),
  25. Resource.considerations.ilike('%' + query + '%'),
  26. Resource.references.ilike('%' + query + '%'),
  27. Resource.author.ilike('%' + query + '%'),
  28. Resource.isbn.ilike('%' + query + '%'),
  29. Resource.typology.ilike('%' + query + '%'),
  30. )).all()
  31. # render Markdown as HTML
  32. for result in results:
  33. result.description = markdown.markdown(result.description)
  34. return render_template('search.html', results=results)
  35. else:
  36. return redirect(url_for('main.index'))