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 | """Implemention of the hotlist issues list output as a CSV file.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | from features import hotlistissues |
| 12 | from framework import framework_views |
| 13 | from framework import csv_helpers |
| 14 | from framework import permissions |
| 15 | from framework import xsrf |
| 16 | |
| 17 | |
| 18 | # TODO(jojwang): can be refactored even more, see similarities with |
| 19 | # IssueListCsv |
| 20 | class HotlistIssuesCsv(hotlistissues.HotlistIssues): |
| 21 | """HotlistIssuesCsv provides to the user a list of issues as a CSV document. |
| 22 | |
| 23 | Overrides the standard HotlistIssues servlet but uses a different EZT template |
| 24 | to provide the same content as the HotlistIssues only as CSV. Adds the HTTP |
| 25 | header to offer the result as a download. |
| 26 | """ |
| 27 | |
| 28 | _PAGE_TEMPLATE = 'tracker/issue-list-csv.ezt' |
| 29 | |
| 30 | def GatherPageData(self, mr): |
| 31 | if not mr.auth.user_id: |
| 32 | raise permissions.PermissionException( |
| 33 | 'Anonymous users are not allowed to download hotlist CSV') |
| 34 | |
| 35 | owner_id = mr.hotlist.owner_ids[0] # only one owner allowed |
| 36 | users_by_id = framework_views.MakeAllUserViews( |
| 37 | mr.cnxn, self.services.user, |
| 38 | [owner_id]) |
| 39 | owner = users_by_id[owner_id] |
| 40 | |
| 41 | # Try to validate XSRF by either user email or user ID. |
| 42 | try: |
| 43 | xsrf.ValidateToken( |
| 44 | mr.token, mr.auth.user_id, |
| 45 | '/u/%s/hotlists/%s.do' % (owner.email, mr.hotlist.name)) |
| 46 | except xsrf.TokenIncorrect: |
| 47 | xsrf.ValidateToken( |
| 48 | mr.token, mr.auth.user_id, |
| 49 | '/u/%s/hotlists/%s.do' % (owner.user_id, mr.hotlist.name)) |
| 50 | |
| 51 | # Sets headers to allow the response to be downloaded. |
| 52 | self.content_type = 'text/csv; charset=UTF-8' |
| 53 | download_filename = 'hotlist_%d-issues.csv' % mr.hotlist_id |
| 54 | self.response.headers.add( |
| 55 | 'Content-Disposition', 'attachment; filename=%s' % download_filename) |
| 56 | self.response.headers.add('X-Content-Type-Options', 'nosniff') |
| 57 | |
| 58 | mr.ComputeColSpec(mr.hotlist) |
| 59 | mr.col_spec = csv_helpers.RewriteColspec(mr.col_spec) |
| 60 | page_data = hotlistissues.HotlistIssues.GatherPageData(self, mr) |
| 61 | return csv_helpers.ReformatRowsForCSV( |
| 62 | mr, page_data, '%d/csv' % mr.hotlist_id) |