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