Exemple #1
0
def _retrieve_new_task(project_id):

    project = project_repo.get(project_id)
    if project is None or not (project.published or current_user.admin
                               or current_user.id in project.owners_ids):
        raise NotFound

    if current_user.is_anonymous:
        info = dict(error="This project does not allow anonymous contributors")
        error = [model.task.Task(info=info)]
        return error, None, lambda x: x

    if current_user.get_quiz_failed(project):
        # User is blocked from project so don't return a task
        return None, None, None

    # check cookie
    pwd_manager = get_pwd_manager(project)
    user_id_or_ip = get_user_id_or_ip()
    if pwd_manager.password_needed(project, user_id_or_ip):
        raise Forbidden("No project password provided")

    if request.args.get('external_uid'):
        resp = jwt_authorize_project(project,
                                     request.headers.get('Authorization'))
        if resp != True:
            return resp, lambda x: x

    if request.args.get('limit'):
        limit = int(request.args.get('limit'))
    else:
        limit = 1

    if limit > 100:
        limit = 100

    if request.args.get('offset'):
        offset = int(request.args.get('offset'))
    else:
        offset = 0

    if request.args.get('orderby'):
        orderby = request.args.get('orderby')
    else:
        orderby = 'id'

    if request.args.get('desc'):
        desc = fuzzyboolean(request.args.get('desc'))
    else:
        desc = False

    user_id = None if current_user.is_anonymous else current_user.id
    user_ip = (anonymizer.ip(request.remote_addr or '127.0.0.1')
               if current_user.is_anonymous else None)
    external_uid = request.args.get('external_uid')
    sched_rand_within_priority = project.info.get('sched_rand_within_priority',
                                                  False)

    user = user_repo.get(user_id)
    if (user.get_quiz_not_started(project) and user.get_quiz_enabled(project)
            and not task_repo.get_user_has_task_run_for_project(
                project_id, user_id)):
        user.set_quiz_status(project, 'in_progress')

    user_repo.update(user)

    task = sched.new_task(project.id,
                          project.info.get('sched'),
                          user_id,
                          user_ip,
                          external_uid,
                          offset,
                          limit,
                          orderby=orderby,
                          desc=desc,
                          rand_within_priority=sched_rand_within_priority,
                          gold_only=user.get_quiz_in_progress(project))

    handler = partial(pwd_manager.update_response,
                      project=project,
                      user=user_id_or_ip)
    return task, project.info.get('timeout'), handler
Exemple #2
0
def _retrieve_new_task(project_id):

    project = project_repo.get(project_id)
    if project is None or not (project.published or current_user.admin
                               or current_user.id in project.owners_ids):
        raise NotFound

    if current_user.is_anonymous:
        info = dict(error="This project does not allow anonymous contributors")
        error = [model.task.Task(info=info)]
        return error, None, lambda x: x

    if current_user.get_quiz_failed(project):
        # User is blocked from project so don't return a task
        return None, None, None

    # check cookie
    pwd_manager = get_pwd_manager(project)
    user_id_or_ip = get_user_id_or_ip()
    if pwd_manager.password_needed(project, user_id_or_ip):
        raise Forbidden("No project password provided")

    if request.args.get('external_uid'):
        resp = jwt_authorize_project(project,
                                     request.headers.get('Authorization'))
        if resp != True:
            return resp, lambda x: x

    if request.args.get('limit'):
        limit = int(request.args.get('limit'))
    else:
        limit = 1

    if limit > 100:
        limit = 100

    if request.args.get('offset'):
        offset = int(request.args.get('offset'))
    else:
        offset = 0

    if request.args.get('orderby'):
        orderby = request.args.get('orderby')
    else:
        orderby = 'id'

    if request.args.get('desc'):
        desc = fuzzyboolean(request.args.get('desc'))
    else:
        desc = False

    user_id = None if current_user.is_anonymous else current_user.id
    user_ip = (anonymizer.ip(request.remote_addr or '127.0.0.1')
               if current_user.is_anonymous else None)
    external_uid = request.args.get('external_uid')
    sched_rand_within_priority = project.info.get('sched_rand_within_priority',
                                                  False)

    user = user_repo.get(user_id)
    if (project.published and user_id != project.owner_id
            and user_id not in project.owners_ids
            and user.get_quiz_not_started(project)
            and user.get_quiz_enabled(project)
            and not task_repo.get_user_has_task_run_for_project(
                project_id, user_id)):
        user.set_quiz_status(project, 'in_progress')

    # We always update the user even if we didn't change the quiz status.
    # The reason for that is the user.<?quiz?> methods take a snapshot of the project's quiz
    # config the first time it is accessed for a user and save that snapshot
    # with the user. So we want to commit that snapshot if this is the first access.
    user_repo.update(user)

    # Allow scheduling a gold-only task if quiz mode is enabled for the user and the project.
    quiz_mode_enabled = user.get_quiz_in_progress(
        project) and project.info["quiz"]["enabled"]

    task = sched.new_task(project.id,
                          project.info.get('sched'),
                          user_id,
                          user_ip,
                          external_uid,
                          offset,
                          limit,
                          orderby=orderby,
                          desc=desc,
                          rand_within_priority=sched_rand_within_priority,
                          gold_only=quiz_mode_enabled)

    handler = partial(pwd_manager.update_response,
                      project=project,
                      user=user_id_or_ip)
    return task, project.info.get('timeout'), handler