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