def pending_list_add(options): with Session() as session: try: pending_list = get_list_by_exact_name(options.list_name, session=session) except NoResultFound: console('Could not find a pending list with name `{}`, creating'.format(options.list_name)) pending_list = PendingListList(name=options.list_name) session.add(pending_list) session.merge(pending_list) session.commit() title = options.entry_title entry = {'title': options.entry_title, 'url': options.url} db_entry = get_entry_by_title(list_id=pending_list.id, title=title, session=session) if db_entry: console("Entry with the title `{}` already exist with list `{}`. Will replace identifiers if given".format( title, pending_list.name)) operation = 'updated' else: console("Adding entry with title `{}` to list `{}`".format(title, pending_list.name)) db_entry = PendingListEntry(entry=entry, pending_list_id=pending_list.id) if options.approved: console('marking entry as approved') db_entry.approved = True session.add(db_entry) operation = 'added' if options.attributes: console('Adding attributes to entry `{}`'.format(title)) for identifier in options.attributes: for k, v in identifier.items(): entry[k] = v db_entry.entry = entry console('Successfully {} entry `{}` to pending list `{}` '.format(operation, title, pending_list.name))
def post(self, session=None): """ Create a new pending list """ data = request.json name = data.get('name') try: get_list_by_exact_name(name=name, session=session) except NoResultFound: pass else: raise Conflict('list with name \'%s\' already exists' % name) pending_list = PendingListList() pending_list.name = name session.add(pending_list) session.commit() resp = jsonify(pending_list.to_dict()) resp.status_code = 201 return resp
def test_pending_list_pagination(self, api_client, link_headers): base_entry = dict(title='test_title_', original_url='url_') number_of_entries = 200 with Session() as session: pending_list = PendingListList(name='test list') session.add(pending_list) for i in range(number_of_entries): entry = copy.deepcopy(base_entry) for k, v in entry.items(): entry[k] = v + str(i) e = Entry(entry) pending_list.entries.append(PendingListEntry(e, pending_list.id)) # Default values rsp = api_client.get('/pending_list/1/entries/') assert rsp.status_code == 200 data = json.loads(rsp.get_data(as_text=True)) assert len(data) == 50 assert int(rsp.headers['total-count']) == 200 assert int(rsp.headers['count']) == 50 links = link_headers(rsp) assert links['last']['page'] == 4 assert links['next']['page'] == 2 # Change page size rsp = api_client.get('/pending_list/1/entries/?per_page=100') assert rsp.status_code == 200 data = json.loads(rsp.get_data(as_text=True)) assert len(data) == 100 assert int(rsp.headers['total-count']) == 200 assert int(rsp.headers['count']) == 100 links = link_headers(rsp) assert links['last']['page'] == 2 assert links['next']['page'] == 2 # Get different page rsp = api_client.get('/pending_list/1/entries/?page=2') assert rsp.status_code == 200 data = json.loads(rsp.get_data(as_text=True)) assert len(data) == 50 assert int(rsp.headers['total-count']) == 200 assert int(rsp.headers['count']) == 50 links = link_headers(rsp) assert links['last']['page'] == 4 assert links['next']['page'] == 3 assert links['prev']['page'] == 1
def test_pending_list_sorting(self, api_client): base_entry_1 = dict(title='test_title_1', original_url='url_c') base_entry_2 = dict(title='test_title_2', original_url='url_b') base_entry_3 = dict(title='test_title_3', original_url='url_a') with Session() as session: pending_list = PendingListList(name='test list') session.add(pending_list) e1 = Entry(base_entry_1) e2 = Entry(base_entry_2) e3 = Entry(base_entry_3) pending_list.entries.append(PendingListEntry(e1, pending_list.id)) pending_list.entries.append(PendingListEntry(e2, pending_list.id)) pending_list.entries.append(PendingListEntry(e3, pending_list.id)) # Sort by title rsp = api_client.get('/pending_list/1/entries/?sort_by=title') assert rsp.status_code == 200 data = json.loads(rsp.get_data(as_text=True)) assert data[0]['title'] == 'test_title_3' rsp = api_client.get('/pending_list/1/entries/?sort_by=title&order=asc') assert rsp.status_code == 200 data = json.loads(rsp.get_data(as_text=True)) assert data[0]['title'] == 'test_title_1' # Sort by original url rsp = api_client.get('/pending_list/1/entries/?sort_by=original_url') assert rsp.status_code == 200 data = json.loads(rsp.get_data(as_text=True)) assert data[0]['original_url'] == 'url_c' rsp = api_client.get('/pending_list/1/entries/?sort_by=original_url&order=asc') assert rsp.status_code == 200 data = json.loads(rsp.get_data(as_text=True)) assert data[0]['original_url'] == 'url_a' # Combine sorting and pagination rsp = api_client.get('/pending_list/1/entries/?sort_by=title&per_page=2&page=2') assert rsp.status_code == 200 data = json.loads(rsp.get_data(as_text=True)) assert data[0]['title'] == 'test_title_1'