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

112 lines
4.0KB

  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. total_number = solr.get_total_number(core)
  29. return render_template('search.html', results=results, num_found=num_found, total_number=total_number, search=search, core=core, sort=sort)
  30. else:
  31. return redirect(url_for('main.index'))
  32. # route for id_search page
  33. @search.route('/search/id/', methods=['GET'])
  34. def id_search():
  35. if request.args.get('id') is None:
  36. return redirect(url_for('main.index'))
  37. else:
  38. id = request.args.get('id')
  39. if request.args.get('core') is not None:
  40. core = request.args.get('core')
  41. else:
  42. core = 'all'
  43. if request.args.get('sort') is not None:
  44. sort = request.args.get('sort')
  45. else:
  46. sort = 'relevance'
  47. search_results = solr.content_search(core, sort, search, id)
  48. results = search_results[0]
  49. for result in results:
  50. publication_details = ops.get_publication_details(result['doc_ref'])
  51. result.update(publication_details)
  52. if ops.get_images(result['doc_ref']):
  53. image = ops.get_images(result['doc_ref'])
  54. result.update(image)
  55. return render_template('record.html', results=results)
  56. # route for country search page
  57. @search.route('/search/country/', methods=['GET', 'POST'])
  58. def country_search():
  59. if request.method == 'POST':
  60. country_code = request.form.get('country_code')
  61. core = request.form.get('core')
  62. sort = request.form.get('sort')
  63. else:
  64. country_code = request.args.get('country_code')
  65. core = request.args.get('core')
  66. sort = request.args.get('sort')
  67. if country_code is None:
  68. return redirect(url_for('main.index'))
  69. if core is None:
  70. core = 'all'
  71. if sort is None:
  72. sort = 'relevance'
  73. field = 'country'
  74. search_results = solr.term_search(core, sort, field, country_code)
  75. results = search_results[0]
  76. num_found = search_results[1]
  77. total_number = solr.get_total_number(core)
  78. return render_template('search.html', results=results, num_found=num_found, total_number=total_number, country_code=country_code, core=core, sort=sort)
  79. # route for year search page
  80. @search.route('/search/year/', methods=['GET', 'POST'])
  81. def year_search():
  82. if request.method == 'POST':
  83. year = request.form.get('year')
  84. core = request.form.get('core')
  85. sort = request.form.get('sort')
  86. else:
  87. year = request.args.get('year')
  88. core = request.args.get('core')
  89. sort = request.args.get('sort')
  90. if year is None:
  91. return redirect(url_for('main.index'))
  92. if core is None:
  93. core = 'all'
  94. if sort is None:
  95. sort = 'relevance'
  96. field = 'year'
  97. search_results = solr.term_search(core, sort, field, year)
  98. results = search_results[0]
  99. num_found = search_results[1]
  100. total_number = solr.get_total_number(core)
  101. return render_template('search.html', results=results, num_found=num_found, total_number=total_number, year=year, core=core, sort=sort)