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 |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 12 | from six.moves import urllib |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 13 | |
| 14 | import settings |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 15 | from framework import flaskservlet |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 16 | from framework import permissions |
| 17 | from framework import servlet |
| 18 | from framework import urls |
| 19 | from services import tracker_fulltext |
| 20 | |
| 21 | |
| 22 | class IssueReindex(servlet.Servlet): |
| 23 | """IssueReindex shows a form to request that issues be indexed.""" |
| 24 | |
| 25 | _PAGE_TEMPLATE = 'tracker/issue-reindex-page.ezt' |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 26 | _MAIN_TAB_MODE = flaskservlet.FlaskServlet.MAIN_TAB_ISSUES |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 27 | |
| 28 | def AssertBasePermission(self, mr): |
| 29 | """Check whether the user has any permission to visit this page. |
| 30 | |
| 31 | Args: |
| 32 | mr: commonly used info parsed from the request. |
| 33 | """ |
| 34 | super(IssueReindex, self).AssertBasePermission(mr) |
| 35 | if not self.CheckPerm(mr, permissions.EDIT_PROJECT): |
| 36 | raise permissions.PermissionException( |
| 37 | 'You are not allowed to administer this project') |
| 38 | |
| 39 | def GatherPageData(self, mr): |
| 40 | """Build up a dictionary of data values to use when rendering the page. |
| 41 | |
| 42 | Args: |
| 43 | mr: commonly used info parsed from the request. |
| 44 | |
| 45 | Returns: |
| 46 | Dict of values used by EZT for rendering the page. |
| 47 | """ |
| 48 | return { |
| 49 | # start and num are already passed to the template. |
| 50 | 'issue_tab_mode': None, |
| 51 | 'auto_submit': mr.auto_submit, |
| 52 | 'page_perms': self.MakePagePerms(mr, None, permissions.CREATE_ISSUE), |
| 53 | } |
| 54 | |
| 55 | def ProcessFormData(self, mr, post_data): |
| 56 | """Process a posted issue reindex form. |
| 57 | |
| 58 | Args: |
| 59 | mr: commonly used info parsed from the request. |
| 60 | post_data: HTML form data from the request. |
| 61 | |
| 62 | Returns: |
| 63 | String URL to redirect the user to after processing. The URL will contain |
| 64 | a new start that is auto-incremented using the specified num value. |
| 65 | """ |
| 66 | start = max(0, int(post_data['start'])) |
| 67 | num = max(0, min(settings.max_artifact_search_results_per_page, |
| 68 | int(post_data['num']))) |
| 69 | |
| 70 | issues = self.services.issue.GetIssuesByLocalIDs( |
| 71 | mr.cnxn, mr.project_id, list(range(start, start + num))) |
| 72 | logging.info('got %d issues to index', len(issues)) |
| 73 | if issues: |
| 74 | tracker_fulltext.IndexIssues( |
| 75 | mr.cnxn, issues, self.services.user, self.services.issue, |
| 76 | self.services.config) |
| 77 | |
| 78 | # Make the browser keep submitting the form, if the user wants that, |
| 79 | # and we have not run out of issues to process. |
| 80 | auto_submit = issues and ('auto_submit' in post_data) |
| 81 | |
| 82 | query_map = { |
| 83 | 'start': start + num, # auto-increment start. |
| 84 | 'num': num, |
| 85 | 'auto_submit': bool(auto_submit), |
| 86 | } |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 87 | return '/p/%s%s?%s' % ( |
| 88 | mr.project_name, urls.ISSUE_REINDEX, urllib.parse.urlencode(query_map)) |
| 89 | |
| 90 | # def GetIssueReindex(self, **kwargs): |
| 91 | # return self.handler(**kwargs) |
| 92 | |
| 93 | # def PostIssueReindex(self, **kwargs): |
| 94 | # return self.handler(**kwargs) |