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
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

102 lines
3.8KB

  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 Section 1: Searching the archive
  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. import pycountry
  12. search = Blueprint('search', __name__)
  13. # route for basic search page
  14. @search.route('/search/', methods=['GET', 'POST'])
  15. def basic_search():
  16. if request.method == 'POST':
  17. if request.form.get('query') is not None:
  18. query = request.form.get('query')
  19. else:
  20. query = None
  21. if request.form.get('core') is not None:
  22. core = request.form.get('core')
  23. else:
  24. core = 'all'
  25. if request.form.get('sort') is not None:
  26. sort = request.form.get('sort')
  27. else:
  28. sort = 'relevance'
  29. if request.form.get('country') is not None:
  30. country = request.form.get('country')
  31. else:
  32. country = None
  33. if request.form.get('year') is not None:
  34. year = request.form.get('year')
  35. else:
  36. year = None
  37. else:
  38. if request.args.get('query') is not None:
  39. query = request.args.get('query')
  40. else:
  41. query = None
  42. if request.args.get('core') is not None:
  43. core = request.args.get('core')
  44. else:
  45. core = 'all'
  46. if request.args.get('sort') is not None:
  47. sort = request.args.get('sort')
  48. else:
  49. sort = 'relevance'
  50. if request.args.get('country') is not None:
  51. country = request.args.get('country')
  52. else:
  53. country = None
  54. if request.args.get('year') is not None:
  55. year = request.args.get('year')
  56. else:
  57. year = None
  58. if (query is None and country is None and year is None):
  59. return redirect(url_for('main.index'))
  60. else:
  61. search_results = solr.query_search(core, sort, query, country, year)
  62. results = search_results[0]
  63. num_found = search_results[1]
  64. year_facet = search_results[2]['year']
  65. country_facet = search_results[2]['country']
  66. for i in range(0, len(country_facet)):
  67. if i % 2 == 0:
  68. country_full = pycountry.countries.get(alpha_2=country_facet[i])
  69. if country_full is not None:
  70. country_facet[i] = country_full
  71. else:
  72. country_full = pycountry.historic_countries.get(alpha_2=country_facet[i])
  73. if country_full is not None:
  74. country_facet[i] = country_full
  75. total_number = solr.get_total_number(core)
  76. return render_template('search.html', results=results, num_found=num_found, total_number=total_number, country_facet=country_facet, year_facet=year_facet, query=query, core=core, sort=sort, country=country, year=year)
  77. # route for id_search page
  78. @search.route('/search/id/', methods=['GET'])
  79. def id_search():
  80. if request.args.get('id') is None:
  81. return redirect(url_for('main.index'))
  82. else:
  83. id = request.args.get('id')
  84. if request.args.get('core') is not None:
  85. core = request.args.get('core')
  86. else:
  87. core = 'all'
  88. search_results = solr.id_search(core, id)
  89. results = search_results[0]
  90. for result in results:
  91. publication_details = ops.get_publication_details(result['doc_ref'])
  92. result.update(publication_details)
  93. if ops.get_images(result['doc_ref']):
  94. image = ops.get_images(result['doc_ref'])
  95. result.update(image)
  96. return render_template('record.html', results=results)