def test_task_save_sufficient_permissions(self): patched_levels = self.patched_levels( valid_project_levels_for_task_level={'L4': ['L4']}, valid_task_levels_for_project_level={'L4': ['L4']}) with patch.dict(data_access.data_access_levels, patched_levels): project = ProjectFactory.create() task = Task(project_id=project.id, info={'data_access': ['L4']}) task_repo.save(task)
def test_task_save_insufficient_permissions(self): patched_levels = self.patched_levels( valid_project_levels_for_task_level={'A': ['B']}, valid_task_levels_for_project_level={'B': ['C']}) with patch.dict(data_access.data_access_levels, patched_levels): project = ProjectFactory.create(info={'data_access': ['B']}) with assert_raises(Exception): task = Task(project_id=project.id, info={'data_access': ['A']}) task_repo.save(task)
def test_task_2(self): owner = UserFactory.create(id=500) owner.user_pref = {'languages': ['de', 'en']} user_repo.save(owner) project = ProjectFactory.create(owner=owner) project.info['sched'] = Schedulers.user_pref task = TaskFactory.create_batch(1, project=project, n_answers=10)[0] task.user_pref = {'languages': ['en', 'zh']} task_repo.save(task) assert n_available_tasks_for_user(project, 500) == 1
def test_no_user_pref(self): """ Task has preferences set, user doesn't """ owner = UserFactory.create(id=500) user_repo.save(owner) project = ProjectFactory.create(owner=owner) task = TaskFactory.create_batch(1, project=project, n_answers=10)[0] task.user_pref = {'languages': ['en', 'de']} task_repo.save(task) tasks = get_user_pref_task(1, 500) assert not tasks
def test_task_save_sufficient_permissions(self): patched_levels = self.patched_levels( valid_project_levels_for_task_level={'A': ['B']}, valid_task_levels_for_project_level={'A': ['B']} ) with patch.dict(data_access.data_access_levels, patched_levels): project = ProjectFactory.create(info={ 'data_access': ['A'], 'ext_config': {'data_access': {'tracking_id': '123'}} }) task = Task(project_id=project.id, info={'data_access': ['A']}) task_repo.save(task)
def test_task_2(self): """ Task has multiple preferences, user has multiple preferences; match """ owner = UserFactory.create(id=500) owner.user_pref = {'languages': ['en', 'de']} user_repo.save(owner) project = ProjectFactory.create(owner=owner) task = TaskFactory.create_batch(1, project=project, n_answers=10)[0] task.user_pref = {'languages': ['en', 'es']} task_repo.save(task) tasks = get_user_pref_task(1, 500) assert tasks
def test_task_1(self): ''' Default user scheduler doesn't check user preference ''' owner = UserFactory.create(id=500) owner.user_pref = {'languages': ['de']} user_repo.save(owner) project = ProjectFactory.create(owner=owner) project.info['sched'] = Schedulers.locked task = TaskFactory.create_batch(1, project=project, n_answers=10)[0] task.user_pref = {'languages': ['en', 'zh']} task_repo.save(task) assert n_available_tasks_for_user(project, 500) == 1
def test_task_7(self): """ Invalid user preference """ owner = UserFactory.create(id=500) owner.user_pref = {'languages': 'invalid_user_pref'} user_repo.save(owner) project = ProjectFactory.create(owner=owner) task = TaskFactory.create_batch(1, project=project, n_answers=10)[0] task.user_pref = {'languages': ['en', 'zh']} task_repo.save(task) tasks = get_user_pref_task(1, 500) assert not tasks
def test_task_6(self): """ User has multiple preferences of different kinds, task has multiple preferences of different kinds, no match """ owner = UserFactory.create(id=500) owner.user_pref = {'languages': ['de'], 'locations': ['us']} user_repo.save(owner) project = ProjectFactory.create(owner=owner) task = TaskFactory.create_batch(1, project=project, n_answers=10)[0] task.user_pref = {'languages': ['en', 'zh'], 'locations': ['es']} task_repo.save(task) tasks = get_user_pref_task(1, 500) assert not tasks
def test_task_routing_3(self): ''' User has empty profile set, only tasks that have empty filter can be assigned to user ''' user_info = dict(metadata={"profile": json.dumps({})}) owner = UserFactory.create(id=500, info=user_info) user_repo.save(owner) project = ProjectFactory.create(owner=owner) project.info['sched'] = Schedulers.user_pref tasks = TaskFactory.create_batch(3, project=project, n_answers=10) tasks[0].worker_filter = {'finance': [0.8, '>=']} task_repo.save(tasks[0]) tasks[1].worker_filter = {'geography': [0.5, '>=']} task_repo.save(tasks[1]) assert n_available_tasks_for_user(project, 500) == 1
def test_task_4(self): ''' Tasks match user profile ''' owner = UserFactory.create(id=500) owner.user_pref = {'languages': ['de', 'en']} user_repo.save(owner) project = ProjectFactory.create(owner=owner) project.info['sched'] = Schedulers.user_pref tasks = TaskFactory.create_batch(2, project=project, n_answers=10) tasks[0].user_pref = {'languages': ['en', 'zh']} task_repo.save(tasks[0]) tasks[1].user_pref = {'languages': ['de']} task_repo.save(tasks[1]) assert n_available_tasks_for_user(project, 500) == 2
def test_task_6(self): ''' Task is assigned to a user whose profile doesn't match task preference ''' owner = UserFactory.create(id=500) owner.user_pref = {'languages': ['de', 'en']} owner.email_addr = '*****@*****.**' user_repo.save(owner) project = ProjectFactory.create(owner=owner) project.info['sched'] = Schedulers.user_pref task = TaskFactory.create_batch(1, project=project, n_answers=10)[0] task.user_pref = { 'languages': ['ch', 'zh'], 'assign_user': ['*****@*****.**'] } task_repo.save(task) assert n_available_tasks_for_user(project, 500) == 0
def add_task(project): store_questions(project) project_id=project.id project_path=session["zzz"] if(session.get("question") is not None): for i in ["images","videos","documents","audios"]: if os.path.exists(project_path+"/"+i): print "in if" for file in os.listdir(project_path+"/"+i): p=os.path.join(project_path+"/"+i,file) p=p[p.rfind("uploads"):] print p dictobj={"type":i,"url":"/"+p,"subtype":file.rsplit('.',1)[1].lower()} s=json.dumps(dictobj) #print s.type task = Task(project_id=project_id) task.info=dictobj task_repo.save(task) session.pop('question', None)
def test_task_7(self): ''' task[0]: doesn't have any language/country preference, should be able to assign to the user. task[1]: both preference and email match, should be able to assign to the user ''' owner = UserFactory.create(id=500) owner.user_pref = {'languages': ['de', 'en']} owner.email_addr = '*****@*****.**' user_repo.save(owner) project = ProjectFactory.create(owner=owner) project.info['sched'] = Schedulers.user_pref tasks = TaskFactory.create_batch(3, project=project, n_answers=10) tasks[0].user_pref = {'assign_user': ['*****@*****.**']} task_repo.save(tasks[0]) tasks[1].user_pref = { 'languages': ['de'], 'assign_user': ['*****@*****.**'] } task_repo.save(tasks[1]) assert n_available_tasks_for_user(project, 500) == 2
def test_task_5(self): from pybossa import data_access owner = UserFactory.create(id=500) user = UserFactory.create(id=501, info=dict(data_access=["L1"])) patch_data_access_levels = dict( valid_access_levels=["L1", "L2", "L3", "L4"], valid_user_levels_for_project_level=dict(L1=[], L2=["L1"]), valid_project_levels_for_user_level=dict(L1=["L2", "L3", "L4"], L2=["L3", "L4"]), ) user_repo.save(owner) project = ProjectFactory.create(owner=owner, info=dict(project_users=[owner.id])) project.info['sched'] = Schedulers.user_pref tasks = TaskFactory.create_batch(3, project=project, n_answers=2, info=dict(data_access=["L1"])) tasks[0].info['data_access'] = ["L1"] task_repo.save(tasks[0]) tasks[1].info['data_access'] = ["L1"] task_repo.save(tasks[1]) tasks[2].info['data_access'] = ["L2"] task_repo.save(tasks[2]) with patch.object(data_access, 'data_access_levels', patch_data_access_levels): assert n_available_tasks_for_user(project, 501) == 3
def test_recent_contributor_with_multiple_user_pref_notified(self): """ User with multiple user pref to be notified when one of his/her user pref matches any new task user pref """ owner = UserFactory.create(id=500) owner.user_pref = {'languages': ['en']} # owner is english user user_repo.save(owner) sp_fr_user = UserFactory.create(id=501) sp_fr_user.user_pref = { 'languages': ['sp', 'fr'] } # spanish french language user user_repo.save(sp_fr_user) # Stage 1 : # Create 3 tasks - 1 english, 1 spanish, 1 french. project = ProjectFactory.create(owner=owner) project.info['sched'] = Schedulers.user_pref tasks = TaskFactory.create_batch(6, project=project, n_answers=1) tasks[0].user_pref = {'languages': ['en']} task_repo.save(tasks[0]) tasks[1].user_pref = {'languages': ['sp']} task_repo.save(tasks[1]) tasks[2].user_pref = {'languages': ['fr']} task_repo.save(tasks[2]) # Submit 1 english and spanish tasks. this will complete # such tasks and assoicated users to be notified about new task imported taskrun1 = TaskRunFactory.create(task=tasks[0], user=owner) taskrun2 = TaskRunFactory.create(task=tasks[1], user=sp_fr_user) # Stage 2 : # create 1 spanish task # at this stage, record current time. # there is french ongoing task, but since spanish task is complete # sp_fr_user will be notified now = datetime.datetime.utcnow().isoformat() tasks = TaskFactory.create_batch(1, project=project, n_answers=1) tasks[0].user_pref = {'languages': ['sp']} task_repo.save(tasks[0]) recent_contributors = user_repo.get_user_pref_recent_contributor_emails( project.id, now) # with one spanish task completed, user2 will be notified about new spanish task imported err_msg = 'There should be 1 contributors' assert len(recent_contributors) == 1, err_msg err_msg = 'user1 and user3 with english and chinese language preference should be notified' assert '*****@*****.**' in recent_contributors, err_msg
def test_task_routing_2(self): ''' task[0]: needs finance skill at least 0.8, should not be able to assign to the user task[1]: needs geography skill at least 0.5, should not be able to assign to the user task[3]: doesnt have filters, should be able to assign to the user ''' user_info = dict( metadata={ "profile": json.dumps({ "finance": 0.6, "marketing": 0.4 }) }) owner = UserFactory.create(id=500, info=user_info) user_repo.save(owner) project = ProjectFactory.create(owner=owner) project.info['sched'] = Schedulers.user_pref tasks = TaskFactory.create_batch(3, project=project, n_answers=10) tasks[0].worker_filter = {'finance': [0.8, '>=']} task_repo.save(tasks[0]) tasks[1].worker_filter = {'geography': [0.5, '>=']} task_repo.save(tasks[1]) assert n_available_tasks_for_user(project, 500) == 1
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() """Accepting both content types - text or with file""" content_type_file = 'multipart/form-data' content_type_text = 'application/x-www-form-urlencoded' request_headers = request.headers.get('Content-Type') if request_headers is None: request_headers = [] """ check for content type - file or text""" if ( (content_type_file in request_headers or content_type_text in request_headers) and cls_name in self.allowed_classes_upload): data = dict() for key in list(request.form.keys()): #Adding user_id in data if key in ['project_id']: data[key] = int(request.form[key]) elif key == 'info': data[key] = json.loads(request.form[key]) else: data[key] = request.form[key] #Check if task exists tasks = task_repo.getTasks(data['info']['uuid'],data['project_id']) try: #if it exists, add as task id task = [row[0] for row in tasks] data['task_id'] = task[0] except: #if does not exist, add new task info = data['info'] task = Task(project_id=data['project_id'], info=info,n_answers=10) task_repo.save(task) data['task_id'] = task.id """Try to get user by uuid, if not present, add a new user""" user = user_repo.get_by(mykaarma_user_id=data['useruuid']) if(user is None): name = get_mykaarma_username_from_full_name(data["fullname"]) user = user_repo.get_by_name(name) while(user is not None): name = get_mykaarma_username_from_full_name(data["fullname"]) user = user_repo.get_by_name(name) user = User(fullname=data['fullname'], name=name, email_addr=data['email'], mykaarma_user_id=data['useruuid']) user_repo.save(user) """ add user id extracted from user repo""" data['user_id'] = user.id """ delete extra keys to suit Taskrun class format""" del data['useruuid'] del data['fullname'] del data['email'] data = self.hateoas.remove_links(data) inst = self.__class__(**data) self._add_user_info(inst) is_authorized(current_user, 'create', inst) upload_method = current_app.config.get('UPLOAD_METHOD') """Add user id to container""" container = "user_%s" % data['user_id'] if data.get('info') is None: data['info'] = dict() data['info']['container'] = container if(request.files.get('file') is not None): _file = request.files['file'] if _file.filename == 'blob' or _file.filename is None: _file.filename = "%s.png" % time.time() uploader.upload_file(_file, container=container) avatar_absolute = current_app.config.get('AVATAR_ABSOLUTE') file_url = get_avatar_url(upload_method, _file.filename, container, avatar_absolute) data['media_url'] = file_url data['info']['file_name'] = _file.filename return data else: return None
def test_recent_contributors_list_as_per_user_pref(self): """ Notify users about new tasks imported based on user preference and those who were not notified previously """ owner = UserFactory.create(id=500) owner.user_pref = {'languages': ['en']} # owner is english user user_repo.save(owner) ch_user = UserFactory.create(id=501) ch_user.user_pref = {'languages': ['ch']} # chinese language user user_repo.save(ch_user) ru_user = UserFactory.create(id=502) ru_user.user_pref = {'languages': ['ru']} # russian language user user_repo.save(ru_user) # Stage 1 : # Create 4 tasks - 3 english, 1 chinese. project = ProjectFactory.create(owner=owner) project.info['sched'] = Schedulers.user_pref tasks = TaskFactory.create_batch(4, project=project, n_answers=1) tasks[0].user_pref = {'languages': ['en']} task_repo.save(tasks[0]) tasks[1].user_pref = {'languages': ['en']} task_repo.save(tasks[1]) tasks[2].user_pref = {'languages': ['ru']} task_repo.save(tasks[2]) tasks[3].user_pref = {'languages': ['ch']} task_repo.save(tasks[3]) # Complete 2 english and 1 russian tasks # completing 1 russian task will mark all russian tasks completed # and such users to be notified about new task imported taskrun1 = TaskRunFactory.create(task=tasks[0], user=owner) taskrun2 = TaskRunFactory.create(task=tasks[1], user=owner) taskrun3 = TaskRunFactory.create(task=tasks[2], user=ru_user) # Stage 2 : # create 3 more tasks; 2 russian and 1 chinese # at this stage, record current time. # chinese user has existing ongoing task, hence won't be notified # russian user has all tasks completed, hence will be notified now = datetime.datetime.utcnow().isoformat() tasks = TaskFactory.create_batch(3, project=project, n_answers=1) tasks[0].user_pref = {'languages': ['ru']} task_repo.save(tasks[0]) tasks[1].user_pref = {'languages': ['ru']} task_repo.save(tasks[1]) tasks[2].user_pref = {'languages': ['ch']} task_repo.save(tasks[2]) recent_contributors = user_repo.get_user_pref_recent_contributor_emails( project.id, now) # with russian task completed, russian user will be notified about new task imported err_msg = 'There should be 1 contributors' assert len(recent_contributors) == 1, err_msg err_msg = 'only user3 that has language preference russian should be notified' assert recent_contributors[0] == '*****@*****.**', err_msg
def test_recent_contributors_list_with_multiple_user_pref(self): """ User with multiple user pref to be excluded from notifying when there are existing ongoing tasks matching any one of same user pref """ owner = UserFactory.create(id=500) owner.user_pref = {'languages': ['en']} # owner is english user user_repo.save(owner) sp_fr_user = UserFactory.create(id=501) sp_fr_user.user_pref = { 'languages': ['sp', 'fr'] } # spanish french language user user_repo.save(sp_fr_user) ch_user = UserFactory.create(id=502) ch_user.user_pref = {'languages': ['ch']} # russian language user user_repo.save(ch_user) # Stage 1 : # Create 4 tasks - 3 english, 1 chinese. project = ProjectFactory.create(owner=owner) project.info['sched'] = Schedulers.user_pref tasks = TaskFactory.create_batch(6, project=project, n_answers=1) tasks[0].user_pref = {'languages': ['en']} task_repo.save(tasks[0]) tasks[1].user_pref = {'languages': ['sp']} task_repo.save(tasks[1]) tasks[2].user_pref = {'languages': ['sp']} task_repo.save(tasks[2]) tasks[3].user_pref = {'languages': ['fr']} task_repo.save(tasks[3]) tasks[4].user_pref = {'languages': ['fr']} task_repo.save(tasks[4]) tasks[5].user_pref = {'languages': ['ch']} task_repo.save(tasks[5]) # Submit 1 english and chinese task runs. this will complete # such tasks and assoicated users to be notified about new task imported taskrun1 = TaskRunFactory.create(task=tasks[0], user=owner) taskrun2 = TaskRunFactory.create(task=tasks[5], user=ch_user) # Submit 1 spanish and 1 french task runs. since there are 1 each onging # tasks, assoicated users wont be notified about new task imported taskrun1 = TaskRunFactory.create(task=tasks[1], user=sp_fr_user) taskrun2 = TaskRunFactory.create(task=tasks[3], user=sp_fr_user) # Stage 2 : # create 3 more tasks; 1 spanish, 1 french and 1 chinese # at this stage, record current time. # spanish and french user has existing ongoing task, hence won't be notified # chinese and english user has all tasks completed, hence will be notified now = datetime.datetime.utcnow().isoformat() tasks = TaskFactory.create_batch(4, project=project, n_answers=1) tasks[0].user_pref = {'languages': ['en']} task_repo.save(tasks[0]) tasks[1].user_pref = {'languages': ['sp']} task_repo.save(tasks[1]) tasks[2].user_pref = {'languages': ['fr']} task_repo.save(tasks[2]) tasks[3].user_pref = {'languages': ['ch']} task_repo.save(tasks[3]) recent_contributors = user_repo.get_user_pref_recent_contributor_emails( project.id, now) # with english and chinese task completed, two such user will be notified about new task imported err_msg = 'There should be 2 contributors' assert len(recent_contributors) == 2, err_msg err_msg = 'user1 and user3 with english and chinese language preference should be notified' assert ('*****@*****.**' in recent_contributors and '*****@*****.**' in recent_contributors and '*****@*****.**' not in recent_contributors), err_msg