def set_object_list_and_total_count(self, query, fields, sort, start, limit, is_public_request): try: if not is_public_request: xform = self.get_object() where, where_params = get_where_clause(query) if where: self.object_list = self.object_list.extra(where=where, params=where_params) if (start and limit or limit) and (not sort and not fields): start = start if start is not None else 0 limit = limit if start is None or start == 0 else start + limit self.object_list = filter_queryset_xform_meta_perms( self.get_object(), self.request.user, self.object_list) self.object_list = \ self.object_list.order_by('pk')[start: limit] self.total_count = self.object_list.count() elif (sort or limit or start or fields) and not is_public_request: try: query = \ filter_queryset_xform_meta_perms_sql(self.get_object(), self.request.user, query) self.object_list = query_data(xform, query=query, sort=sort, start_index=start, limit=limit, fields=fields) self.total_count = query_data(xform, query=query, sort=sort, start_index=start, limit=limit, fields=fields, count=True)[0].get('count') except NoRecordsPermission: self.object_list = [] self.total_count = 0 else: self.total_count = self.object_list.count() if self.total_count and isinstance(self.object_list, QuerySet): self.etag_hash = get_etag_hash_from_query(self.object_list) elif self.total_count: sql, params, records = get_sql_with_params(xform, query=query, sort=sort, start_index=start, limit=limit, fields=fields) self.etag_hash = get_etag_hash_from_query(records, sql, params) except ValueError as e: raise ParseError(unicode(e)) except DataError as e: raise ParseError(unicode(e))
def set_object_list(self, query, fields, sort, start, limit, is_public_request): try: enable_etag = True if not is_public_request: xform = self.get_object() self.data_count = xform.num_of_submissions enable_etag = self.data_count <\ SUBMISSION_RETRIEVAL_THRESHOLD where, where_params = get_where_clause(query) if where: self.object_list = self.object_list.extra(where=where, params=where_params) if (start and limit or limit) and (not sort and not fields): start = start if start is not None else 0 limit = limit if start is None or start == 0 else start + limit self.object_list = filter_queryset_xform_meta_perms( self.get_object(), self.request.user, self.object_list) self.object_list = self.object_list[start:limit] elif (sort or limit or start or fields) and not is_public_request: try: query = \ filter_queryset_xform_meta_perms_sql(self.get_object(), self.request.user, query) self.object_list = query_data( xform, query=query, sort=sort, start_index=start, limit=limit, fields=fields, json_only=not self.kwargs.get('format') == 'xml') except NoRecordsPermission: self.object_list = [] # ETags are Disabled for XForms with Submissions that surpass # the configured SUBMISSION_RETRIEVAL_THRESHOLD setting if enable_etag: if isinstance(self.object_list, QuerySet): self.etag_hash = get_etag_hash_from_query(self.object_list) else: sql, params, records = get_sql_with_params( xform, query=query, sort=sort, start_index=start, limit=limit, fields=fields) self.etag_hash = get_etag_hash_from_query( records, sql, params) except ValueError as e: raise ParseError(text(e)) except DataError as e: raise ParseError(text(e))
def test_retrieve_records_based_on_form_verion(self): self._create_user_and_login() self._publish_transportation_form() initial_version = self.xform.version # 0 and 1 are indexes in a list representing transport instances with # the same form version - same as the transport form for a in [0, 1]: self._submit_transport_instance(survey_at=a) # the instances below have a different form vresion transport_instances_with_different_version = [ 'transport_2011-07-25_19-05-51', 'transport_2011-07-25_19-05-52' ] for a in transport_instances_with_different_version: self._make_submission(os.path.join( self.this_directory, 'fixtures', 'transportation', 'instances', a, a + '.xml')) instances = Instance.objects.filter( xform__id_string=self.xform.id_string ) instances_count = instances.count() self.assertEqual(instances_count, 4) # retrieve based on updated form version sql, params, records = get_sql_with_params( xform=self.xform, query='{"_version": "20170517"}' ) self.assertEqual(2, records.count()) # retrived record based on initial form version sql, params, records = get_sql_with_params( xform=self.xform, query='{"_version": "%s"}' % initial_version ) self.assertEqual(2, records.count())
def set_object_list( self, query, fields, sort, start, limit, is_public_request): try: if not is_public_request: xform = self.get_object() where, where_params = get_where_clause(query) if where: self.object_list = self.object_list.extra(where=where, params=where_params) if (start and limit or limit) and (not sort and not fields): start = start if start is not None else 0 limit = limit if start is None or start == 0 else start + limit self.object_list = filter_queryset_xform_meta_perms( self.get_object(), self.request.user, self.object_list) self.object_list = self.object_list[start:limit] elif (sort or limit or start or fields) and not is_public_request: try: query = \ filter_queryset_xform_meta_perms_sql(self.get_object(), self.request.user, query) self.object_list = query_data(xform, query=query, sort=sort, start_index=start, limit=limit, fields=fields) except NoRecordsPermission: self.object_list = [] if isinstance(self.object_list, QuerySet): self.etag_hash = get_etag_hash_from_query(self.object_list) else: sql, params, records = get_sql_with_params( xform, query=query, sort=sort, start_index=start, limit=limit, fields=fields ) self.etag_hash = get_etag_hash_from_query(records, sql, params) except ValueError as e: raise ParseError(text(e)) except DataError as e: raise ParseError(text(e))
def test_retrieve_records_using_list_of_queries(self): self._create_user_and_login() self._publish_transportation_form() # 0 and 1 are indexes in a list representing transport instances with # the same form version - same as the transport form for a in [0, 1]: self._submit_transport_instance(survey_at=a) transport_instances_with_different_version = [ 'transport_2011-07-25_19-05-51', 'transport_2011-07-25_19-05-52' ] # make submissions for a in transport_instances_with_different_version: self._make_submission(os.path.join( self.this_directory, 'fixtures', 'transportation', 'instances', a, a + '.xml')) instances = Instance.objects.filter( xform__id_string=self.xform.id_string ) instances_count = instances.count() self.assertEqual(instances_count, 4) # bob accesses all records sql, params, records = get_sql_with_params( xform=self.xform, query={'_submitted_by': 'bob'} ) self.assertEqual(4, records.count()) # only three records with name ambulance sql, params, records = get_sql_with_params( xform=self.xform, query=[{'_submitted_by': 'bob'}, 'ambulance'] ) self.assertEqual(3, records.count()) # create user alice user_alice = self._create_user('alice', 'alice') # create user profile and set require_auth to false for tests profile, created = UserProfile.objects.get_or_create(user=user_alice) profile.require_auth = False profile.save() # change ownership of first two records for i in instances[:2]: i.user = user_alice i.save() self.xform.save() # bob accesses only two record sql, params, records = get_sql_with_params( xform=self.xform, query={'_submitted_by': 'bob'} ) self.assertEqual(2, records.count()) # both remaining records have ambulance sql, params, records = get_sql_with_params( xform=self.xform, query=[{'_submitted_by': 'bob'}, 'ambulance'] ) self.assertEqual(2, records.count())