def test_create_result_event(self, mock_create_result): """Test create_result is called.""" from pybossa.core import db task = TaskFactory.create(n_answers=1) TaskRunFactory.create(task=task) conn = db.engine.connect() result_id = create_result(conn, task.project_id, task.id) result = result_repo.filter_by(project_id=task.project_id, task_id=task.id, last_version=True)[0] assert mock_create_result.called err_msg = "The result should ID should be the same" assert result_id == result.id, err_msg task.n_answers = 2 task_repo.update(task) TaskRunFactory.create(task=task) result_id = create_result(conn, task.project_id, task.id) assert mock_create_result.called result = result_repo.filter_by(project_id=task.project_id, task_id=task.id, last_version=True) assert len(result) == 1, len(result) result = result[0] err_msg = "The result should ID should be the same" assert result_id == result.id, err_msg
def _file_upload(self, data): """Method that must be overriden by the class to allow file uploads for only a few classes.""" cls_name = self.__class__.__name__.lower() content_type_text = 'application/json' request_headers = request.headers.get('Content-Type') if request_headers is None: request_headers = [] if ((content_type_text in request_headers) and cls_name in self.allowed_classes_upload): data = dict() enc = json.loads(request.data) data['id'] = enc['task_id'] task = task_repo.get_task(enc['task_id']) data['project_id'] = task.project_id task.state = "completed" task.flagged = 1 task_repo.update(task) data['state'] = 'completed' data['flagged'] = 1 data['project_id'] = 1 flag_task = FlaggedTask(project_id=data['project_id'], task_id=data['id'], user_id=current_user.id, reason=enc['reason']) flagged_task_repo.save(flag_task) return data else: return None
def task_gold(project_id=None): """Make task gold""" if not current_user.is_authenticated: return abort(401) project = project_repo.get(project_id) if project is None or not (current_user.admin or current_user.id in project.owners_ids): raise Forbidden task_data = request.json task_id = task_data['task_id'] task = task_repo.get_task(task_id) if task.project_id != project_id: raise Forbidden task.calibration = 1 task.exported = True task.state = 'ongoing' preprocess_task_run(project_id, task_id, task_data) info = task_data['info'] if data_access_levels: task.gold_answers = upload_gold_data(task, project_id, info, task_id) else: task.gold_answers = info task_repo.update(task) return Response(json.dumps({'success': True}), 200, mimetype="application/json")
def post(self): """Add User ID to task as a favorite.""" try: self.valid_args() data = json.loads(request.data) if (len(data.keys()) != 1) or ('task_id' not in data.keys()): raise AttributeError if current_user.is_anonymous(): raise Unauthorized uid = current_user.id tasks = task_repo.get_task_favorited(uid, data['task_id']) if len(tasks) == 1: task = tasks[0] if len(tasks) == 0: task = task_repo.get_task(data['task_id']) if task is None: raise NotFound if task.fav_user_ids is None: task.fav_user_ids = [uid] else: task.fav_user_ids.append(uid) task_repo.update(task) self._log_changes(None, task) return Response(json.dumps(task.dictize()), 200, mimetype='application/json') except Exception as e: return error.format_exception( e, target=self.__class__.__name__.lower(), action='POST')
def anonymize_ips(): """Anonymize all the IPs of the server.""" from pybossa.core import anonymizer, task_repo taskruns = task_repo.filter_task_runs_by(user_id=None) for tr in taskruns: print "Working on taskrun %s" % tr.id print "From %s to %s" % (tr.user_ip, anonymizer.ip(tr.user_ip)) tr.user_ip = anonymizer.ip(tr.user_ip) task_repo.update(tr)
def test_get_returns_result_after_increasig_redundancy(self): """Test get method returns a result if after increasing redundancy""" n_answers = 1 task = TaskFactory.create(n_answers=n_answers) task_run = TaskRunFactory.create(task=task) result = self.result_repo.filter_by(project_id=1) err_msg = "There should be a result" assert len(result) == 1, err_msg result = result[0] assert result.project_id == 1, err_msg assert result.task_id == task.id, err_msg assert len(result.task_run_ids) == n_answers, err_msg err_msg = "The task_run id is missing in the results array" for tr_id in result.task_run_ids: assert tr_id == task_run.id, err_msg # Increase redundancy tmp = task_repo.get_task(task.id) tmp.n_answers = 2 task_repo.update(task) err_msg = "There should be only one result" results = result_repo.filter_by(project_id=1) assert len(results) == 1, err_msg task_run_2 = TaskRunFactory.create(task=task) err_msg = "There should be 1 results" results = result_repo.filter_by(project_id=1) assert len(results) == 1, err_msg err_msg = "There should be 2 results" results = result_repo.filter_by(project_id=1, last_version=False) assert len(results) == 2, err_msg assert results[1].project_id == 1, err_msg assert results[1].task_id == task.id, err_msg err_msg = "First result should have only one task run ID" assert len(results[0].task_run_ids) == 1, err_msg err_msg = "Second result should have only two task run IDs" assert len(results[1].task_run_ids) == 2, err_msg err_msg = "The task_run id is missing in the results array" for tr_id in results[1].task_run_ids: assert tr_id in [task_run.id, task_run_2.id], err_msg
def delete(self, oid): """Delete User ID from task as a favorite.""" try: if current_user.is_anonymous(): raise abort(401) uid = current_user.id tasks = task_repo.get_task_favorited(uid, oid) if tasks == []: raise NotFound if len(tasks) == 1: task = tasks[0] idx = task.fav_user_ids.index(uid) task.fav_user_ids.pop(idx) task_repo.update(task) return Response(json.dumps(task.dictize()), 200, mimetype='application/json') except Exception as e: return error.format_exception( e, target=self.__class__.__name__.lower(), action='DEL')
def task_gold(project_id=None): """Make task gold""" try: if not current_user.is_authenticated: return abort(401) project = project_repo.get(project_id) # Allow project owner, sub-admin co-owners, and admins to update Gold tasks. is_gold_access = (current_user.subadmin and current_user.id in project.owners_ids) or current_user.admin if project is None or not is_gold_access: raise Forbidden if request.method == 'POST': task_data = json.loads( request.form['request_json'] ) if 'request_json' in request.form else request.json task_id = task_data['task_id'] task = task_repo.get_task(task_id) if task.project_id != project_id: raise Forbidden preprocess_task_run(project_id, task_id, task_data) info = task_data['info'] set_gold_answers(task, info) task_repo.update(task) response_body = json.dumps({'success': True}) else: task = sched.select_task_for_gold_mode(project, current_user.id) if task: task = task.dictize() sign_task(task) response_body = json.dumps(task) return Response(response_body, 200, mimetype="application/json") except Exception as e: return error.format_exception(e, target='taskgold', action=request.method)
def _test_get_breadth_first_task_limit(self, user=None, external_uid=None): admin, owner, user = UserFactory.create_batch(3) project = ProjectFactory.create(owner=owner, info=dict(sched='breadth_first')) tasks = TaskFactory.create_batch(3, project=project, n_answers=3) # now check we get task without task runs as anonymous user limit = 2 out = pybossa.sched.get_breadth_first_task(project.id, limit=limit) assert len(out) == limit, out assert out[0].id == tasks[0].id, out assert out[1].id == tasks[1].id, out # now check we get task without task runs as a user out = pybossa.sched.get_breadth_first_task(project.id, user.id, limit=limit) assert len(out) == limit, out assert out[0].id == tasks[0].id, out assert out[1].id == tasks[1].id, out # now check we get task without task runs as a external uid out = pybossa.sched.get_breadth_first_task(project.id, external_uid=external_uid, limit=limit) assert len(out) == limit, out assert out[0].id == tasks[0].id, out assert out[1].id == tasks[1].id, out # now check that offset works out1 = pybossa.sched.get_breadth_first_task(project.id, limit=limit) out2 = pybossa.sched.get_breadth_first_task(project.id, offset=1, limit=limit) assert len(out1) == limit, out1 assert len(out2) == limit, out2 assert out1 != out2, (out1, out2) assert out1[0].id == tasks[0].id, (tasks[0], out1) assert out1[1].id == tasks[1].id, (tasks[1], out1) assert out2[0].id == tasks[1].id, (tasks[1], out2[0]) assert out2[1].id == tasks[2].id, (tasks[2], out2) # asking for a bigger offset (max 10) out2 = pybossa.sched.get_breadth_first_task(project.id, offset=11, limit=2) assert out2 == [], out2 # Create a taskrun by Anon, so the next task should be returned for anon and auth users self.create_task_run(project, user) out = pybossa.sched.get_breadth_first_task(project.id, limit=limit) assert len(out) == limit, out assert out[0].id == tasks[1].id, out assert out[1].id == tasks[2].id, out out = pybossa.sched.get_breadth_first_task(project.id, user.id, limit=limit) assert len(out) == limit, out assert out[0].id == tasks[1].id, out assert out[1].id == tasks[2].id, out # We create another taskrun and the last task should be returned first, as we # are getting two tasks. It should always return first the tasks with less number # of task runs self.create_task_run(project, user) out = pybossa.sched.get_breadth_first_task(project.id, limit=limit) assert len(out) == limit, out assert out[0].id == tasks[2].id, out out = pybossa.sched.get_breadth_first_task(project.id, owner.id, limit=limit) assert len(out) == limit, out assert out[0].id == tasks[2].id, out # Add another taskrun to first task, so we have 2, 1, 0 taskruns for each task TaskRunFactory.create(task=tasks[0], project=project, id=15) out = pybossa.sched.get_breadth_first_task(project.id, UserFactory.create().id, limit=limit) assert len(out) == limit, out out = out[0] assert out.id == tasks[2].id, out # Mark last task as completed, so the scheduler returns tasks[1] task = task_repo.get_task(tasks[2].id) task.state = 'completed' task_repo.update(task) out = pybossa.sched.get_breadth_first_task(project.id, UserFactory.create().id, limit=2) assert len(out) == limit, out out = out[0] assert out.id == tasks[1].id, out
def _test_get_breadth_first_task(self, user=None, external_uid=None): admin, owner, user = UserFactory.create_batch(3) project = ProjectFactory.create(owner=owner, info=dict(sched='breadth_first')) tasks = TaskFactory.create_batch(3, project=project, n_answers=3) # now check we get task without task runs as anonymous user out = pybossa.sched.get_breadth_first_task(project.id) assert len(out) == 1, out out = out[0] assert out.id == tasks[0].id, out # now check we get task without task runs as a user out = pybossa.sched.get_breadth_first_task(project.id, user.id) assert len(out) == 1, out out = out[0] assert out.id == tasks[0].id, out # now check we get task without task runs as a external uid out = pybossa.sched.get_breadth_first_task(project.id, external_uid=external_uid) assert len(out) == 1, out out = out[0] assert out.id == tasks[0].id, out # now check that offset works out1 = pybossa.sched.get_breadth_first_task(project.id) out2 = pybossa.sched.get_breadth_first_task(project.id, offset=1) assert len(out1) == 1, out1 assert len(out2) == 1, out2 assert out1[0].id != out2[0].id, (out1, out2) assert out1[0].id == tasks[0].id, (tasks[0], out1) assert out2[0].id == tasks[1].id, (tasks[1], out2) # Now check that orderby works out1 = pybossa.sched.get_breadth_first_task(project.id, orderby='created', desc=True) assert out1[0].id == tasks[2].id, out1 out1 = pybossa.sched.get_breadth_first_task(project.id, orderby='created', desc=False) assert out1[0].id == tasks[0].id, out1 # Now check that orderby works with fav_user_ids t = task_repo.get_task(tasks[1].id) t.fav_user_ids = [1, 2, 3, 4, 5] task_repo.update(t) t = task_repo.get_task(tasks[2].id) t.fav_user_ids = [1, 2, 3] task_repo.update(t) out1 = pybossa.sched.get_breadth_first_task(project.id, orderby='fav_user_ids', desc=True) assert out1[0].id == tasks[1].id, out1 assert out1[0].fav_user_ids == [1, 2, 3, 4, 5], out1[0].dictize() out1 = pybossa.sched.get_breadth_first_task(project.id, orderby='fav_user_ids', desc=False, offset=1) assert out1[0].id == tasks[2].id, out1[0].dictize() assert out1[0].fav_user_ids == [1, 2, 3], out1 # asking for a bigger offset (max 10) out2 = pybossa.sched.get_breadth_first_task(project.id, offset=11) assert out2 == [], out2 # Create a taskrun, so the next task should be returned for anon and auth users self.create_task_run(project, user) out = pybossa.sched.get_breadth_first_task(project.id) assert len(out) == 1, out out = out[0] assert out.id == tasks[1].id, out out = pybossa.sched.get_breadth_first_task(project.id, user.id) assert len(out) == 1, out out = out[0] assert out.id == tasks[1].id, out # We create another taskrun and the last task should be returned self.create_task_run(project, user) out = pybossa.sched.get_breadth_first_task(project.id) assert len(out) == 1, out out = out[0] assert out.id == tasks[2].id, out out = pybossa.sched.get_breadth_first_task(project.id, owner.id) assert len(out) == 1, out out = out[0] assert out.id == tasks[2].id, out # Add another taskrun to first task, so we have 2, 1, 0 taskruns for each task TaskRunFactory.create(task=tasks[0], project=project, id=15) out = pybossa.sched.get_breadth_first_task(project.id, UserFactory.create().id) assert len(out) == 1, out out = out[0] assert out.id == tasks[2].id, out # Mark last task as completed, so the scheduler returns tasks[1] task = task_repo.get_task(tasks[2].id) task.state = 'completed' task_repo.update(task) out = pybossa.sched.get_breadth_first_task(project.id, UserFactory.create().id) assert len(out) == 1, out out = out[0] assert out.id == tasks[1].id, out