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文字以内のものにしてください。

search.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. import math
  13. search = Blueprint('search', __name__)
  14. # route for basic search page
  15. @search.route('/search/', methods=['GET', 'POST'])
  16. def basic_search():
  17. if request.method == 'POST':
  18. if request.form.get('query') is not None:
  19. query = request.form.get('query')
  20. else:
  21. query = None
  22. if request.form.get('core') is not None:
  23. core = request.form.get('core')
  24. else:
  25. core = 'all'
  26. if request.form.get('sort') is not None:
  27. sort = request.form.get('sort')
  28. else:
  29. sort = 'relevance'
  30. if request.form.get('country') is not None:
  31. country = request.form.get('country')
  32. else:
  33. country = None
  34. if request.form.get('year') is not None:
  35. year = request.form.get('year')
  36. else:
  37. year = None
  38. if request.form.get('page') is not None:
  39. page = request.form.get('page')
  40. else:
  41. page = '1'
  42. else:
  43. if request.args.get('query') is not None:
  44. query = request.args.get('query')
  45. else:
  46. query = None
  47. if request.args.get('core') is not None:
  48. core = request.args.get('core')
  49. else:
  50. core = 'all'
  51. if request.args.get('sort') is not None:
  52. sort = request.args.get('sort')
  53. else:
  54. sort = 'relevance'
  55. if request.args.get('country') is not None:
  56. country = request.args.get('country')
  57. else:
  58. country = None
  59. if request.args.get('year') is not None:
  60. year = request.args.get('year')
  61. else:
  62. year = None
  63. if request.args.get('page') is not None:
  64. page = request.args.get('page')
  65. else:
  66. page = '1'
  67. if (query is None and country is None and year is None):
  68. return redirect(url_for('main.index'))
  69. else:
  70. search_results = solr.query_search(core, sort, query, country, year, page)
  71. results = search_results[0]
  72. num_found = search_results[1]
  73. year_facet = search_results[2]['year']
  74. country_facet = search_results[2]['country']
  75. for i in range(0, len(country_facet)):
  76. if i % 2 == 0:
  77. country_full = pycountry.countries.get(alpha_2=country_facet[i])
  78. if country_full is not None:
  79. country_facet[i] = country_full
  80. else:
  81. country_full = pycountry.historic_countries.get(alpha_2=country_facet[i])
  82. if country_full is not None:
  83. country_facet[i] = country_full
  84. # get total number of records
  85. total_number = solr.get_total_number(core)
  86. # work out how many pages to display in the pagination
  87. num_pages = math.ceil(int(num_found) / 10)
  88. raw_pagination = list(range(1, num_pages+1))
  89. if int(page) > 5:
  90. pagination = raw_pagination[int(page)-5:int(page)+4]
  91. #pagination.insert(0,raw_pagination[0])
  92. #pagination.append(raw_pagination[len(raw_pagination)-1])
  93. else:
  94. end = 9-int(page)
  95. pagination = raw_pagination[:int(page)+end]
  96. #pagination.insert(0,raw_pagination[0])
  97. #pagination.append(raw_pagination[len(raw_pagination)-1])
  98. # work out page range for display
  99. page_end = int(page) * 10
  100. page_start = page_end - 9
  101. page_end = page_start + (len(results)-1)
  102. page_range = str(page_start) + '–' + str(page_end)
  103. 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, page=page, page_range=page_range, pagination=pagination)
  104. # route for id_search page
  105. @search.route('/search/id/', methods=['GET'])
  106. def id_search():
  107. if request.args.get('id') is None:
  108. return redirect(url_for('main.index'))
  109. else:
  110. id = request.args.get('id')
  111. if request.args.get('core') is not None:
  112. core = request.args.get('core')
  113. else:
  114. core = 'all'
  115. search_results = solr.id_search(core, id)
  116. results = search_results[0]
  117. for result in results:
  118. publication_details = ops.get_publication_details(result['doc_ref'])
  119. result.update(publication_details)
  120. if ops.get_images(result['doc_ref']):
  121. image = ops.get_images(result['doc_ref'])
  122. result.update(image)
  123. return render_template('record.html', results=results)