##// END OF EJS Templates
search: remove excesive quoting on search pagination....
dan -
r4280:4c0a23db default
parent child Browse files
Show More
@@ -1,180 +1,178 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2011-2019 RhodeCode GmbH
3 # Copyright (C) 2011-2019 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import logging
21 import logging
22 import urllib
22 import urllib
23 from pyramid.view import view_config
23 from pyramid.view import view_config
24 from webhelpers2.html.tools import update_params
24 from webhelpers2.html.tools import update_params
25
25
26 from rhodecode.apps._base import BaseAppView, RepoAppView, RepoGroupAppView
26 from rhodecode.apps._base import BaseAppView, RepoAppView, RepoGroupAppView
27 from rhodecode.lib.auth import (
27 from rhodecode.lib.auth import (
28 LoginRequired, HasRepoPermissionAnyDecorator, HasRepoGroupPermissionAnyDecorator)
28 LoginRequired, HasRepoPermissionAnyDecorator, HasRepoGroupPermissionAnyDecorator)
29 from rhodecode.lib.helpers import Page
29 from rhodecode.lib.helpers import Page
30 from rhodecode.lib.utils2 import safe_str
30 from rhodecode.lib.utils2 import safe_str
31 from rhodecode.lib.index import searcher_from_config
31 from rhodecode.lib.index import searcher_from_config
32 from rhodecode.model import validation_schema
32 from rhodecode.model import validation_schema
33 from rhodecode.model.validation_schema.schemas import search_schema
33 from rhodecode.model.validation_schema.schemas import search_schema
34
34
35 log = logging.getLogger(__name__)
35 log = logging.getLogger(__name__)
36
36
37
37
38 def perform_search(request, tmpl_context, repo_name=None, repo_group_name=None):
38 def perform_search(request, tmpl_context, repo_name=None, repo_group_name=None):
39 searcher = searcher_from_config(request.registry.settings)
39 searcher = searcher_from_config(request.registry.settings)
40 formatted_results = []
40 formatted_results = []
41 execution_time = ''
41 execution_time = ''
42
42
43 schema = search_schema.SearchParamsSchema()
43 schema = search_schema.SearchParamsSchema()
44 search_tags = []
44 search_tags = []
45 search_params = {}
45 search_params = {}
46 errors = []
46 errors = []
47
47
48 try:
48 try:
49 search_params = schema.deserialize(
49 search_params = schema.deserialize(
50 dict(
50 dict(
51 search_query=request.GET.get('q'),
51 search_query=request.GET.get('q'),
52 search_type=request.GET.get('type'),
52 search_type=request.GET.get('type'),
53 search_sort=request.GET.get('sort'),
53 search_sort=request.GET.get('sort'),
54 search_max_lines=request.GET.get('max_lines'),
54 search_max_lines=request.GET.get('max_lines'),
55 page_limit=request.GET.get('page_limit'),
55 page_limit=request.GET.get('page_limit'),
56 requested_page=request.GET.get('page'),
56 requested_page=request.GET.get('page'),
57 )
57 )
58 )
58 )
59 except validation_schema.Invalid as e:
59 except validation_schema.Invalid as e:
60 errors = e.children
60 errors = e.children
61
61
62 def url_generator(page_num):
62 def url_generator(page_num):
63 q = urllib.quote(safe_str(search_query))
64
63
65 query_params = {
64 query_params = {
66 'page': page_num,
65 'page': page_num,
67 'q': q,
66 'q': safe_str(search_query),
68 'type': safe_str(search_type),
67 'type': safe_str(search_type),
69 'max_lines': search_max_lines,
68 'max_lines': search_max_lines,
70 'sort': search_sort
69 'sort': search_sort
71 }
70 }
72
71
73 return '?' + urllib.urlencode(query_params)
72 return '?' + urllib.urlencode(query_params)
74
73
75
76 c = tmpl_context
74 c = tmpl_context
77 search_query = search_params.get('search_query')
75 search_query = search_params.get('search_query')
78 search_type = search_params.get('search_type')
76 search_type = search_params.get('search_type')
79 search_sort = search_params.get('search_sort')
77 search_sort = search_params.get('search_sort')
80 search_max_lines = search_params.get('search_max_lines')
78 search_max_lines = search_params.get('search_max_lines')
81 if search_params.get('search_query'):
79 if search_params.get('search_query'):
82 page_limit = search_params['page_limit']
80 page_limit = search_params['page_limit']
83 requested_page = search_params['requested_page']
81 requested_page = search_params['requested_page']
84
82
85 try:
83 try:
86 search_result = searcher.search(
84 search_result = searcher.search(
87 search_query, search_type, c.auth_user, repo_name, repo_group_name,
85 search_query, search_type, c.auth_user, repo_name, repo_group_name,
88 requested_page=requested_page, page_limit=page_limit, sort=search_sort)
86 requested_page=requested_page, page_limit=page_limit, sort=search_sort)
89
87
90 formatted_results = Page(
88 formatted_results = Page(
91 search_result['results'], page=requested_page,
89 search_result['results'], page=requested_page,
92 item_count=search_result['count'],
90 item_count=search_result['count'],
93 items_per_page=page_limit, url_maker=url_generator)
91 items_per_page=page_limit, url_maker=url_generator)
94 finally:
92 finally:
95 searcher.cleanup()
93 searcher.cleanup()
96
94
97 search_tags = searcher.extract_search_tags(search_query)
95 search_tags = searcher.extract_search_tags(search_query)
98
96
99 if not search_result['error']:
97 if not search_result['error']:
100 execution_time = '%s results (%.4f seconds)' % (
98 execution_time = '%s results (%.4f seconds)' % (
101 search_result['count'],
99 search_result['count'],
102 search_result['runtime'])
100 search_result['runtime'])
103 elif not errors:
101 elif not errors:
104 node = schema['search_query']
102 node = schema['search_query']
105 errors = [
103 errors = [
106 validation_schema.Invalid(node, search_result['error'])]
104 validation_schema.Invalid(node, search_result['error'])]
107
105
108 c.perm_user = c.auth_user
106 c.perm_user = c.auth_user
109 c.repo_name = repo_name
107 c.repo_name = repo_name
110 c.repo_group_name = repo_group_name
108 c.repo_group_name = repo_group_name
111 c.errors = errors
109 c.errors = errors
112 c.formatted_results = formatted_results
110 c.formatted_results = formatted_results
113 c.runtime = execution_time
111 c.runtime = execution_time
114 c.cur_query = search_query
112 c.cur_query = search_query
115 c.search_type = search_type
113 c.search_type = search_type
116 c.searcher = searcher
114 c.searcher = searcher
117 c.search_tags = search_tags
115 c.search_tags = search_tags
118
116
119 direction, sort_field = searcher.get_sort(search_type, search_sort)
117 direction, sort_field = searcher.get_sort(search_type, search_sort)
120 sort_definition = searcher.sort_def(search_type, direction, sort_field)
118 sort_definition = searcher.sort_def(search_type, direction, sort_field)
121 c.sort = ''
119 c.sort = ''
122 c.sort_tag = None
120 c.sort_tag = None
123 c.sort_tag_dir = direction
121 c.sort_tag_dir = direction
124 if sort_definition:
122 if sort_definition:
125 c.sort = '{}:{}'.format(direction, sort_field)
123 c.sort = '{}:{}'.format(direction, sort_field)
126 c.sort_tag = sort_field
124 c.sort_tag = sort_field
127
125
128
126
129 class SearchView(BaseAppView):
127 class SearchView(BaseAppView):
130 def load_default_context(self):
128 def load_default_context(self):
131 c = self._get_local_tmpl_context()
129 c = self._get_local_tmpl_context()
132 return c
130 return c
133
131
134 @LoginRequired()
132 @LoginRequired()
135 @view_config(
133 @view_config(
136 route_name='search', request_method='GET',
134 route_name='search', request_method='GET',
137 renderer='rhodecode:templates/search/search.mako')
135 renderer='rhodecode:templates/search/search.mako')
138 def search(self):
136 def search(self):
139 c = self.load_default_context()
137 c = self.load_default_context()
140 perform_search(self.request, c)
138 perform_search(self.request, c)
141 return self._get_template_context(c)
139 return self._get_template_context(c)
142
140
143
141
144 class SearchRepoView(RepoAppView):
142 class SearchRepoView(RepoAppView):
145 def load_default_context(self):
143 def load_default_context(self):
146 c = self._get_local_tmpl_context()
144 c = self._get_local_tmpl_context()
147 c.active = 'search'
145 c.active = 'search'
148 return c
146 return c
149
147
150 @LoginRequired()
148 @LoginRequired()
151 @HasRepoPermissionAnyDecorator(
149 @HasRepoPermissionAnyDecorator(
152 'repository.read', 'repository.write', 'repository.admin')
150 'repository.read', 'repository.write', 'repository.admin')
153 @view_config(
151 @view_config(
154 route_name='search_repo', request_method='GET',
152 route_name='search_repo', request_method='GET',
155 renderer='rhodecode:templates/search/search.mako')
153 renderer='rhodecode:templates/search/search.mako')
156 @view_config(
154 @view_config(
157 route_name='search_repo_alt', request_method='GET',
155 route_name='search_repo_alt', request_method='GET',
158 renderer='rhodecode:templates/search/search.mako')
156 renderer='rhodecode:templates/search/search.mako')
159 def search_repo(self):
157 def search_repo(self):
160 c = self.load_default_context()
158 c = self.load_default_context()
161 perform_search(self.request, c, repo_name=self.db_repo_name)
159 perform_search(self.request, c, repo_name=self.db_repo_name)
162 return self._get_template_context(c)
160 return self._get_template_context(c)
163
161
164
162
165 class SearchRepoGroupView(RepoGroupAppView):
163 class SearchRepoGroupView(RepoGroupAppView):
166 def load_default_context(self):
164 def load_default_context(self):
167 c = self._get_local_tmpl_context()
165 c = self._get_local_tmpl_context()
168 c.active = 'search'
166 c.active = 'search'
169 return c
167 return c
170
168
171 @LoginRequired()
169 @LoginRequired()
172 @HasRepoGroupPermissionAnyDecorator(
170 @HasRepoGroupPermissionAnyDecorator(
173 'group.read', 'group.write', 'group.admin')
171 'group.read', 'group.write', 'group.admin')
174 @view_config(
172 @view_config(
175 route_name='search_repo_group', request_method='GET',
173 route_name='search_repo_group', request_method='GET',
176 renderer='rhodecode:templates/search/search.mako')
174 renderer='rhodecode:templates/search/search.mako')
177 def search_repo_group(self):
175 def search_repo_group(self):
178 c = self.load_default_context()
176 c = self.load_default_context()
179 perform_search(self.request, c, repo_group_name=self.db_repo_group_name)
177 perform_search(self.request, c, repo_group_name=self.db_repo_group_name)
180 return self._get_template_context(c)
178 return self._get_template_context(c)
General Comments 0
You need to be logged in to leave comments. Login now