Show More
@@ -0,0 +1,101 b'' | |||||
|
1 | # -*- coding: utf-8 -*- | |||
|
2 | ||||
|
3 | # Copyright (C) 2011-2018 RhodeCode GmbH | |||
|
4 | # | |||
|
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 | |||
|
7 | # (only), as published by the Free Software Foundation. | |||
|
8 | # | |||
|
9 | # This program is distributed in the hope that it will be useful, | |||
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
|
12 | # GNU General Public License for more details. | |||
|
13 | # | |||
|
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/>. | |||
|
16 | # | |||
|
17 | # This program is dual-licensed. If you wish to learn more about the | |||
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |||
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |||
|
20 | ||||
|
21 | ||||
|
22 | import logging | |||
|
23 | ||||
|
24 | from rhodecode.api import jsonrpc_method | |||
|
25 | from rhodecode.api.exc import JSONRPCValidationError | |||
|
26 | from rhodecode.api.utils import Optional | |||
|
27 | from rhodecode.lib.index import searcher_from_config | |||
|
28 | from rhodecode.model import validation_schema | |||
|
29 | from rhodecode.model.validation_schema.schemas import search_schema | |||
|
30 | ||||
|
31 | log = logging.getLogger(__name__) | |||
|
32 | ||||
|
33 | ||||
|
34 | @jsonrpc_method() | |||
|
35 | def search(request, apiuser, search_query, search_type, page_limit=Optional(10), page=Optional(1), search_sort=Optional('newfirst'), repo_name=Optional(None)): | |||
|
36 | """ | |||
|
37 | Search | |||
|
38 | ||||
|
39 | :param apiuser: This is filled automatically from the |authtoken|. | |||
|
40 | :type apiuser: AuthUser | |||
|
41 | :param search_query: Search query. | |||
|
42 | :type search_query: str | |||
|
43 | :param search_type: Search type. either commit or content. | |||
|
44 | :type search_type: str | |||
|
45 | :param page_limit: Page item limit. Default 10 items. | |||
|
46 | :type page_limit: Optional(int) | |||
|
47 | :param page: Page number. Default first page. | |||
|
48 | :type page: Optional(int) | |||
|
49 | :param search_sort: Search sort. Default newfirst. | |||
|
50 | :type search_sort: Optional(str) | |||
|
51 | :param repo_name: Filter by one repo. Default is all. | |||
|
52 | :type repo_name: Optional(str) | |||
|
53 | """ | |||
|
54 | ||||
|
55 | searcher = searcher_from_config(request.registry.settings) | |||
|
56 | data = {'execution_time': ''} | |||
|
57 | repo_name = Optional.extract(repo_name) | |||
|
58 | ||||
|
59 | schema = search_schema.SearchParamsSchema() | |||
|
60 | ||||
|
61 | search_params = {} | |||
|
62 | try: | |||
|
63 | search_params = schema.deserialize( | |||
|
64 | dict(search_query=search_query, | |||
|
65 | search_type=search_type, | |||
|
66 | search_sort=Optional.extract(search_sort), | |||
|
67 | page_limit=Optional.extract(page_limit), | |||
|
68 | requested_page=Optional.extract(page)) | |||
|
69 | ) | |||
|
70 | except validation_schema.Invalid as err: | |||
|
71 | raise JSONRPCValidationError(colander_exc=err) | |||
|
72 | ||||
|
73 | search_query = search_params.get('search_query') | |||
|
74 | search_type = search_params.get('search_type') | |||
|
75 | search_sort = search_params.get('search_sort') | |||
|
76 | ||||
|
77 | if search_params.get('search_query'): | |||
|
78 | page_limit = search_params['page_limit'] | |||
|
79 | requested_page = search_params['requested_page'] | |||
|
80 | ||||
|
81 | try: | |||
|
82 | search_result = searcher.search( | |||
|
83 | search_query, search_type, apiuser, repo_name, | |||
|
84 | requested_page, page_limit, search_sort) | |||
|
85 | ||||
|
86 | data.update(dict( | |||
|
87 | results=list(search_result['results']), page=requested_page, | |||
|
88 | item_count=search_result['count'], | |||
|
89 | items_per_page=page_limit)) | |||
|
90 | finally: | |||
|
91 | searcher.cleanup() | |||
|
92 | ||||
|
93 | if not search_result['error']: | |||
|
94 | data['execution_time'] = '%s results (%.3f seconds)' % ( | |||
|
95 | search_result['count'], | |||
|
96 | search_result['runtime']) | |||
|
97 | else: | |||
|
98 | node = schema['search_query'] | |||
|
99 | raise JSONRPCValidationError(colander_exc=validation_schema.Invalid(node, search_result['error'])) | |||
|
100 | ||||
|
101 | return data |
General Comments 0
You need to be logged in to leave comments.
Login now