From 627625d20be80415f28eb7fc484f969c2d135e98 Mon Sep 17 00:00:00 2001 From: Adrian Moennich Date: Thu, 23 Oct 2014 11:09:26 +0200 Subject: [PATCH] Add conference search box --- search/indico_search/controllers.py | 6 +-- search/indico_search/forms.py | 2 +- search/indico_search/plugin.py | 7 ++++ .../templates/searchbox_conference.html | 7 ++++ search/indico_search/util.py | 37 +++++++++++++++++++ search/indico_search/views.py | 5 +-- 6 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 search/indico_search/templates/searchbox_conference.html create mode 100644 search/indico_search/util.py diff --git a/search/indico_search/controllers.py b/search/indico_search/controllers.py index 8d03391..0131622 100644 --- a/search/indico_search/controllers.py +++ b/search/indico_search/controllers.py @@ -21,7 +21,7 @@ from flask_pluginengine import current_plugin from MaKaC.conference import ConferenceHolder, Conference, CategoryManager from MaKaC.webinterface.rh.conferenceBase import RHCustomizable -from indico_search.views import WPSearchCategory, WPSearchEvent +from indico_search.views import WPSearchCategory, WPSearchConference class RHSearch(RHCustomizable): @@ -43,8 +43,8 @@ class RHSearch(RHCustomizable): def _process(self): with current_plugin.engine_plugin.plugin_context(): - form = current_plugin.search_form() - view_class = WPSearchEvent if isinstance(self.obj, Conference) else WPSearchCategory + form = current_plugin.search_form(prefix='search-') + view_class = WPSearchConference if isinstance(self.obj, Conference) else WPSearchCategory result = None if form.validate_on_submit(): result = current_plugin.perform_search(form.data, self.obj, self.page) diff --git a/search/indico_search/forms.py b/search/indico_search/forms.py index 5b0a6f0..0a0de8c 100644 --- a/search/indico_search/forms.py +++ b/search/indico_search/forms.py @@ -35,6 +35,6 @@ FIELD_CHOICES = [('', _('Anywhere')), class SearchForm(IndicoForm): phrase = StringField(_('Phrase'), filters=[strip_whitespace]) - field = SelectField(_('Search in'), choices=FIELD_CHOICES) + field = SelectField(_('Search in'), choices=FIELD_CHOICES, default='') start_date = DateField('Start Date', [Optional()], parse_kwargs={'dayfirst': True}) end_date = DateField('End Date', [Optional()], parse_kwargs={'dayfirst': True}) diff --git a/search/indico_search/plugin.py b/search/indico_search/plugin.py index c0cd9c1..c915500 100644 --- a/search/indico_search/plugin.py +++ b/search/indico_search/plugin.py @@ -21,6 +21,7 @@ from flask_pluginengine import plugins_loaded from indico.core.plugins import IndicoPlugin from indico_search.blueprint import blueprint +from indico_search.util import render_engine_or_search_template class SearchPlugin(IndicoPlugin): @@ -35,6 +36,7 @@ class SearchPlugin(IndicoPlugin): def init(self): super(SearchPlugin, self).init() self.connect(plugins_loaded, self._plugins_loaded, sender=self.app) + self.template_hook('conference-header', self._add_conference_search_box) def _plugins_loaded(self, sender, **kwargs): if not self.engine_plugin: @@ -52,3 +54,8 @@ class SearchPlugin(IndicoPlugin): def get_blueprints(self): return blueprint + + def _add_conference_search_box(self, event, **kwargs): + if event.getDisplayMgr().getSearchEnabled(): + form = self.engine_plugin.search_form(prefix='search-') + return render_engine_or_search_template('searchbox_conference.html', event=event, form=form) diff --git a/search/indico_search/templates/searchbox_conference.html b/search/indico_search/templates/searchbox_conference.html new file mode 100644 index 0000000..8d436d4 --- /dev/null +++ b/search/indico_search/templates/searchbox_conference.html @@ -0,0 +1,7 @@ +
+
+ + {{ form.phrase(class_='searchField', id='conference-search-phrase') }} + {% block extra_fields %}{% endblock %} +
+
diff --git a/search/indico_search/util.py b/search/indico_search/util.py new file mode 100644 index 0000000..07d5e7d --- /dev/null +++ b/search/indico_search/util.py @@ -0,0 +1,37 @@ +# This file is part of Indico. +# Copyright (C) 2002 - 2014 European Organization for Nuclear Research (CERN). +# +# Indico is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 3 of the +# License, or (at your option) any later version. +# +# Indico is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Indico; if not, see . + +from __future__ import unicode_literals + +from flask_pluginengine import render_plugin_template, current_plugin + + +def render_engine_or_search_template(template_name, **context): + """Renders a template from the engine plugin or the search plugin + + If the template is available in the engine plugin, it's taken + from there, otherwise the template from this plugin is used. + + :param template_name: name of the template + :param context: the variables that should be available in the + context of the template. + """ + from indico_search.plugin import SearchPlugin + assert current_plugin == SearchPlugin.instance + + templates = ('{}:{}'.format(SearchPlugin.instance.engine_plugin.name, template_name), + template_name) + return render_plugin_template(templates, **context) diff --git a/search/indico_search/views.py b/search/indico_search/views.py index 063c1d0..e47983a 100644 --- a/search/indico_search/views.py +++ b/search/indico_search/views.py @@ -18,7 +18,7 @@ from __future__ import unicode_literals from indico.core.plugins import WPJinjaMixinPlugin from MaKaC.webinterface.pages.category import WPCategoryDisplayBase -from MaKaC.webinterface.pages.conferences import WPConferenceDisplayBase +from MaKaC.webinterface.pages.conferences import WPConferenceDefaultDisplayBase class WPSearchCategory(WPJinjaMixinPlugin, WPCategoryDisplayBase): @@ -26,7 +26,6 @@ class WPSearchCategory(WPJinjaMixinPlugin, WPCategoryDisplayBase): return self._getPageContent(params) -class WPSearchEvent(WPJinjaMixinPlugin, WPConferenceDisplayBase): - # XXX shouldn't this inhert from WPConferenceDefaultDisplayBase?! +class WPSearchConference(WPJinjaMixinPlugin, WPConferenceDefaultDisplayBase): def _getBody(self, params): return self._getPageContent(params)