Я выбрал первый метод, и он отлично работает, сравните со вторым методом, он сохранил мои модификации для реализации export.js
Вы можете добавить любые критерии поиска в URL, template/search.html
:
<a href={{h.url_for("/export", search_criteria='random string', ...)}}></a>
Затем в plugin.py
вы можете переопределить before_map
в IRoutes
inferface и добавить URL в before_map
:
class ExportPlugin(plugins.SingletonPlugin):
plugins.implements(plugins.IRoutes, inherit=True)
def before_map(self, m):
m.connect('export', '/export',
controller='ckanext.my_export_plugin.controller:ExportController',
action='export')
Затем в controller.py
вы можете унаследовать BaseController
так, чтобы вы могли обрабатывать поиск с помощью package_search
API и передавать любой из настроенных критериев поиска в запросе фасета fq
. Вы также можете добавить функцию преобразования и загрузки в этом контроллере:
class ExportController(base.BaseController):
def json_to_csv(original_json):
'''
Other Implementation of convertion
'''
return csv.read()
def export(self, data):
'''
Other Implementation of handling search operation
'''
search_results = toolkit.get_action('package_search')(data_dict=data)
file_name = "export.csv"
response.headers['Content-Type'] = 'application/csv'
response.headers['Content-Disposition'] = file_name
return json_to_csv(search_results)
После того, как вы создадите свою собственную реализацию, вы сможете создать настраиваемый URL для службы REST и полностью обработать запрос на бэкэнд.