本文整理汇总了Python中onadata.apps.viewer.models.parsed_instance.ParsedInstance.query_mongo_no_paging方法的典型用法代码示例。如果您正苦于以下问题:Python ParsedInstance.query_mongo_no_paging方法的具体用法?Python ParsedInstance.query_mongo_no_paging怎么用?Python ParsedInstance.query_mongo_no_paging使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类onadata.apps.viewer.models.parsed_instance.ParsedInstance
的用法示例。
在下文中一共展示了ParsedInstance.query_mongo_no_paging方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: modify
# 需要导入模块: from onadata.apps.viewer.models.parsed_instance import ParsedInstance [as 别名]
# 或者: from onadata.apps.viewer.models.parsed_instance.ParsedInstance import query_mongo_no_paging [as 别名]
def modify(self, request, *args, **kwargs):
xform = self.get_object()
http_status = status.HTTP_200_OK
response = {}
if request.user.has_perm("validate_xform", xform):
owner = xform.user
userform_id = "{}_{}".format(owner.username, xform.id_string)
query = {ParsedInstance.USERFORM_ID: userform_id} # Query used for MongoDB
filter_ = {"xform_id": xform.id} # Filter for Django ORM
payload = {}
try:
payload = json.loads(request.data.get("payload", "{}"))
except ValueError:
http_status = status.HTTP_400_BAD_REQUEST
response = {"detail": _("Invalid payload")}
if http_status == status.HTTP_200_OK:
new_validation_status_uid = payload.get("validation_status.uid")
if new_validation_status_uid is None:
http_status = status.HTTP_400_BAD_REQUEST
response = {"detail": _("No validation_status.uid provided")}
else:
# Create new validation_status object
new_validation_status = get_validation_status(
new_validation_status_uid, xform, request.user.username)
# 3 scenarios to update submissions
# First scenario / Modify submissions based on user's query
if payload.get("query"):
# Validate if query is valid.
try:
query.update(payload.get("query"))
except ValueError:
raise ParseError(_("Invalid query: %(query)s"
% {'query': json.dumps(payload.get("query"))}))
query_kwargs = {
"query": json.dumps(query),
"fields": '["_id"]'
}
cursor = ParsedInstance.query_mongo_no_paging(**query_kwargs)
submissions_ids = [record.get("_id") for record in list(cursor)]
filter_.update({"id__in": submissions_ids})
# Second scenario / Modify submissions based on list of ids
elif payload.get("submissions_ids"):
try:
# Use int() to test if list of integers is valid.
submissions_ids = payload.get("submissions_ids", [])
or_ = {u"$or": [{u"_id": int(submission_id)} for submission_id in submissions_ids]}
query.update(or_)
except ValueError:
raise ParseError(_("Invalid submissions ids: %(submissions_ids)s"
% {'submissions_ids': json.dumps(payload.get("submissions_ids"))}))
filter_.update({"id__in": submissions_ids})
# Third scenario / Modify all submissions in form, but confirmation param must be among payload
elif payload.get("confirm", False) is not True:
http_status = status.HTTP_400_BAD_REQUEST
response = {"detail": _("No confirmations provided")}
# If everything is OK, submit data to DBs
if http_status == status.HTTP_200_OK:
# Update Postgres & Mongo
updated_records_count = Instance.objects.\
filter(**filter_).update(validation_status=new_validation_status)
ParsedInstance.bulk_update_validation_statuses(query, new_validation_status)
response = {"detail": _("{} submissions have been updated").format(updated_records_count)}
return Response(response, http_status)
else:
raise PermissionDenied(_(u"You do not have validate permissions."))