blob: 30c80d3b7ae8acf0b8d2aac49cb2a439334ff735 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# Copyright 2016 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Classes that implement an admin utility to re-index issues in bulk."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import logging
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020011from six.moves import urllib
Copybara854996b2021-09-07 19:36:02 +000012
13import settings
14from framework import permissions
15from framework import servlet
16from framework import urls
17from services import tracker_fulltext
18
19
20class IssueReindex(servlet.Servlet):
21 """IssueReindex shows a form to request that issues be indexed."""
22
23 _PAGE_TEMPLATE = 'tracker/issue-reindex-page.ezt'
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010024 _MAIN_TAB_MODE = servlet.Servlet.MAIN_TAB_ISSUES
Copybara854996b2021-09-07 19:36:02 +000025
26 def AssertBasePermission(self, mr):
27 """Check whether the user has any permission to visit this page.
28
29 Args:
30 mr: commonly used info parsed from the request.
31 """
32 super(IssueReindex, self).AssertBasePermission(mr)
33 if not self.CheckPerm(mr, permissions.EDIT_PROJECT):
34 raise permissions.PermissionException(
35 'You are not allowed to administer this project')
36
37 def GatherPageData(self, mr):
38 """Build up a dictionary of data values to use when rendering the page.
39
40 Args:
41 mr: commonly used info parsed from the request.
42
43 Returns:
44 Dict of values used by EZT for rendering the page.
45 """
46 return {
47 # start and num are already passed to the template.
48 'issue_tab_mode': None,
49 'auto_submit': mr.auto_submit,
50 'page_perms': self.MakePagePerms(mr, None, permissions.CREATE_ISSUE),
51 }
52
53 def ProcessFormData(self, mr, post_data):
54 """Process a posted issue reindex form.
55
56 Args:
57 mr: commonly used info parsed from the request.
58 post_data: HTML form data from the request.
59
60 Returns:
61 String URL to redirect the user to after processing. The URL will contain
62 a new start that is auto-incremented using the specified num value.
63 """
64 start = max(0, int(post_data['start']))
65 num = max(0, min(settings.max_artifact_search_results_per_page,
66 int(post_data['num'])))
67
68 issues = self.services.issue.GetIssuesByLocalIDs(
69 mr.cnxn, mr.project_id, list(range(start, start + num)))
70 logging.info('got %d issues to index', len(issues))
71 if issues:
72 tracker_fulltext.IndexIssues(
73 mr.cnxn, issues, self.services.user, self.services.issue,
74 self.services.config)
75
76 # Make the browser keep submitting the form, if the user wants that,
77 # and we have not run out of issues to process.
78 auto_submit = issues and ('auto_submit' in post_data)
79
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010080 query_map = (
81 ('start', start + num), # auto-increment start.
82 ('num', num),
83 ('auto_submit', bool(auto_submit)),
84 )
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020085 return '/p/%s%s?%s' % (
86 mr.project_name, urls.ISSUE_REINDEX, urllib.parse.urlencode(query_map))
87
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010088 def GetIssueReindex(self, **kwargs):
89 return self.handler(**kwargs)
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020090
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010091 def PostIssueReindex(self, **kwargs):
92 return self.handler(**kwargs)