diff --git a/pbclient/__init__.py b/pbclient/__init__.py index 2d9b996..5a0fc0d 100644 --- a/pbclient/__init__.py +++ b/pbclient/__init__.py @@ -431,9 +431,9 @@ def get_tasks(project_id, limit=100, offset=0, last_id=None): :param limit: Number of returned items, default 100 :type limit: integer :param offset: Offset for the query, default 0 + :type offset: integer :param last_id: id of the last task, used for pagination. If provided, offset is ignored :type last_id: integer - :type offset: integer :returns: True -- the response status code """ @@ -454,6 +454,52 @@ def get_tasks(project_id, limit=100, offset=0, last_id=None): raise +def get_task(project_id, task_id): + """ + + :param project_id: + :param task_id: + :return: + """ + params = { + 'project_id': project_id, + 'id': task_id + } + try: + res = _pybossa_req('get', 'task', params=params) + return res + except: # pragma: no cover + raise + + +def get_new_task(project_id, limit=1, offset=0, last_id=None): + """Return a new task or list of new tasks for a given project ID. + :param project_id: PYBOSSA Project ID + :type project_id: integer + :param limit: Number of returned items, default 1 + :type limit: integer + :param offset: Offset for the query, default 0 + :type offset: integer + :param last_id: id of the last task, used for pagination. If provided, offset is ignored + :type last_id: integer + :returns: True -- the response status code + """ + if last_id is not None: + params = dict(limit=limit, last_id=last_id) + else: + params = dict(limit=limit, offset=offset) + print(OFFSET_WARNING) + try: + res = _pybossa_req('get', 'project/{0}/newtask'.format(project_id), + params=params) + if type(res).__name__ == 'list': + return [Task(task) for task in res] + else: + return res + except: # pragma: no cover + raise + + def find_tasks(project_id, **kwargs): """Return a list of matched tasks for a given project ID. @@ -601,6 +647,57 @@ def find_taskruns(project_id, **kwargs): raise +def create_taskrun(project_id, task_id, info, user_ip=None, user_id=None): + """Create a task for a given project ID. + + :param project_id: PYBOSSA Project ID + :type project_id: integer + :param task_id: PYBOSSA Task ID + :type task_id: integer + :param info: PYBOSSA Project info JSON field + :type info: dict + :param user_ip: + :type user_ip: + :param user_id: + :type user_id: + :returns: True -- the response status code + """ + try: + taskrun = dict( + project_id=project_id, + task_id=task_id, + info=info, + calibration=0, + user_ip=user_ip, + user_id=user_id + ) + res = _pybossa_req('post', 'taskrun', payload=taskrun) + if res.get('id'): + return TaskRun(res) + else: + return res + except: # pragma: no cover + raise + + +def update_taskrun(taskrun): + """Update a task for a given task ID. + + :param task: PYBOSSA task + + """ + try: + taskrun_id = taskrun.id + taskrun = _forbidden_attributes(taskrun) + res = _pybossa_req('put', 'taskrun', taskrun_id, payload=taskrun.data) + if res.get('id'): + return TaskRun(res) + else: + return res + except: # pragma: no cover + raise + + def delete_taskrun(taskrun_id): """Delete the given taskrun. diff --git a/setup.py b/setup.py index 54a49d7..3285eae 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='pybossa-client', - version='2.0.1', + version='2.0.2', packages=find_packages(), install_requires=['requests>=0.13.0'], # metadata for upload to PyPI diff --git a/test/test_task.py b/test/test_task.py index ead361d..a31344d 100644 --- a/test/test_task.py +++ b/test/test_task.py @@ -67,6 +67,45 @@ def test_get_tasks_errors(self, Mock): err = self.client.get_tasks(1) self.check_error_output(err_output, err) + @patch('pbclient.requests.get') + def test_get_new_task(self, Mock): + """Test get_new_tasks works""" + Mock.return_value = self.create_fake_request([self.task.copy()], 200) + res = self.client.get_new_task(1) + assert len(res) == 1, len(res) + task = res[0] + assert task.id == self.task['id'], task + assert task.project_id == self.task['project_id'], task + + @patch('pbclient.requests.get') + def test_get_tasks_with_keyset_pagination(self, Mock): + """Test get_new_task uses keyset pagination if a last_id argument is + provided""" + Mock.return_value = self.create_fake_request([], 200) + self.client.get_new_task(1, last_id=1, limit=3) + + Mock.assert_called_once_with('http://localhost:5000/api/project/1/newtask', + params={'api_key': 'tester', + 'limit': 3, + 'last_id': 1}) + + @patch('pbclient.requests.get') + def test_get_tasks_errors(self, Mock): + """Test get new task errors works""" + targets = ['newtask'] + errors = {'Unauthorized': 401, 'NotFound': 404, 'Forbidden': 401, + 'TypeError': 415} + for target in targets: + for error in errors.keys(): + err_output = self.create_error_output(action='GET', + status_code=errors[error], + target=target, + exception_cls=error) + Mock.return_value = self.create_fake_request(err_output, + errors[error]) + err = self.client.get_new_task(1) + self.check_error_output(err_output, err) + @patch('pbclient.requests.get') def test_find_tasks(self, Mock): """Test find_tasks works"""