|
1 # |
|
2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net> |
|
3 # All Rights Reserved. |
|
4 # |
|
5 # This software is subject to the provisions of the Zope Public License, |
|
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
10 # FOR A PARTICULAR PURPOSE. |
|
11 # |
|
12 |
|
13 __docformat__ = 'restructuredtext' |
|
14 |
|
15 |
|
16 # import standard library |
|
17 |
|
18 # import interfaces |
|
19 from pyams_form.interfaces.form import IWidgetForm, ISearchForm |
|
20 from pyams_skin.interfaces import IContentSearch, IInnerPage, ISearchPage |
|
21 from pyams_skin.layer import IPyAMSLayer |
|
22 from z3c.table.interfaces import IValues |
|
23 |
|
24 # import packages |
|
25 from pyams_form.form import AddForm |
|
26 from pyams_form.schema import ResetButton |
|
27 from pyams_skin.table import BaseTable |
|
28 from pyams_template.template import template_config |
|
29 from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config |
|
30 from pyramid.response import Response |
|
31 from z3c.form import field, button |
|
32 from zope.interface import implementer, Interface |
|
33 from zope.schema import TextLine |
|
34 |
|
35 from pyams_form import _ |
|
36 |
|
37 |
|
38 class ISearchFields(Interface): |
|
39 """Default search form interface""" |
|
40 |
|
41 query = TextLine(title=_("Search query")) |
|
42 |
|
43 |
|
44 class ISearchButtons(Interface): |
|
45 """Search form buttons interface""" |
|
46 |
|
47 reset = ResetButton(name='reset', title=_("Reset")) |
|
48 search = button.Button(name='search', title=_("Search")) |
|
49 |
|
50 |
|
51 @implementer(IWidgetForm, ISearchForm) |
|
52 class SearchForm(AddForm): |
|
53 """Base search form""" |
|
54 |
|
55 legend = _("Search") |
|
56 widget_icon_class = 'fa fa-fw fa-search' |
|
57 |
|
58 fields = field.Fields(ISearchFields) |
|
59 buttons = button.Buttons(ISearchButtons) |
|
60 |
|
61 ajax_handler = 'search-results.html' |
|
62 form_target = '#search-results' |
|
63 |
|
64 def updateActions(self): |
|
65 super(SearchForm, self).updateActions() |
|
66 if 'search' in self.actions: |
|
67 self.actions['search'].addClass('btn-primary') |
|
68 |
|
69 def get_search_results(self): |
|
70 search = IContentSearch(self.context, None) |
|
71 if search is not None: |
|
72 self.updateWidgets() |
|
73 data, errors = self.extractData() |
|
74 return search.get_search_results(data) |
|
75 |
|
76 |
|
77 @template_config(template='templates/search.pt', layer=IPyAMSLayer) |
|
78 @implementer(IInnerPage, ISearchPage) |
|
79 class SearchView(object): |
|
80 """Base search view""" |
|
81 |
|
82 search_form_factory = SearchForm |
|
83 |
|
84 def update(self): |
|
85 self.search_form = self.search_form_factory(self.context, self.request) |
|
86 self.search_form.update() |
|
87 |
|
88 |
|
89 class SearchResultsView(BaseTable): |
|
90 """Search results view""" |
|
91 |
|
92 search_form_factory = SearchForm |
|
93 |
|
94 def __call__(self): |
|
95 self.update() |
|
96 return Response(self.renderTable()) |
|
97 |
|
98 |
|
99 @adapter_config(context=(Interface, IPyAMSLayer, SearchResultsView), provides=IValues) |
|
100 class SearchResultsViewValuesAdapter(ContextRequestViewAdapter): |
|
101 """Search results view values adapter""" |
|
102 |
|
103 @property |
|
104 def values(self): |
|
105 form = self.view.search_form_factory(self.context, self.request) |
|
106 return form.get_search_results() |