blob: 04fdb52bb59b2f3d927a7f4b41f86287f994008d [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# 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.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Implemention of the hotlist issues list output as a CSV file."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10from features import hotlistissues
11from framework import framework_views
12from framework import csv_helpers
13from framework import permissions
14from framework import xsrf
15
16
17# TODO(jojwang): can be refactored even more, see similarities with
18# IssueListCsv
19class 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ínezde942802022-07-15 14:06:55 +020062
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010063 def GetHotlistIssuesCsvPage(self, **kwargs):
64 return self.handler(**kwargs)