本文整理汇总了Python中webob.Response.content_type_params方法的典型用法代码示例。如果您正苦于以下问题:Python Response.content_type_params方法的具体用法?Python Response.content_type_params怎么用?Python Response.content_type_params使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类webob.Response
的用法示例。
在下文中一共展示了Response.content_type_params方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: xls_response
# 需要导入模块: from webob import Response [as 别名]
# 或者: from webob.Response import content_type_params [as 别名]
def xls_response(rows, encoding='ISO-8859-1', filename='response.xls', num_formats=(), col_widths=()):
"""Creates a downloadable webob.Response instance with the given row data
as an XLS (Excel) file.
:param rows: The spreadsheet data as rows.
:type rows: iterable
:param encoding: The character encoding for the data in the spreadsheet.
:type encoding: str
:param filename: The suggested filename for the user to save the file as.
:type filename: str
:param num_formats: A tuple of Excel format strings for numeric values.
:type num_formats: tuple
"""
stream = StringIO()
wb = xlwt.Workbook(encoding=encoding)
ws = wb.add_sheet('Nuorisovaalit 2011')
# Compile the style objects
styles = []
for fmt in num_formats:
if fmt is None:
styles.append(None)
else:
styles.append(xlwt.easyxf(num_format_str=fmt))
# Set column formatting styles.
if len(rows) > 0 and len(num_formats) == len(rows[0]):
for c in xrange(len(rows[0])):
if styles[c] is not None:
ws.col(c).set_style(styles[c])
# Set column widths.
if len(rows) > 0 and len(col_widths) == len(rows[0]):
for c in xrange(len(rows[0])):
if col_widths[c] is not None:
ws.col(c).width = col_widths[c]
for r, row in enumerate(rows):
for c, item in enumerate(row):
if len(styles) == len(row) and styles[c] is not None:
ws.write(r, c, item, styles[c])
else:
ws.write(r, c, item)
wb.save(stream)
response = Response()
response.body = stream.getvalue()
stream.close()
# Set Response headers.
response.content_type = 'application/vnd.ms-excel'
response.content_type_params = {}
response.content_disposition = 'attachment; filename={0}'.format(filename)
return response
示例2: send_legacy_result
# 需要导入模块: from webob import Response [as 别名]
# 或者: from webob.Response import content_type_params [as 别名]
def send_legacy_result(code, headers):
resp = Response()
if 'content-type' not in headers:
headers['content-type'] = "text/plain"
resp.headers = headers
resp.status = code
if code > 399:
return resp
resp.content_type_params = {'charset': 'UTF-8'}
return resp
示例3: send_error
# 需要导入模块: from webob import Response [as 别名]
# 或者: from webob.Response import content_type_params [as 别名]
def send_error(code, req, result):
content = None
resp = Response()
resp.headers['content-type'] = None
resp.status = code
if result:
if is_xml_response(req):
content = result.to_xml()
resp.headers['content-type'] = "application/xml"
else:
content = result.to_json()
resp.headers['content-type'] = "application/json"
resp.content_type_params = {'charset': 'UTF-8'}
resp.unicode_body = content.decode('UTF-8')
return resp
示例4: send_error
# 需要导入模块: from webob import Response [as 别名]
# 或者: from webob.Response import content_type_params [as 别名]
def send_error(code, req, result):
content = None
resp = Response()
resp.headers["content-type"] = None
resp.headers["Vary"] = "X-Auth-Token"
resp.status = code
if result:
if is_xml_response(req):
content = result.to_xml()
resp.headers["content-type"] = "application/xml"
else:
content = result.to_json()
resp.headers["content-type"] = "application/json"
resp.content_type_params = {"charset": "UTF-8"}
resp.unicode_body = content.decode("UTF-8")
return resp
示例5: xls_response_multiple
# 需要导入模块: from webob import Response [as 别名]
# 或者: from webob.Response import content_type_params [as 别名]
def xls_response_multiple(sheets, encoding='ISO-8859-1', filename='response.xls', num_formats=(), col_widths=()):
"""Creates a downloadable webob.Response instance with the given row data
as an XLS (Excel) file.
:param sheets: The spreadsheet data as an iterable of (sheet_name,
rows) tuples with the row data of a sheet in the
rows iterable.
:type sheets: iterable
:param encoding: The character encoding for the data in the spreadsheet.
:type encoding: str
:param filename: The suggested filename for the user to save the file as.
:type filename: str
:param num_formats: A tuple of Excel format strings for numeric values.
:type num_formats: tuple
"""
wb = xlwt.Workbook(encoding=encoding)
# Compile the style objects
styles = []
for fmt in num_formats:
if fmt is None:
styles.append(None)
else:
styles.append(xlwt.easyxf(num_format_str=fmt))
if not sheets:
wb.add_sheet(u'Tyhjä taulukko')
for sheet_name, rows in sheets:
# Worksheet name must not be longer than 31 characters.
if len(sheet_name) > 31:
sheet_name = sheet_name[:31]
# Create a new sheet and write to it.
ws = wb.add_sheet(sheet_name)
# Set column widths.
if len(rows) > 0 and len(col_widths) == len(rows[0]):
for c in xrange(len(rows[0])):
if col_widths[c] is not None:
ws.col(c).width = col_widths[c]
for r, row in enumerate(rows):
for c, item in enumerate(row):
if len(styles) == len(row) and styles[c] is not None:
ws.write(r, c, item, styles[c])
else:
ws.write(r, c, item)
stream = StringIO()
wb.save(stream)
response = Response()
response.body = stream.getvalue()
stream.close()
# Set Response headers.
response.content_type = 'application/vnd.ms-excel'
response.content_type_params = {}
response.content_disposition = 'attachment; filename={0}'.format(filename)
return response
示例6: test_content_type_params_set_ok_param_quoting
# 需要导入模块: from webob import Response [as 别名]
# 或者: from webob.Response import content_type_params [as 别名]
def test_content_type_params_set_ok_param_quoting():
res = Response()
res.content_type_params = {'a':''}
eq_(res.headers['Content-Type'], 'text/html; a=""')
示例7: test_content_type_params_set_value_dict_empty
# 需要导入模块: from webob import Response [as 别名]
# 或者: from webob.Response import content_type_params [as 别名]
def test_content_type_params_set_value_dict_empty():
res = Response()
res.headers['Content-Type'] = 'foo;bar'
res.content_type_params = None
eq_(res.headers['Content-Type'], 'foo')