Total Complexity | 4 |
Total Lines | 23 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | # http://docs.pylonsproject.org/projects/pyramid-cookbook/en/latest/templates/customrenderers.html |
||
6 | class CSVRenderer(object): |
||
7 | def __init__(self, info): |
||
8 | pass |
||
9 | |||
10 | def __call__(self, value, system): |
||
11 | """ Returns a plain CSV-encoded string with content-type |
||
12 | ``text/csv``. The content-type may be overridden by |
||
13 | setting ``request.response.content_type``.""" |
||
14 | |||
15 | request = system.get('request') |
||
16 | if request is not None: |
||
17 | response = request.response |
||
18 | ct = response.content_type |
||
19 | if ct == response.default_content_type: |
||
20 | response.content_type = 'text/csv' |
||
21 | |||
22 | fout = io.StringIO() |
||
23 | writer = csv.writer(fout, delimiter=',', quotechar=',', quoting=csv.QUOTE_MINIMAL) |
||
24 | |||
25 | writer.writerow(value.get('header', [])) |
||
26 | writer.writerows(value.get('rows', [])) |
||
27 | |||
28 | return fout.getvalue() |
||
29 | |||
30 |