本文整理汇总了Python中onadata.apps.viewer.models.parsed_instance.ParsedInstance类的典型用法代码示例。如果您正苦于以下问题:Python ParsedInstance类的具体用法?Python ParsedInstance怎么用?Python ParsedInstance使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ParsedInstance类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_data
def _get_data(self, query, fields, sort, start, limit, is_public_request):
try:
where, where_params = ParsedInstance._get_where_clause(query)
except ValueError as e:
raise ParseError(unicode(e))
if where:
self.object_list = self.object_list.extra(where=where,
params=where_params)
if (sort or limit or start or fields) and not is_public_request:
if self.object_list.count():
xform = self.object_list[0].xform
self.object_list = \
ParsedInstance.query_data(
xform, query=query, sort=sort,
start_index=start, limit=limit,
fields=fields)
if not isinstance(self.object_list, types.GeneratorType):
page = self.paginate_queryset(self.object_list)
serializer = self.get_pagination_serializer(page)
else:
serializer = self.get_serializer(self.object_list, many=True)
page = None
return Response(serializer.data)
示例2: test_edit_updated_geopoint_cache
def test_edit_updated_geopoint_cache(self):
query_args = {
'xform': self.xform,
'query': '{}',
'fields': '[]',
'count': True
}
xml_submission_file_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"..", "fixtures", "tutorial", "instances",
"tutorial_2012-06-27_11-27-53_w_uuid.xml"
)
self._make_submission(xml_submission_file_path)
self.assertEqual(self.response.status_code, 201)
# query mongo for the _geopoint field
query_args['count'] = False
records = ParsedInstance.query_data(**query_args)
self.assertEqual(len(records), 1)
# submit the edited instance
xml_submission_file_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"..", "fixtures", "tutorial", "instances",
"tutorial_2012-06-27_11-27-53_w_uuid_edited.xml"
)
self._make_submission(xml_submission_file_path)
self.assertEqual(self.response.status_code, 201)
records = ParsedInstance.query_data(**query_args)
self.assertEqual(len(records), 1)
cached_geopoint = records[0][GEOLOCATION]
# the cached geopoint should equal the gps field
gps = records[0]['gps'].split(" ")
self.assertEqual(float(gps[0]), float(cached_geopoint[0]))
self.assertEqual(float(gps[1]), float(cached_geopoint[1]))
示例3: test_deleted_submission_not_in_export
def test_deleted_submission_not_in_export(self):
self._publish_transportation_form()
initial_count = ParsedInstance.query_mongo(
self.user.username, self.xform.id_string, '{}', '[]', '{}',
count=True)[0]['count']
self._submit_transport_instance(0)
self._submit_transport_instance(1)
count = ParsedInstance.query_mongo(
self.user.username, self.xform.id_string, '{}', '[]', '{}',
count=True)[0]['count']
self.assertEqual(count, initial_count + 2)
# get id of second submission
instance_id = Instance.objects.filter(
xform=self.xform).order_by('id').reverse()[0].id
delete_url = reverse(
delete_data, kwargs={"username": self.user.username,
"id_string": self.xform.id_string})
params = {'id': instance_id}
self.client.post(delete_url, params)
count = ParsedInstance.query_mongo(
self.user.username, self.xform.id_string, '{}', '[]', '{}',
count=True)[0]['count']
self.assertEqual(count, initial_count + 1)
# create the export
csv_export_url = reverse(
'csv_export', kwargs={"username": self.user.username,
"id_string": self.xform.id_string})
response = self.client.get(csv_export_url)
self.assertEqual(response.status_code, 200)
f = StringIO.StringIO(self._get_response_content(response))
csv_reader = csv.reader(f)
num_rows = len([row for row in csv_reader])
f.close()
# number of rows == 2 i.e. initial_count + header plus one row
self.assertEqual(num_rows, initial_count + 2)
示例4: test_edited_submission
def test_edited_submission(self):
"""
Test submissions that have been edited
"""
xml_submission_file_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"..",
"fixtures",
"tutorial",
"instances",
"tutorial_2012-06-27_11-27-53_w_uuid.xml",
)
num_instances_history = InstanceHistory.objects.count()
num_instances = Instance.objects.count()
query_args = {
"username": self.user.username,
"id_string": self.xform.id_string,
"query": "{}",
"fields": "[]",
"sort": "[]",
"count": True,
}
cursor = ParsedInstance.query_mongo(**query_args)
num_mongo_instances = cursor[0]["count"]
# make first submission
self._make_submission(xml_submission_file_path)
self.assertEqual(self.response.status_code, 201)
self.assertEqual(Instance.objects.count(), num_instances + 1)
# no new record in instances history
self.assertEqual(InstanceHistory.objects.count(), num_instances_history)
# check count of mongo instances after first submission
cursor = ParsedInstance.query_mongo(**query_args)
self.assertEqual(cursor[0]["count"], num_mongo_instances + 1)
# edited submission
xml_submission_file_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"..",
"fixtures",
"tutorial",
"instances",
"tutorial_2012-06-27_11-27-53_w_uuid_edited.xml",
)
self._make_submission(xml_submission_file_path)
self.assertEqual(self.response.status_code, 201)
# we must have the same number of instances
self.assertEqual(Instance.objects.count(), num_instances + 1)
# should be a new record in instances history
self.assertEqual(InstanceHistory.objects.count(), num_instances_history + 1)
cursor = ParsedInstance.query_mongo(**query_args)
self.assertEqual(cursor[0]["count"], num_mongo_instances + 1)
# make sure we edited the mongo db record and NOT added a new row
query_args["count"] = False
cursor = ParsedInstance.query_mongo(**query_args)
record = cursor[0]
with open(xml_submission_file_path, "r") as f:
xml_str = f.read()
xml_str = clean_and_parse_xml(xml_str).toxml()
edited_name = re.match(ur"^.+?<name>(.+?)</name>", xml_str).groups()[0]
self.assertEqual(record["name"], edited_name)
示例5: test_edited_submission
def test_edited_submission(self):
"""
Test submissions that have been edited
"""
xml_submission_file_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"..", "fixtures", "tutorial", "instances",
"tutorial_2012-06-27_11-27-53_w_uuid.xml"
)
num_instances_history = InstanceHistory.objects.count()
num_instances = Instance.objects.count()
query_args = {
'username': self.user.username,
'id_string': self.xform.id_string,
'query': '{}',
'fields': '[]',
'sort': '[]',
'count': True
}
cursor = ParsedInstance.query_mongo(**query_args)
num_mongo_instances = cursor[0]['count']
# make first submission
self._make_submission(xml_submission_file_path)
self.assertEqual(self.response.status_code, 201)
self.assertEqual(Instance.objects.count(), num_instances + 1)
# no new record in instances history
self.assertEqual(
InstanceHistory.objects.count(), num_instances_history)
# check count of mongo instances after first submission
cursor = ParsedInstance.query_mongo(**query_args)
self.assertEqual(cursor[0]['count'], num_mongo_instances + 1)
# edited submission
xml_submission_file_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"..", "fixtures", "tutorial", "instances",
"tutorial_2012-06-27_11-27-53_w_uuid_edited.xml"
)
client = DigestClient()
client.set_authorization('bob', 'bob', 'Digest')
self._make_submission(xml_submission_file_path, client=client)
self.assertEqual(self.response.status_code, 201)
# we must have the same number of instances
self.assertEqual(Instance.objects.count(), num_instances + 1)
# should be a new record in instances history
self.assertEqual(
InstanceHistory.objects.count(), num_instances_history + 1)
cursor = ParsedInstance.query_mongo(**query_args)
self.assertEqual(cursor[0]['count'], num_mongo_instances + 1)
# make sure we edited the mongo db record and NOT added a new row
query_args['count'] = False
cursor = ParsedInstance.query_mongo(**query_args)
record = cursor[0]
with open(xml_submission_file_path, "r") as f:
xml_str = f.read()
xml_str = clean_and_parse_xml(xml_str).toxml()
edited_name = re.match(ur"^.+?<name>(.+?)</name>", xml_str).groups()[0]
self.assertEqual(record['name'], edited_name)
示例6: to_representation
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)
示例7: to_native
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
示例8: api
def api(request, username=None, id_string=None):
"""
Returns all results as JSON. If a parameter string is passed,
it takes the 'query' parameter, converts this string to a dictionary, an
that is then used as a MongoDB query string.
NOTE: only a specific set of operators are allow, currently $or and $and.
Please send a request if you'd like another operator to be enabled.
NOTE: Your query must be valid JSON, double check it here,
http://json.parser.online.fr/
E.g. api?query='{"last_name": "Smith"}'
"""
if request.method == "OPTIONS":
response = HttpResponse()
add_cors_headers(response)
return response
helper_auth_helper(request)
helper_auth_helper(request)
xform, owner = check_and_set_user_and_form(username, id_string, request)
if not xform:
return HttpResponseForbidden(_(u'Not shared.'))
try:
args = {
'username': username,
'id_string': id_string,
'query': request.GET.get('query'),
'fields': request.GET.get('fields'),
'sort': request.GET.get('sort')
}
if 'start' in request.GET:
args["start"] = int(request.GET.get('start'))
if 'limit' in request.GET:
args["limit"] = int(request.GET.get('limit'))
if 'count' in request.GET:
args["count"] = True if int(request.GET.get('count')) > 0\
else False
cursor = ParsedInstance.query_mongo(**args)
except ValueError as e:
return HttpResponseBadRequest(e.__str__())
records = list(record for record in cursor)
response_text = json_util.dumps(records)
if 'callback' in request.GET and request.GET.get('callback') != '':
callback = request.GET.get('callback')
response_text = ("%s(%s)" % (callback, response_text))
response = HttpResponse(response_text, mimetype='application/json')
add_cors_headers(response)
return response
示例9: test_edit_updated_geopoint_cache
def test_edit_updated_geopoint_cache(self):
query_args = {
"username": self.user.username,
"id_string": self.xform.id_string,
"query": "{}",
"fields": "[]",
"sort": "[]",
"count": True,
}
xml_submission_file_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"..",
"fixtures",
"tutorial",
"instances",
"tutorial_2012-06-27_11-27-53_w_uuid.xml",
)
self._make_submission(xml_submission_file_path)
self.assertEqual(self.response.status_code, 201)
# query mongo for the _geopoint field
query_args["count"] = False
records = ParsedInstance.query_mongo(**query_args)
self.assertEqual(len(records), 1)
# submit the edited instance
xml_submission_file_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"..",
"fixtures",
"tutorial",
"instances",
"tutorial_2012-06-27_11-27-53_w_uuid_edited.xml",
)
self._make_submission(xml_submission_file_path)
self.assertEqual(self.response.status_code, 201)
records = ParsedInstance.query_mongo(**query_args)
self.assertEqual(len(records), 1)
cached_geopoint = records[0][GEOLOCATION]
# the cached geopoint should equal the gps field
gps = records[0]["gps"].split(" ")
self.assertEqual(float(gps[0]), float(cached_geopoint[0]))
self.assertEqual(float(gps[1]), float(cached_geopoint[1]))
示例10: test_xform_delete_cascades_mongo_instances
def test_xform_delete_cascades_mongo_instances(self):
initial_mongo_count = ParsedInstance.query_mongo(
self.user.username, self.xform.id_string, "{}", "[]", "{}", count=True
)[0]["count"]
# submit instance
for i in range(len(self.surveys)):
self._submit_transport_instance(i)
# check mongo record exists
mongo_count = ParsedInstance.query_mongo(
self.user.username, self.xform.id_string, "{}", "[]", "{}", count=True
)[0]["count"]
self.assertEqual(mongo_count, initial_mongo_count + len(self.surveys))
# delete form
xform_delete_url = reverse(
delete_xform, kwargs={"username": self.user.username, "id_string": self.xform.id_string}
)
self.client.post(xform_delete_url)
mongo_count = ParsedInstance.query_mongo(
self.user.username, self.xform.id_string, "{}", "[]", "{}", count=True
)[0]["count"]
self.assertEqual(mongo_count, initial_mongo_count)
示例11: _query_data
def _query_data(self, query='{}', start=0,
limit=ParsedInstance.DEFAULT_LIMIT,
fields='[]', count=False):
# ParsedInstance.query_mongo takes params as json strings
# so we dumps the fields dictionary
count_args = {
'xform': self.xform,
'query': query,
'start': self.start,
'end': self.end,
'fields': '[]',
'sort': '{}',
'count': True
}
count_object = list(ParsedInstance.query_data(**count_args))
record_count = count_object[0]["count"]
if record_count < 1:
raise NoRecordsFoundError("No records found for your query")
# if count was requested, return the count
if count:
return record_count
else:
query_args = {
'xform': self.xform,
'query': query,
'fields': fields,
'start': self.start,
'end': self.end,
# TODO: we might want to add this in for the user
# to sepcify a sort order
'sort': 'id',
'start_index': start,
'limit': limit,
'count': False
}
# use ParsedInstance.query_mongo
cursor = ParsedInstance.query_data(**query_args)
return cursor
示例12: test_edited_submissions_in_exports
def test_edited_submissions_in_exports(self):
self._publish_transportation_form()
initial_count = ParsedInstance.query_mongo(
self.user.username, self.xform.id_string, '{}', '[]', '{}',
count=True)[0]['count']
instance_name = 'transport_2011-07-25_19-05-36'
path = _main_fixture_path(instance_name)
self._make_submission(path)
count = ParsedInstance.query_mongo(
self.user.username, self.xform.id_string, '{}', '[]', '{}',
count=True)[0]['count']
self.assertEqual(count, initial_count + 1)
# make edited submission - simulating what enketo would return
instance_name = 'transport_2011-07-25_19-05-36-edited'
path = _main_fixture_path(instance_name)
self._make_submission(path)
count = ParsedInstance.query_mongo(
self.user.username, self.xform.id_string, '{}', '[]', '{}',
count=True)[0]['count']
self.assertEqual(count, initial_count + 1)
# create the export
csv_export_url = reverse(
'csv_export', kwargs={"username": self.user.username,
"id_string": self.xform.id_string})
response = self.client.get(csv_export_url)
self.assertEqual(response.status_code, 200)
f = StringIO.StringIO(self._get_response_content(response))
csv_reader = csv.DictReader(f)
data = [row for row in csv_reader]
f.close()
num_rows = len(data)
# number of rows == initial_count + 1
self.assertEqual(num_rows, initial_count + 1)
key = 'transport/loop_over_transport_types_frequency/ambulance/'\
'frequency_to_referral_facility'
self.assertEqual(data[initial_count][key], "monthly")
示例13: to_native
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
示例14: to_representation
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]
示例15: api
def api(request, username=None, id_string=None):
"""
Returns all results as JSON. If a parameter string is passed,
it takes the 'query' parameter, converts this string to a dictionary, an
that is then used as a MongoDB query string.
NOTE: only a specific set of operators are allow, currently $or and $and.
Please send a request if you'd like another operator to be enabled.
NOTE: Your query must be valid JSON, double check it here,
http://json.parser.online.fr/
E.g. api?query='{"last_name": "Smith"}'
"""
if request.method == "OPTIONS":
response = HttpResponse()
add_cors_headers(response)
return response
helper_auth_helper(request)
helper_auth_helper(request)
xform, owner = check_and_set_user_and_form(username, id_string, request)
if not xform:
return HttpResponseForbidden(_(u"Not shared."))
try:
args = {
"username": username,
"id_string": id_string,
"query": request.GET.get("query"),
"fields": request.GET.get("fields"),
"sort": request.GET.get("sort"),
}
if "start" in request.GET:
args["start"] = int(request.GET.get("start"))
if "limit" in request.GET:
args["limit"] = int(request.GET.get("limit"))
if "count" in request.GET:
args["count"] = True if int(request.GET.get("count")) > 0 else False
cursor = ParsedInstance.query_mongo(**args)
except ValueError, e:
return HttpResponseBadRequest(e.__str__())