本文整理汇总了Python中avocado.models.DataQuery.json方法的典型用法代码示例。如果您正苦于以下问题:Python DataQuery.json方法的具体用法?Python DataQuery.json怎么用?Python DataQuery.json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类avocado.models.DataQuery
的用法示例。
在下文中一共展示了DataQuery.json方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_request_query
# 需要导入模块: from avocado.models import DataQuery [as 别名]
# 或者: from avocado.models.DataQuery import json [as 别名]
def get_request_query(request, attrs=None):
"""
Resolves the appropriate DataQuery object for use from the request.
"""
# Attempt to derive the `attrs` from the request
if attrs is None:
if request.method == 'POST':
attrs = request.data.get('query')
elif request.method == 'GET':
attrs = request.GET.get('query')
# If the `attrs` could not be derived from the request(meaning no query
# was explicity defined), try to construct the query by deriving a context
# and view from the request.
if attrs is None:
json = {}
context = get_request_context(request)
if context:
json['context'] = context.json
view = get_request_view(request)
if view:
json['view'] = view.json
return DataQuery(json)
# If `attrs` were derived or supplied then validate them and return a
# DataQuery based off the `attrs`.
if isinstance(attrs, dict):
# We cannot simply validate and create a DataQuery based off the
# `attrs` as they are now because the context and or view might not
# contain json but might instead be a pk or some other value. Use the
# internal helper methods to construct the context and view objects
# and build the query from the json of those objects' json.
json = {}
context = get_request_context(request, attrs=attrs)
if context:
json['context'] = context.json
view = get_request_view(request, attrs=attrs)
if view:
json['view'] = view.json
DataQuery.validate(json)
return DataQuery(json)
kwargs = {}
# If an authenticated user made the request, filter by the user or
# fallback to an active session key.
if getattr(request, 'user', None) and request.user.is_authenticated():
kwargs['user'] = request.user
else:
# If not session has been created, this is a cookie-less user agent
# which is most likely a bot or a non-browser client (e.g. cURL).
if request.session.session_key is None:
return DataQuery()
kwargs['session_key'] = request.session.session_key
# Assume it is a primary key and fallback to the sesssion
try:
kwargs['pk'] = int(attrs)
except (ValueError, TypeError):
kwargs['session'] = True
try:
return DataQuery.objects.get(**kwargs)
except DataQuery.DoesNotExist:
pass
# Fallback to an instance based off the default template if one exists
instance = DataQuery()
default = DataQuery.objects.get_default_template()
if default:
instance.json = default.json
return instance