def get_page_by_filter(self, correlation_id: Optional[str], filter: Any, paging: PagingParams, sort: Any, select: Any) -> DataPage: """ Gets a page of data items retrieved by a given filter and sorted according to sort parameters. This method shall be called by a public getPageByFilter method from child class that receives FilterParams and converts them into a filter function. :param correlation_id: (optional) transaction id to trace execution through call chain. :param filter: (optional) a filter JSON object :param paging: (optional) paging parameters :param sort: (optional) sorting JSON object :param select: (optional) projection JSON object :return: a data page or raise error """ select = select if select and len(select) > 0 else '*' query = "SELECT " + select + " FROM " + self._quoted_table_name() # Adjust max item count based on configuration paging = paging or PagingParams() skip = paging.get_skip(-1) take = paging.get_take(self._max_page_size) paging_enabled = paging.total if filter and filter != '': query += " WHERE " + filter if sort: query += " ORDER BY " + sort query += " LIMIT " + str(take) if skip >= 0: query += " OFFSET " + str(skip) result = self._request(query) items = result['items'] if items is not None: self._logger.trace(correlation_id, "Retrieved %d from %s", len(items), self._table_name) items = list(map(self._convert_to_public, items)) if paging_enabled: query = 'SELECT COUNT(*) AS count FROM ' + self._quoted_table_name() if filter is not None and filter != '': query += " WHERE " + filter result = self._request(query) count = LongConverter.to_long(len(result['items'][0])) if result and len(result['items']) == 1 else 0 return DataPage(items, count) else: return DataPage(items)
def get_page_by_filter(self, correlation_id: Optional[str], filter: FilterParams, paging: PagingParams) -> DataPage: filters = filter if filter is not None else FilterParams() key = filters.get_as_nullable_string("key") paging = paging if not (paging is None) else PagingParams() skip = paging.get_skip(0) take = paging.get_take(100) result = [] self.__lock.acquire() try: for item in self.__items: if not (key is None) and key != item.key: continue skip -= 1 if skip >= 0: continue take -= 1 if take < 0: break result.append(item) finally: self.__lock.release() return DataPage(result)
def get_beacons_by_filter(self, correlation_id, filter, paging, sort=None, select=None): items = list(self._items) # Filter and sort if not (filter is None): items = list(filtered(filter, items)) if not (sort is None): items = list(items.sort(key=sort)) # items = sorted(items, sort) # Prepare paging parameters paging = paging if not (paging is None) else PagingParams() skip = paging.get_skip(-1) take = paging.get_take(self._max_page_size) # Get a page data = items if skip > 0: data = data[skip:] if take > 0: data = data[:take + 1] # Convert values if not (select is None): data = map(select, data) # Return a page return DataPage(data, len(items))
def get_authorized_sites(self): filter_params = self._get_filter_params() paging = self._get_paging_params() roles: List[str] = [] if not hasattr(bottle.request, 'user') else bottle.request.user.roles or [] site_ids: List[str] = [] # Get authorized site ids for role in roles: tokens = role.split(':') if len(tokens) == 2: site_ids.append(tokens[0]) # Consider ids parameter old_site_ids = filter_params.get('ids') if old_site_ids: site_ids = list(set(old_site_ids).intersection(set(site_ids))) # Is user has no sites then exit if len(site_ids) == 0: return self._send_result(DataPage([])) filter_params.set_as_object('ids', site_ids) result = self.__sites_client.get_sites(None, filter_params, paging) return self._send_result(result)
def get_page_by_filter(self, correlation_id, filter, paging): filter = filter if filter is not None else FilterParams() key = filter.get_as_nullable_string("key") paging = paging if paging is not None else PagingParams() skip = paging.get_skip(0) take = paging.get_take(100) result = [] self._lock.acquire() try: for item in self._items: if key is not None and key != item['key']: continue skip -= 1 if skip >= 0: continue take -= 1 if take < 0: break result.append(item) finally: self._lock.release() return DataPage(result, len(result))
def get_beacons_by_filter(self, correlation_id, filter, paging): response = self.call_command('get_beacons', correlation_id, { 'filter': filter, 'paging': paging }) page = DataPage(data=[BeaconV1(**item) for item in response['data']], total=response['total']) return page
def get_roles_by_filter(self, correlation_id: Optional[str], filter_params: FilterParams, paging: PagingParams) -> DataPage: roles = list(filter(self.__compose_filter(filter_params), self.__roles)) return DataPage(roles, len(roles))
def get_page_by_filter(self, correlation_id: Optional[str], filter: Any, paging: PagingParams, sort: Any = None, select: Any = None) -> DataPage: """ Gets a page of data items retrieved by a given filter and sorted according to sort parameters. This method shall be called by a public :func:`get_page_by_filter` method from child class that receives :class:`FilterParams <pip_services3_commons.data.FilterParams.FilterParams>` and converts them into a filter function. :param correlation_id: (optional) transaction id to trace execution through call chain. :param filter: (optional) a filter function to filter items :param paging: (optional) paging parameters :param sort: (optional) sorting parameters :param select: (optional) projection parameters (not used yet) :return: a data page of result by filter. """ with self._lock: items = deepcopy(self._items) # Filter and sort if filter is not None: items = list(filtered(filter, items)) if sort is not None: items = list(filtered(sort, items)) # items = sorted(items, sort) # Prepare paging parameters paging = paging if paging is not None else PagingParams() skip = paging.get_skip(-1) take = paging.get_take(self._max_page_size) # Get a page data = items if skip > 0: data = data[skip:] if take > 0: data = data[:take] # Convert values if not (select is None): data = map(select, data) self._logger.trace(correlation_id, "Retrieved " + str(len(data)) + " items") # Return a page return DataPage(data, len(items))
def get_page_by_filter(self, correlation_id: Optional[str], filter: FilterParams, paging: PagingParams) -> DataPage: result = self.call_command('get_dummies', correlation_id, { 'filter': filter, 'paging': paging }) page = DataPage( data=[Dummy.from_json(item) for item in result['data']], total=result['total']) return page
def test_crud_operations(self): # Create one dummy response = self.invoke( "/dummy/create_dummy", Parameters.from_tuples("dummy", DUMMY1.to_json())) dummy1 = Dummy.from_json(response) assert dummy1 is not None assert DUMMY1.key == dummy1.key assert DUMMY1.content == dummy1.content # Create another dummy response = self.invoke( "/dummy/create_dummy", Parameters.from_tuples("dummy", DUMMY2.to_json())) dummy2 = Dummy.from_json(response) assert dummy2 is not None assert DUMMY2.key == dummy2.key assert DUMMY2.content == dummy2.content # Get all dummies response = self.invoke("/dummy/get_dummies", Parameters.from_tuples("dummies")) page = DataPage( [Dummy.from_json(item) for item in response.get('data', [])], response.get('total')) assert page is not None assert 2 == len(page.data) # Update the dummy dummy1.content = "Updated Content 1" response = self.invoke( "/dummy/update_dummy", Parameters.from_tuples("dummy", dummy1.to_json())) dummy = Dummy.from_json(response) assert dummy is not None assert dummy1.id == dummy.id assert dummy1.key == dummy.key assert "Updated Content 1" == dummy.content # Delete the dummy self.invoke("/dummy/delete_dummy", Parameters.from_tuples("dummy_id", dummy1.id)) # Try to get deleted dummy get_dummy = self.invoke("/dummy/get_dummy_by_id", Parameters.from_tuples("dummy_id", dummy1.id)) assert get_dummy is None
def get_page_by_filter(self, correlation_id, filter, paging, sort=None, select=None): """ Gets a page of data items retrieved by a given filter and sorted according to sort parameters. This method shall be called by a public getPageByFilter method from child class that receives FilterParams and converts them into a filter function. :param correlation_id: (optional) transaction id to trace execution through call chain. :param filter: (optional) a filter JSON object :param paging: (optional) paging parameters :param sort: (optional) sorting JSON object :param select: (optional) projection JSON object :return: a data page of result by filter """ # Adjust max item count based on configuration paging = paging if paging != None else PagingParams() skip = paging.get_skip(-1) take = paging.get_take(self._max_page_size) # Configure statement statement = self._collection.find(filter) if skip >= 0: statement = statement.skip(skip) statement = statement.limit(take) if sort != None: statement = statement.sort(sort) if select != None: statement = statement.select(select) # Retrive page items items = [] for item in statement: item = self._convert_to_public(item) items.append(item) # Calculate total if needed total = None if paging.total: total = self._collection.find(filter).count() return DataPage(items, total)
def get_page_by_filter(self, correlation_id, filter, paging, sort = None, select = None): """ Gets a page of data items retrieved by a given filter and sorted according to sort parameters. This method shall be called by a public getPageByFilter method from child class that receives FilterParams and converts them into a filter function. :param correlation_id: (optional) transaction id to trace execution through call chain. :param filter: (optional) a filter function to filter items :param paging: (optional) paging parameters :param sort: (optional) sorting parameters :param select: (optional) projection parameters (not used yet) :return: a data page of result by filter. """ self._lock.acquire() try: items = list(self._items) finally: self._lock.release() # Filter and sort if filter != None: items = list(filtered(filter, items)) if sort != None: items = list(items.sort(key=sort)) # items = sorted(items, sort) # Prepare paging parameters paging = paging if paging != None else PagingParams() skip = paging.get_skip(-1) take = paging.get_take(self._max_page_size) # Get a page data = items if skip > 0: data = data[skip:] if take > 0: data = data[:take+1] # Convert values if select != None: data = map(select, data) self._logger.trace(correlation_id, "Retrieved " + str(len(data)) + " items") # Return a page return DataPage(data, len(items))
def test_crud_operations(self): # Create one dummy response = self.invoke('POST', "/dummies", {"body": DUMMY1.to_json()}) dummy1 = Dummy.from_json(response) assert dummy1 is not None assert DUMMY1.key == dummy1.key assert DUMMY1.content == dummy1.content # Create another dummy response = self.invoke('POST', "/dummies", {"body": DUMMY2.to_json()}) dummy2 = Dummy.from_json(response) assert dummy2 is not None assert DUMMY2.key == dummy2.key assert DUMMY2.content == dummy2.content # Get all dummies response = self.invoke('GET', '/dummies', None) page = DataPage( [Dummy.from_json(item) for item in response.get('data', [])], response.get('total')) assert page is not None assert len(page.data) == 2 # Update the dummy dummy1.content = 'Updated Content 1' response = self.invoke('PUT', '/dummies', {'body': dummy1.to_json()}) dummy = Dummy(**response) assert dummy.content == 'Updated Content 1' assert dummy.key == dummy1.key dummy1 = dummy # Delete dummy self.invoke('DELETE', f'/dummies/{dummy1.id}') # Try to get delete dummy response = self.invoke('GET', f'/dummies/{dummy1.id}') assert response is None assert 4 == self.service.get_number_of_calls()
def get_beacons(self, correlation_id, filter, paging): filter_beacons = self.compose_filter(filter) beacons = [item for item in self._items if filter_beacons(item) is True] # Extract a page paging = paging if paging is not None else PagingParams() skip = paging.get_skip(-1) take = paging.get_take(self._max_page_size) total = None if paging.total: total = len(beacons) if skip > 0: beacons = beacons[skip:] beacons = beacons[:take] page = DataPage(beacons, total) return page
def get_page_by_filter(self, correlation_id, filters: FilterParams, paging: PagingParams) -> DataPage: params = {} self._add_filter_params(params, filters) self._add_paging_params(params, paging) timing = self._instrument(correlation_id, 'dummy.get_page_by_filter') try: result = self._call('GET', '/dummies', correlation_id, params) page = DataPage( data=[Dummy.from_json(item) for item in result['data']], total=result['total']) return page except Exception as err: timing.end_timing(err) finally: timing.end_success()
def get_accounts(self, correlation_id: Optional[str], filter_params: FilterParams, paging: PagingParams) -> DataPage: filter_curl = self.__compose_filter(filter_params) accounts = list(filter(filter_curl, self.__accounts)) # Extract a page paging = PagingParams() if paging is None else paging skip = paging.get_skip(-1) take = paging.get_take(self.__max_page_size) total = None if paging.total: total = len(accounts) if skip > 0: accounts = accounts[skip:] accounts = accounts[:take] page = DataPage(accounts, total) return page
def get_beacons_by_filter(self, correlation_id, filter, paging): result = self.call_command('get_beacons', correlation_id, { 'filter': filter, 'paging': paging }) return DataPage(result['data'], result['total'])
def get_page_by_filter(self, correlation_id, filters, paging): params = RestQueryParams(correlation_id, filters, paging) result = self.call('GET', '/dummies', correlation_id, params) return DataPage(result['data'], result['total'])
def get_beacons(self, correlation_id: Optional[str], filter_params: FilterParams, paging: PagingParams) -> DataPage: beacons = list(filter(self.__compose_filter(filter_params), self.__beacons)) return DataPage(beacons, len(beacons))
def get_sections(self, correlation_id: Optional[str], filter_params: FilterParams, paging: PagingParams) -> DataPage: return DataPage([])