def get(self, list_id, session=None): """ Get entries by list ID """ try: el.get_list_by_id(list_id=list_id, session=session) except NoResultFound: raise NotFoundError('list_id %d does not exist' % list_id) args = entries_parser.parse_args() # Pagination and sorting params page = args['page'] per_page = args['per_page'] sort_by = args['sort_by'] sort_order = args['order'] # Handle max size limit if per_page > 100: per_page = 100 start = per_page * (page - 1) stop = start + per_page descending = bool(sort_order == 'desc') kwargs = { 'start': start, 'stop': stop, 'list_id': list_id, 'order_by': sort_by, 'descending': descending, 'session': session } total_items = el.get_entries_by_list_id(count=True, **kwargs) if not total_items: return jsonify([]) log.debug('entry lists entries count is %d', total_items) entries = [entry.to_dict() for entry in el.get_entries_by_list_id(**kwargs)] # Total number of pages total_pages = int(ceil(total_items / float(per_page))) if page > total_pages: raise NotFoundError('page %s does not exist' % page) # Actual results in page actual_size = min(len(entries), per_page) # Get pagination headers pagination = pagination_headers(total_pages, total_items, actual_size, request) # Create response rsp = jsonify(entries) # Add link header to response rsp.headers.extend(pagination) return rsp
def get(self, list_id, session=None): """ Get entries by list ID """ args = entry_list_parser.parse_args() page = args.get('page') page_size = args.get('page_size') start = page_size * (page - 1) stop = start + page_size if args.get('order') == 'desc': descending = True else: descending = False kwargs = { 'start': start, 'stop': stop, 'list_id': list_id, 'order_by': args.get('sort_by'), 'descending': descending, 'session': session } try: list = el.get_list_by_id(list_id=list_id, session=session) except NoResultFound: return { 'status': 'error', 'message': 'list_id %d does not exist' % list_id }, 404 count = el.get_entries_by_list_id(count=True, **kwargs) log.debug('entry lists entries count is %d', count) entries = [ entry.to_dict() for entry in el.get_entries_by_list_id(**kwargs) ] pages = int(ceil(count / float(page_size))) number_of_entries = min(page_size, count) return jsonify({ 'entries': entries, 'total_number_of_entries': count, 'number_of_entries': number_of_entries, 'page': page, 'total_number_of_pages': pages })
def get(self, list_id, session=None): """ Get entries by list ID """ args = entry_list_parser.parse_args() page = args.get('page') page_size = args.get('page_size') start = page_size * (page - 1) stop = start + page_size if args.get('order') == 'desc': descending = True else: descending = False kwargs = { 'start': start, 'stop': stop, 'list_id': list_id, 'order_by': args.get('sort_by'), 'descending': descending, 'session': session } try: list = el.get_list_by_id(list_id=list_id, session=session) except NoResultFound: return {'status': 'error', 'message': 'list_id %d does not exist' % list_id}, 404 count = el.get_entries_by_list_id(count=True, **kwargs) log.debug('entry lists entries count is %d', count) entries = [entry.to_dict() for entry in el.get_entries_by_list_id(**kwargs)] pages = int(ceil(count / float(page_size))) number_of_entries = min(page_size, count) return jsonify({ 'entries': entries, 'total_number_of_entries': count, 'number_of_entries': number_of_entries, 'page': page, 'total_number_of_pages': pages })
def entry_list_list(options): """List entry list""" with Session() as session: try: entry_list = get_list_by_exact_name(options.list_name, session=session) except NoResultFound: console('Could not find entry list with name {}'.format(options.list_name)) return console('Entries for list `{}`:'.format(options.list_name)) console('-' * 79) for entry in get_entries_by_list_id(entry_list.id, order_by='added', descending=True, session=session): console('{:2d}: {}, {} fields'.format(entry.id, entry.title, len(entry.entry)))
def entry_list_list(options): """List entry list""" with Session() as session: try: entry_list = get_list_by_exact_name(options.list_name, session=session) except NoResultFound: console('Could not find entry list with name {}'.format(options.list_name)) return header = ['#', 'Title', '# of fields'] table_data = [header] for entry in get_entries_by_list_id(entry_list.id, order_by='added', descending=True, session=session): table_data.append([entry.id, entry.title, len(entry.entry)]) table = TerminalTable(options.table_type, table_data) try: console(table.output) except TerminalTableError as e: console('ERROR: %s' % str(e))
def entry_list_list(options): """List entry list""" with Session() as session: try: entry_list = plugin_entry_list.get_list_by_exact_name(options.list_name, session=session) except NoResultFound: console('Could not find entry list with name {}'.format(options.list_name)) return header = ['#', 'Title', '# of fields'] table_data = [header] for entry in plugin_entry_list.get_entries_by_list_id(entry_list.id, order_by='added', descending=True, session=session): table_data.append([entry.id, entry.title, len(entry.entry)]) try: table = TerminalTable(options.table_type, table_data) console(table.output) except TerminalTableError as e: console('ERROR: %s' % str(e))
def entry_list_list(options): """List entry list""" with Session() as session: try: entry_list = get_list_by_exact_name(options.list_name, session=session) except NoResultFound: console('Could not find entry list with name {}'.format( options.list_name)) return console('Entries for list `{}`:'.format(options.list_name)) console('-' * 79) for entry in get_entries_by_list_id(entry_list.id, order_by='added', descending=True, session=session): console('{:2d}: {}, {} fields'.format(entry.id, entry.title, len(entry.entry)))
def get(self, list_id, session=None): """ Get entries by list ID """ try: list = el.get_list_by_id(list_id=list_id, session=session) except NoResultFound: raise NotFoundError('list_id %d does not exist' % list_id) args = entries_parser.parse_args() # Pagination and sorting params page = args['page'] per_page = args['per_page'] sort_by = args['sort_by'] sort_order = args['order'] # Handle max size limit if per_page > 100: per_page = 100 start = per_page * (page - 1) stop = start + per_page descending = sort_order == 'desc' kwargs = { 'start': start, 'stop': stop, 'list_id': list_id, 'order_by': sort_by, 'descending': descending, 'session': session } total_items = list.entries.count() if not total_items: return jsonify([]) log.debug('entry lists entries count is %d', total_items) entries = [ entry.to_dict() for entry in el.get_entries_by_list_id(**kwargs) ] # Total number of pages total_pages = int(ceil(total_items / float(per_page))) if page > total_pages: raise NotFoundError('page %s does not exist' % page) # Actual results in page actual_size = min(len(entries), per_page) # Get pagination headers pagination = pagination_headers(total_pages, total_items, actual_size, request) # Create response rsp = jsonify(entries) # Add link header to response rsp.headers.extend(pagination) return rsp