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 | """Class that implements the reranking on the hotlistissues table page.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import logging |
| 12 | |
| 13 | from features import features_bizobj |
| 14 | from features import hotlist_helpers |
| 15 | from framework import jsonfeed |
| 16 | from framework import permissions |
| 17 | from framework import sorting |
| 18 | from services import features_svc |
| 19 | from tracker import rerank_helpers |
| 20 | |
| 21 | |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 22 | # TODO: convert to FLaskJsonFeed while conver to flask |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 23 | class RerankHotlistIssue(jsonfeed.JsonFeed): |
| 24 | """Rerank an issue in a hotlist.""" |
| 25 | |
| 26 | def AssertBasePermission(self, mr): |
| 27 | super(RerankHotlistIssue, self).AssertBasePermission(mr) |
| 28 | if mr.target_id and mr.moved_ids and mr.split_above: |
| 29 | try: |
| 30 | hotlist = self._GetHotlist(mr) |
| 31 | except features_svc.NoSuchHotlistException: |
| 32 | return |
| 33 | edit_perm = permissions.CanEditHotlist( |
| 34 | mr.auth.effective_ids, mr.perms, hotlist) |
| 35 | if not edit_perm: |
| 36 | raise permissions.PermissionException( |
| 37 | 'User is not allowed to re-rank this hotlist') |
| 38 | |
| 39 | def HandleRequest(self, mr): |
| 40 | changed_ranks = self._GetNewRankings(mr) |
| 41 | |
| 42 | if changed_ranks: |
| 43 | relations_to_change = dict( |
| 44 | (issue_id, rank) for issue_id, rank in changed_ranks) |
| 45 | |
| 46 | self.services.features.UpdateHotlistItemsFields( |
| 47 | mr.cnxn, mr.hotlist_id, new_ranks=relations_to_change) |
| 48 | |
| 49 | hotlist_items = self.services.features.GetHotlist( |
| 50 | mr.cnxn, mr.hotlist_id).items |
| 51 | |
| 52 | # Note: Cannot use mr.hotlist because hotlist_issues |
| 53 | # of mr.hotlist is not updated |
| 54 | |
| 55 | sorting.InvalidateArtValuesKeys( |
| 56 | mr.cnxn, [hotlist_item.issue_id for hotlist_item in hotlist_items]) |
| 57 | (table_data, _) = hotlist_helpers.CreateHotlistTableData( |
| 58 | mr, hotlist_items, self.services) |
| 59 | |
| 60 | json_table_data = [{ |
| 61 | 'cells': [{ |
| 62 | 'type': cell.type, |
| 63 | 'values': [{ |
| 64 | 'item': value.item, |
| 65 | 'isDerived': value.is_derived, |
| 66 | } for value in cell.values], |
| 67 | 'colIndex': cell.col_index, |
| 68 | 'align': cell.align, |
| 69 | 'noWrap': cell.NOWRAP, |
| 70 | 'nonColLabels': [{ |
| 71 | 'value': label.value, |
| 72 | 'isDerived': label.is_derived, |
| 73 | } for label in cell.non_column_labels], |
| 74 | } for cell in table_row.cells], |
| 75 | 'issueRef': table_row.issue_ref, |
| 76 | 'idx': table_row.idx, |
| 77 | 'projectName': table_row.project_name, |
| 78 | 'projectURL': table_row.project_url, |
| 79 | 'localID': table_row.local_id, |
| 80 | 'issueID': table_row.issue_id, |
| 81 | 'isStarred': table_row.starred, |
| 82 | 'issueCleanURL': table_row.issue_clean_url, |
| 83 | 'issueContextURL': table_row.issue_ctx_url, |
| 84 | } for table_row in table_data] |
| 85 | |
| 86 | for row, json_row in zip( |
| 87 | [table_row for table_row in table_data], json_table_data): |
| 88 | if (row.group and row.group.cells): |
| 89 | json_row.update({'group': { |
| 90 | 'rowsInGroup': row.group.rows_in_group, |
| 91 | 'cells': [{'groupName': cell.group_name, |
| 92 | 'values': [{ |
| 93 | # TODO(jojwang): check if this gives error when there |
| 94 | # is no value.item |
| 95 | 'item': value.item if value.item else 'None', |
| 96 | } for value in cell.values], |
| 97 | } for cell in row.group.cells], |
| 98 | }}) |
| 99 | else: |
| 100 | json_row['group'] = 'no' |
| 101 | |
| 102 | return {'table_data': json_table_data} |
| 103 | else: |
| 104 | return {'table_data': ''} |
| 105 | |
| 106 | def _GetHotlist(self, mr): |
| 107 | """Retrieve the current hotlist.""" |
| 108 | if mr.hotlist_id is None: |
| 109 | return None |
| 110 | try: |
| 111 | hotlist = self.services.features.GetHotlist( mr.cnxn, mr.hotlist_id) |
| 112 | except features_svc.NoSuchHotlistException: |
| 113 | self.abort(404, 'hotlist not found') |
| 114 | return hotlist |
| 115 | |
| 116 | def _GetNewRankings(self, mr): |
| 117 | """Compute new issue reference rankings.""" |
| 118 | missing = False |
| 119 | if not (mr.target_id): |
| 120 | logging.info('No target_id.') |
| 121 | missing = True |
| 122 | if not (mr.moved_ids): |
| 123 | logging.info('No moved_ids.') |
| 124 | missing = True |
| 125 | if mr.split_above is None: |
| 126 | logging.info('No split_above.') |
| 127 | missing = True |
| 128 | if missing: |
| 129 | return |
| 130 | |
| 131 | untouched_items = [ |
| 132 | (item.issue_id, item.rank) for item in |
| 133 | mr.hotlist.items if item.issue_id not in mr.moved_ids] |
| 134 | |
| 135 | lower, higher = features_bizobj.SplitHotlistIssueRanks( |
| 136 | mr.target_id, mr.split_above, untouched_items) |
| 137 | return rerank_helpers.GetInsertRankings(lower, higher, mr.moved_ids) |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 138 | |
| 139 | # def GetRerankHotlistIssuePage(self, **kwargs): |
| 140 | # return self.handler(**kwargs) |
| 141 | |
| 142 | # def PostRerankHotlistIssuePage(self, **kwargs): |
| 143 | # return self.handler(**kwargs) |