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