本文整理汇总了Python中onadata.apps.viewer.models.parsed_instance.ParsedInstance.query_mongo_minimal方法的典型用法代码示例。如果您正苦于以下问题:Python ParsedInstance.query_mongo_minimal方法的具体用法?Python ParsedInstance.query_mongo_minimal怎么用?Python ParsedInstance.query_mongo_minimal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类onadata.apps.viewer.models.parsed_instance.ParsedInstance
的用法示例。
在下文中一共展示了ParsedInstance.query_mongo_minimal方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: to_representation
# 需要导入模块: from onadata.apps.viewer.models.parsed_instance import ParsedInstance [as 别名]
# 或者: from onadata.apps.viewer.models.parsed_instance.ParsedInstance import query_mongo_minimal [as 别名]
def to_representation(self, obj):
request = self.context.get('request')
if not isinstance(obj, XForm):
return super(DataListSerializer, self).to_representation(obj)
query_params = (request and request.query_params) or {}
query = {
ParsedInstance.USERFORM_ID:
u'%s_%s' % (obj.user.username, obj.id_string)
}
try:
query.update(json.loads(query_params.get('query', '{}')))
except ValueError:
raise ParseError(_("Invalid query: %(query)s"
% {'query': query_params.get('query')}))
query_kwargs = {
'query': json.dumps(query),
'fields': query_params.get('fields'),
'sort': query_params.get('sort')
}
cursor = ParsedInstance.query_mongo_minimal(**query_kwargs)
return list(cursor)
示例2: to_native
# 需要导入模块: from onadata.apps.viewer.models.parsed_instance import ParsedInstance [as 别名]
# 或者: from onadata.apps.viewer.models.parsed_instance.ParsedInstance import query_mongo_minimal [as 别名]
def to_native(self, obj):
request = self.context.get('request')
if obj is None:
return super(DataListSerializer, self).to_native(obj)
query_params = (request and request.QUERY_PARAMS) or {}
query = {
ParsedInstance.USERFORM_ID:
u'%s_%s' % (obj.user.username, obj.id_string)
}
try:
query.update(json.loads(query_params.get('query', '{}')))
except ValueError:
raise ParseError(_("Invalid query: %(query)s"
% {'query': query_params.get('query')}))
query_kwargs = {
'query': json.dumps(query),
'fields': query_params.get('fields'),
'sort': query_params.get('sort')
}
cursor = ParsedInstance.query_mongo_minimal(**query_kwargs)
records = list(record for record in cursor)
return records
示例3: to_native
# 需要导入模块: from onadata.apps.viewer.models.parsed_instance import ParsedInstance [as 别名]
# 或者: from onadata.apps.viewer.models.parsed_instance.ParsedInstance import query_mongo_minimal [as 别名]
def to_native(self, obj):
request = self.context.get('request')
query_params = (request and request.QUERY_PARAMS) or {}
query = {
ParsedInstance.USERFORM_ID:
u'%s_%s' % (obj.xform.user.username, obj.xform.id_string),
u'_id': obj.pk
}
query_kwargs = {
'query': json.dumps(query),
'fields': query_params.get('fields'),
'sort': query_params.get('sort')
}
cursor = ParsedInstance.query_mongo_minimal(**query_kwargs)
records = list(record for record in cursor)
return (len(records) and records[0]) or records
示例4: to_representation
# 需要导入模块: from onadata.apps.viewer.models.parsed_instance import ParsedInstance [as 别名]
# 或者: from onadata.apps.viewer.models.parsed_instance.ParsedInstance import query_mongo_minimal [as 别名]
def to_representation(self, obj):
request = self.context.get('request')
if not isinstance(obj, XForm):
return super(DataListSerializer, self).to_representation(obj)
query_params = (request and request.query_params) or {}
query = {
ParsedInstance.USERFORM_ID:
u'%s_%s' % (obj.user.username, obj.id_string)
}
limit = query_params.get('limit', False)
start = query_params.get('start', False)
count = query_params.get('count', False)
try:
query.update(json.loads(query_params.get('query', '{}')))
except ValueError:
raise ParseError(_("Invalid query: %(query)s"
% {'query': query_params.get('query')}))
query_kwargs = {
'query': json.dumps(query),
'fields': query_params.get('fields'),
'sort': query_params.get('sort')
}
# if we want the count, we don't kwow to paginate the records.
# start and limit are useless then.
if count:
query_kwargs['count'] = True
else:
if limit:
query_kwargs['limit'] = int(limit)
if start:
query_kwargs['start'] = int(start)
cursor = ParsedInstance.query_mongo_minimal(**query_kwargs)
# if we want the count, we only need the first index of the list.
if count:
return cursor[0]
else:
return [MongoHelper.to_readable_dict(record) for record in cursor]