def collection_get(self): """Tenders List Get Tenders List ---------------- Example request to get tenders list: .. sourcecode:: http GET /tenders HTTP/1.1 Host: example.com Accept: application/json This is what one should expect in response: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "data": [ { "id": "64e93250be76435397e8c992ed4214d1", "dateModified": "2014-10-27T08:06:58.158Z" } ] } """ # http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options params = {} fields = self.request.params.get('opt_fields', '') if fields: params['opt_fields'] = fields limit = self.request.params.get('limit', '') if limit: params['limit'] = limit limit = int(limit) if limit.isdigit() else 100 descending = self.request.params.get('descending') offset = self.request.params.get('offset', '9' if descending else '0') if descending: params['descending'] = descending next_offset = datetime.min.isoformat() if descending else get_now( ).isoformat() results = Tender.view(self.db, 'tenders/by_dateModified', limit=limit + 1, startkey=offset, descending=bool(descending)) results = [tender_serialize(i, fields) for i in results] if len(results) > limit: results, last = results[:-1], results[-1] params['offset'] = last['dateModified'] else: params['offset'] = next_offset next_url = self.request.route_url('collection_Tender', _query=params) next_path = self.request.route_path('collection_Tender', _query=params) return { 'data': results, 'next_page': { "offset": params['offset'], "path": next_path, "uri": next_url } }
def collection_get(self): """Tenders List Get Tenders List ---------------- Example request to get tenders list: .. sourcecode:: http GET /tenders HTTP/1.1 Host: example.com Accept: application/json This is what one should expect in response: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "data": [ { "id": "64e93250be76435397e8c992ed4214d1", "dateModified": "2014-10-27T08:06:58.158Z" } ] } """ # http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options params = {} fields = self.request.params.get('opt_fields', '') if fields: params['opt_fields'] = fields limit = self.request.params.get('limit', '') if limit: params['limit'] = limit limit = int(limit) if limit.isdigit() else 100 descending = self.request.params.get('descending') offset = self.request.params.get('offset', '9' if descending else '0') if descending: params['descending'] = descending next_offset = datetime.min.isoformat() if descending else get_now().isoformat() if fields: LOGGER.info('Used custom fields for tenders list: {}'.format(','.join(sorted(fields.split(','))))) fields = fields.split(',') + ['dateModified', 'id'] results = [ tender_serialize(i, fields) for i in Tender.view(self.db, 'tenders/by_dateModified', limit=limit + 1, startkey=offset, descending=bool(descending), include_docs=True) ] else: results = [ {'id': i.id, 'dateModified': i.key} for i in tenders_by_dateModified_view(self.db, limit=limit + 1, startkey=offset, descending=bool(descending)) ] if len(results) > limit: results, last = results[:-1], results[-1] params['offset'] = last['dateModified'] else: params['offset'] = next_offset return { 'data': results, 'next_page': { "offset": params['offset'], "path": self.request.route_path('collection_Tender', _query=params), "uri": self.request.route_url('collection_Tender', _query=params) } }
def collection_get(self): """Tenders List Get Tenders List ---------------- Example request to get tenders list: .. sourcecode:: http GET /tenders HTTP/1.1 Host: example.com Accept: application/json This is what one should expect in response: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "data": [ { "id": "64e93250be76435397e8c992ed4214d1", "dateModified": "2014-10-27T08:06:58.158Z" } ] } """ # http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options params = {} fields = self.request.params.get('opt_fields', '') if fields: params['opt_fields'] = fields limit = self.request.params.get('limit', '') if limit: params['limit'] = limit limit = int(limit) if limit.isdigit() else 100 descending = self.request.params.get('descending') offset = self.request.params.get('offset', '9' if descending else '0') if descending: params['descending'] = descending mode = self.request.params.get('mode') if mode: params['mode'] = mode list_view = VIEW_MAP.get(mode, tenders_real_by_dateModified_view) next_offset = datetime.min.isoformat() if descending else get_now( ).isoformat() if fields: LOGGER.info('Used custom fields for tenders list: {}'.format( ','.join(sorted(fields.split(',')))), extra={'MESSAGE_ID': 'tender_list_custom'}) fields = fields.split(',') + ['dateModified', 'id'] list_view_name = '/'.join([list_view.design, list_view.name]) results = [ tender_serialize(i, fields) for i in Tender.view(self.db, list_view_name, limit=limit + 1, startkey=offset, descending=bool(descending), include_docs=True) ] else: results = [{ 'id': i.id, 'dateModified': i.key } for i in list_view(self.db, limit=limit + 1, startkey=offset, descending=bool(descending))] if len(results) > limit: results, last = results[:-1], results[-1] params['offset'] = last['dateModified'] else: params['offset'] = next_offset return { 'data': results, 'next_page': { "offset": params['offset'], "path": self.request.route_path('collection_Tender', _query=params), "uri": self.request.route_url('collection_Tender', _query=params) } }