A search interface for the Performing Patents Otherwise publication as part of the Politics of Patents case study (part of Copim WP6): this parses data from the archive of RTF files and provides additional data from the European Patent Office OPS API. https://patents.copim.ac.uk
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.

114 line
4.1KB

  1. # @name: search.py
  2. # @creation_date: 2022-09-07
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: search route for search
  6. # @acknowledgements:
  7. # https://www.digitalocean.com/community/tutorials/how-to-add-authentication-to-your-app-with-flask-login
  8. from flask import Blueprint, render_template, request, redirect, url_for
  9. from . import solr
  10. from . import ops
  11. search = Blueprint('search', __name__)
  12. # route for search page
  13. @search.route('/search/', methods=['GET', 'POST'])
  14. def basic_search():
  15. if request.method == 'POST':
  16. search = request.form.get('search')
  17. if request.form.get('core') is not None:
  18. core = request.form.get('core')
  19. else:
  20. core = 'all'
  21. if request.form.get('sort') is not None:
  22. sort = request.form.get('sort')
  23. else:
  24. sort = 'relevance'
  25. search_results = solr.content_search(core, sort, search)
  26. results = search_results[0]
  27. num_found = search_results[1]
  28. country_facet = search_results[2]
  29. year_facet = search_results[3]
  30. total_number = solr.get_total_number(core)
  31. return render_template('search.html', results=results, num_found=num_found, total_number=total_number, country_facet=country_facet, year_facet=year_facet, search=search, core=core, sort=sort)
  32. else:
  33. return redirect(url_for('main.index'))
  34. # route for id_search page
  35. @search.route('/search/id/', methods=['GET'])
  36. def id_search():
  37. if request.args.get('id') is None:
  38. return redirect(url_for('main.index'))
  39. else:
  40. id = request.args.get('id')
  41. if request.args.get('core') is not None:
  42. core = request.args.get('core')
  43. else:
  44. core = 'all'
  45. if request.args.get('sort') is not None:
  46. sort = request.args.get('sort')
  47. else:
  48. sort = 'relevance'
  49. search_results = solr.content_search(core, sort, search, id)
  50. results = search_results[0]
  51. for result in results:
  52. publication_details = ops.get_publication_details(result['doc_ref'])
  53. result.update(publication_details)
  54. if ops.get_images(result['doc_ref']):
  55. image = ops.get_images(result['doc_ref'])
  56. result.update(image)
  57. return render_template('record.html', results=results)
  58. # route for country search page
  59. @search.route('/search/country/', methods=['GET', 'POST'])
  60. def country_search():
  61. if request.method == 'POST':
  62. country_code = request.form.get('country_code')
  63. core = request.form.get('core')
  64. sort = request.form.get('sort')
  65. else:
  66. country_code = request.args.get('country_code')
  67. core = request.args.get('core')
  68. sort = request.args.get('sort')
  69. if country_code is None:
  70. return redirect(url_for('main.index'))
  71. if core is None:
  72. core = 'all'
  73. if sort is None:
  74. sort = 'relevance'
  75. field = 'country'
  76. search_results = solr.term_search(core, sort, field, country_code)
  77. results = search_results[0]
  78. num_found = search_results[1]
  79. total_number = solr.get_total_number(core)
  80. return render_template('search.html', results=results, num_found=num_found, total_number=total_number, country_code=country_code, core=core, sort=sort)
  81. # route for year search page
  82. @search.route('/search/year/', methods=['GET', 'POST'])
  83. def year_search():
  84. if request.method == 'POST':
  85. year = request.form.get('year')
  86. core = request.form.get('core')
  87. sort = request.form.get('sort')
  88. else:
  89. year = request.args.get('year')
  90. core = request.args.get('core')
  91. sort = request.args.get('sort')
  92. if year is None:
  93. return redirect(url_for('main.index'))
  94. if core is None:
  95. core = 'all'
  96. if sort is None:
  97. sort = 'relevance'
  98. field = 'year'
  99. search_results = solr.term_search(core, sort, field, year)
  100. results = search_results[0]
  101. num_found = search_results[1]
  102. total_number = solr.get_total_number(core)
  103. return render_template('search.html', results=results, num_found=num_found, total_number=total_number, year=year, core=core, sort=sort)