本文整理汇总了Python中piston.emitters.Emitter.get方法的典型用法代码示例。如果您正苦于以下问题:Python Emitter.get方法的具体用法?Python Emitter.get怎么用?Python Emitter.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类piston.emitters.Emitter
的用法示例。
在下文中一共展示了Emitter.get方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: error_resp
# 需要导入模块: from piston.emitters import Emitter [as 别名]
# 或者: from piston.emitters.Emitter import get [as 别名]
def error_resp(message, error_type=None, resp=rc.BAD_REQUEST, status_code=None):
logging.warn("Status Code " + str(resp.status_code) + ", error_type: " + repr(error_type) + ", message: " + repr(message))
error_json = {}
if message and error_type:
(error_type_resp, error_type) = error_type
resp = error_type_resp
error = {}
error['type'] = error_type
error['message'] = message
error_json['error'] = error
elif message:
error = {}
error['type'] = ''
error['message'] = message
error_json['error'] = error
if message or error_type:
emitter, ct = Emitter.get('json') #TODO: Make this work for other emitter formats
srl = emitter(error_json, None, None, None, None)
rendered_resp = srl.render(HttpRequest())
final_resp = HttpResponse(rendered_resp, mimetype=ct)
if status_code:
final_resp.status_code = status_code
else:
final_resp.status_code = resp.status_code
return final_resp
else:
return resp
示例2: apply_json_emitter
# 需要导入模块: from piston.emitters import Emitter [as 别名]
# 或者: from piston.emitters.Emitter import get [as 别名]
def apply_json_emitter(value_to_emit, handler=None):
emitter, ct = Emitter.get('json') #TODO: Make this work for other emitter formats
handler_fields = None
if handler:
handler_fields = handler.fields
srl = emitter(value_to_emit, typemapper, handler, handler_fields, None)
json = srl.construct()
return json
示例3: render_created
# 需要导入模块: from piston.emitters import Emitter [as 别名]
# 或者: from piston.emitters.Emitter import get [as 别名]
def render_created(self, request, new_obj, new_url):
"""Quick hack to return a 201 Created along with a JSON rendering of
what was created"""
resp = rc.CREATED
emitter, ct = Emitter.get('json')
resp['Content-Type'] = ct
resp['Location'] = new_url
srl = emitter(new_obj, typemapper, self, self.fields, False)
resp.content = srl.render(request)
return resp
示例4: form_validation_response
# 需要导入模块: from piston.emitters import Emitter [as 别名]
# 或者: from piston.emitters.Emitter import get [as 别名]
def form_validation_response(self, e, request,em_format):
print 'in form_validation_response 1'
try:
emitter, ct = Emitter.get(em_format)
fields = self.handler.fields
except ValueError:
result = piston.utils.rc.BAD_REQUEST
result.content = "Invalid output format specified '%s'." % em_format
return result
serialized_errors = dict((key, [unicode(v) for v in values])
for key,values in e.form.errors.items())
srl = emitter(serialized_errors, piston.handler.typemapper, self.handler, fields, False)
stream = srl.render(request)
resp = HttpResponse(stream, mimetype=ct, status=400)
return resp
示例5: render
# 需要导入模块: from piston.emitters import Emitter [as 别名]
# 或者: from piston.emitters.Emitter import get [as 别名]
def render(self, request, *args, **kwargs):
f = StringIO.StringIO()
out = csv.writer(f,dialect='excel')
c = self.construct()
if not bool(request.REQUEST.get('headerless', False)):
out.writerow(self.fields)
def mapper(row):
return [row[key] for key in self.fields]
out.writerows(map(mapper, c))
# In this case we'll hanlde the HttpResponse wrapping.
_, ct = Emitter.get('csv')
response = HttpResponse(f.getvalue(), ct)
response['Content-Disposition'] = 'attachment; filename=irack.csv'
return response
示例6: __call__
# 需要导入模块: from piston.emitters import Emitter [as 别名]
# 或者: from piston.emitters.Emitter import get [as 别名]
def __call__(self, request, result, handler):
if isinstance(result, ErrorDict):
result = dictFromErrorDict(result)
resource = handler.resource
em_format = resource.determine_emitter(request)
emitter, ct = Emitter.get(em_format)
srl = emitter(result, typemapper, handler, handler.fields, anonymous=True)
try:
if resource.stream:
stream = srl.stream_render(request)
else:
stream = srl.render(request)
self.__init__(stream, mimetype=ct)
self.streaming = resource.stream
except HttpStatusCode, e:
return e.response
示例7: return
# 需要导入模块: from piston.emitters import Emitter [as 别名]
# 或者: from piston.emitters.Emitter import get [as 别名]
assert range_end != None
return (range_start, range_end)
try:
total = result.count()
start, end = get_range(request_range[0], request_range[1], total)
result = result[start:end + 1]
content_range = "items %i-%i/%i" % (start, end, total)
except BadRangeException, e:
resp = rc.BAD_RANGE
resp.write("\n%s" % e.value)
return resp
#end paging
emitter, ct = Emitter.get(em_format)
fields = handler.fields
if hasattr(handler, 'list_fields') and (
isinstance(result, list) or isinstance(result, QuerySet)):
fields = handler.list_fields
srl = emitter(result, typemapper, handler, fields, anonymous)
try:
"""
Decide whether or not we want a generator here,
or we just want to buffer up the entire result
before sending it to the client. Won't matter for
smaller datasets, but larger will have an impact.
"""
if self.stream: