Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | # 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. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Classes that implement an admin utility to re-index issues in bulk.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import logging |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 11 | from six.moves import urllib |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 12 | |
| 13 | import settings |
| 14 | from framework import permissions |
| 15 | from framework import servlet |
| 16 | from framework import urls |
| 17 | from services import tracker_fulltext |
| 18 | |
| 19 | |
| 20 | class 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ínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 24 | _MAIN_TAB_MODE = servlet.Servlet.MAIN_TAB_ISSUES |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 25 | |
| 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ínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 80 | query_map = ( |
| 81 | ('start', start + num), # auto-increment start. |
| 82 | ('num', num), |
| 83 | ('auto_submit', bool(auto_submit)), |
| 84 | ) |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 85 | return '/p/%s%s?%s' % ( |
| 86 | mr.project_name, urls.ISSUE_REINDEX, urllib.parse.urlencode(query_map)) |
| 87 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 88 | def GetIssueReindex(self, **kwargs): |
| 89 | return self.handler(**kwargs) |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 90 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 91 | def PostIssueReindex(self, **kwargs): |
| 92 | return self.handler(**kwargs) |