##// END OF EJS Templates
search: per-repo search shouldn't require admin permissions. Read is enought because we access the repo only
marcink -
r2048:affe5a68 default
parent child Browse files
Show More
@@ -1,133 +1,134 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2011-2017 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 webhelpers.util import update_params
25 25
26 26 from rhodecode.apps._base import BaseAppView, RepoAppView
27 27 from rhodecode.lib.auth import (LoginRequired, HasRepoPermissionAnyDecorator)
28 28 from rhodecode.lib.helpers import Page
29 29 from rhodecode.lib.utils2 import safe_str, safe_int
30 30 from rhodecode.lib.index import searcher_from_config
31 31 from rhodecode.model import validation_schema
32 32 from rhodecode.model.validation_schema.schemas import search_schema
33 33
34 34 log = logging.getLogger(__name__)
35 35
36 36
37 37 def search(request, tmpl_context, repo_name):
38 38 searcher = searcher_from_config(request.registry.settings)
39 39 formatted_results = []
40 40 execution_time = ''
41 41
42 42 schema = search_schema.SearchParamsSchema()
43 43
44 44 search_params = {}
45 45 errors = []
46 46 try:
47 47 search_params = schema.deserialize(
48 48 dict(search_query=request.GET.get('q'),
49 49 search_type=request.GET.get('type'),
50 50 search_sort=request.GET.get('sort'),
51 51 page_limit=request.GET.get('page_limit'),
52 52 requested_page=request.GET.get('page'))
53 53 )
54 54 except validation_schema.Invalid as e:
55 55 errors = e.children
56 56
57 57 def url_generator(**kw):
58 58 q = urllib.quote(safe_str(search_query))
59 59 return update_params(
60 60 "?q=%s&type=%s" % (q, safe_str(search_type)), **kw)
61 61
62 62 c = tmpl_context
63 63 search_query = search_params.get('search_query')
64 64 search_type = search_params.get('search_type')
65 65 search_sort = search_params.get('search_sort')
66 66 if search_params.get('search_query'):
67 67 page_limit = search_params['page_limit']
68 68 requested_page = search_params['requested_page']
69 69
70 70 try:
71 71 search_result = searcher.search(
72 72 search_query, search_type, c.auth_user, repo_name,
73 73 requested_page, page_limit, search_sort)
74 74
75 75 formatted_results = Page(
76 76 search_result['results'], page=requested_page,
77 77 item_count=search_result['count'],
78 78 items_per_page=page_limit, url=url_generator)
79 79 finally:
80 80 searcher.cleanup()
81 81
82 82 if not search_result['error']:
83 83 execution_time = '%s results (%.3f seconds)' % (
84 84 search_result['count'],
85 85 search_result['runtime'])
86 86 elif not errors:
87 87 node = schema['search_query']
88 88 errors = [
89 89 validation_schema.Invalid(node, search_result['error'])]
90 90
91 91 c.perm_user = c.auth_user
92 92 c.repo_name = repo_name
93 93 c.sort = search_sort
94 94 c.url_generator = url_generator
95 95 c.errors = errors
96 96 c.formatted_results = formatted_results
97 97 c.runtime = execution_time
98 98 c.cur_query = search_query
99 99 c.search_type = search_type
100 100 c.searcher = searcher
101 101
102 102
103 103 class SearchView(BaseAppView):
104 104 def load_default_context(self):
105 105 c = self._get_local_tmpl_context()
106 106 self._register_global_c(c)
107 107 return c
108 108
109 109 @LoginRequired()
110 110 @view_config(
111 111 route_name='search', request_method='GET',
112 112 renderer='rhodecode:templates/search/search.mako')
113 113 def search(self):
114 114 c = self.load_default_context()
115 115 search(self.request, c, repo_name=None)
116 116 return self._get_template_context(c)
117 117
118 118
119 119 class SearchRepoView(RepoAppView):
120 120 def load_default_context(self):
121 121 c = self._get_local_tmpl_context()
122 122 self._register_global_c(c)
123 123 return c
124 124
125 125 @LoginRequired()
126 @HasRepoPermissionAnyDecorator('repository.admin')
126 @HasRepoPermissionAnyDecorator(
127 'repository.read', 'repository.write', 'repository.admin')
127 128 @view_config(
128 129 route_name='search_repo', request_method='GET',
129 130 renderer='rhodecode:templates/search/search.mako')
130 131 def search_repo(self):
131 132 c = self.load_default_context()
132 133 search(self.request, c, repo_name=self.db_repo_name)
133 134 return self._get_template_context(c)
General Comments 0
You need to be logged in to leave comments. Login now