Beispiel #1
0
def _retrieve_new_task(project_id):

    project = project_repo.get(project_id)

    if project is None:
        raise NotFound

    if not project.allow_anonymous_contributors and current_user.is_anonymous():
        info = dict(
            error="This project does not allow anonymous contributors")
        error = model.task.Task(info=info)
        return error

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

    if request.args.get('offset'):
        offset = int(request.args.get('offset'))
    else:
        offset = 0
    user_id = None if current_user.is_anonymous() else current_user.id
    user_ip = request.remote_addr if current_user.is_anonymous() else None
    external_uid = request.args.get('external_uid')
    task = sched.new_task(project_id, project.info.get('sched'),
                          user_id,
                          user_ip,
                          external_uid,
                          offset)
    return task
Beispiel #2
0
def new_task(app_id):
    # Check if the request has an arg:
    try:
        app = db.session.query(model.App).get(app_id)
        if app is None:
            raise NotFound
        if request.args.get('offset'):
            offset = int(request.args.get('offset'))
        else:
            offset = 0
        user_id = None if current_user.is_anonymous() else current_user.id
        user_ip = request.remote_addr if current_user.is_anonymous() else None
        task = sched.new_task(app_id, user_id, user_ip, offset)
        # If there is a task for the user, return it

        if task:
            s = URLSafeSerializer(current_app.config.get('SECRET_KEY'))
            r = make_response(json.dumps(task.dictize()))
            r.mimetype = "application/json"
            cookie_id = 'task_run_for_task_id_%s' % task.id
            r.set_cookie(cookie_id, s.dumps(task.dictize()))
            return r

        else:
            return Response(json.dumps({}), mimetype="application/json")
    except Exception as e:
        return error.format_exception(e, target='app', action='GET')
Beispiel #3
0
def _retrieve_new_task(project_id):

    project = project_repo.get(project_id)

    if project is None:
        raise NotFound

    if not project.allow_anonymous_contributors and current_user.is_anonymous(
    ):
        info = dict(error="This project does not allow anonymous contributors")
        error = model.task.Task(info=info)
        return error

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

    if request.args.get('offset'):
        offset = int(request.args.get('offset'))
    else:
        offset = 0
    user_id = None if current_user.is_anonymous() else current_user.id
    user_ip = request.remote_addr if current_user.is_anonymous() else None
    external_uid = request.args.get('external_uid')
    task = sched.new_task(project_id, project.info.get('sched'), user_id,
                          user_ip, external_uid, offset)
    return task
Beispiel #4
0
def new_task(app_id):
    """Return a new task for an application."""
    # Check if the request has an arg:
    try:
        app = db.session.query(model.App).get(app_id)
        if app is None:
            raise NotFound
        if request.args.get('offset'):
            offset = int(request.args.get('offset'))
        else:
            offset = 0
        user_id = None if current_user.is_anonymous() else current_user.id
        user_ip = request.remote_addr if current_user.is_anonymous() else None
        task = sched.new_task(app_id, user_id, user_ip, offset)
        # If there is a task for the user, return it

        if task:
            s = URLSafeSerializer(current_app.config.get('SECRET_KEY'))
            r = make_response(json.dumps(task.dictize()))
            r.mimetype = "application/json"
            cookie_id = 'task_run_for_task_id_%s' % task.id
            r.set_cookie(cookie_id, s.dumps(task.dictize()))
            return r

        else:
            return Response(json.dumps({}), mimetype="application/json")
    except Exception as e:
        return error.format_exception(e, target='app', action='GET')
Beispiel #5
0
def new_task(app_id):
    """Return a new task for an application."""
    # Check if the request has an arg:
    try:
        app = db.session.query(model.App).get(app_id)
        if app is None:
            raise NotFound
        if request.args.get('offset'):
            offset = int(request.args.get('offset'))
        else:
            offset = 0

        # Identify the current user
        fb_user_id = request.args.get('facebook_user_id')
        if (fb_user_id == None):
            user_id = None if current_user.is_anonymous() else current_user.id
        else:
            fb_api = UserFbAPI()
            fb_user = fb_api.get_user_by_fb_id(int(fb_user_id))
            user_id = fb_user.id
        user_ip = request.remote_addr if current_user.is_anonymous(
        ) and fb_user_id == None else None
        task = sched.new_task(app_id, user_id, user_ip, offset)
        # If there is a task for the user, return it
        if task:
            r = make_response(json.dumps(task.dictize()))
            r.mimetype = "application/json"
            return r
        else:
            return Response(json.dumps({}), mimetype="application/json")
    except Exception as e:
        return error.format_exception(e, target='app', action='GET')
Beispiel #6
0
def new_task(app_id):
    """Return a new task for an application."""
    # Check if the request has an arg:
    try:
        app = db.session.query(model.App).get(app_id)
        if app is None:
            raise NotFound
        if request.args.get('offset'):
            offset = int(request.args.get('offset'))
        else:
            offset = 0
            
        # Identify the current user
        fb_user_id = request.args.get('facebook_user_id')
        if (fb_user_id == None):
            user_id = None if current_user.is_anonymous() else current_user.id
        else:
            fb_api = UserFbAPI()
            fb_user = fb_api.get_user_by_fb_id(int(fb_user_id))
            user_id = fb_user.id
        user_ip = request.remote_addr if current_user.is_anonymous() and fb_user_id == None else None    
        task = sched.new_task(app_id, user_id, user_ip, offset)
        # If there is a task for the user, return it
        if task:
            r = make_response(json.dumps(task.dictize()))
            r.mimetype = "application/json"
            return r
        else:
            return Response(json.dumps({}), mimetype="application/json")
    except Exception as e:
        return error.format_exception(e, target='app', action='GET')
Beispiel #7
0
def _retrieve_new_task(project_id):
    project = project_repo.get(project_id)
    if project is None:
        raise NotFound
    if not project.allow_anonymous_contributors and current_user.is_anonymous():
        info = dict(
            error="This project does not allow anonymous contributors")
        error = model.task.Task(info=info)
        return error
    if request.args.get('offset'):
        offset = int(request.args.get('offset'))
    else:
        offset = 0

    user_id = None if current_user.is_anonymous() else current_user.id
    user_ip = request.remote_addr if current_user.is_anonymous() else None

    if request.headers.get('remote_mobile_addr') is not None :
            user_ip = request.headers.get('remote_mobile_addr') if current_user.is_anonymous() else None
    else:
            if request.headers.getlist("X-Forwarded-For"):
                user_ip = request.headers.getlist("X-Forwarded-For")[0]

    if ',' in user_ip:
            ips = user_ip.split(",")
            for ip in ips:
                user_ip = ip

    print "_retrieve_new_task %s." % user_id
    print "_retrieve_new_task %s." % user_ip
    print "project.info.get('sched') %s." % project.info.get('sched')

    task = sched.new_task(project_id, project.info.get('sched'), user_id, user_ip, offset)
    return task
Beispiel #8
0
def _retrieve_new_task(project_id):

    project = project_repo.get(project_id)

    if project is None:
        raise NotFound

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

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

    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')
    task = sched.new_task(project_id,
                          project.info.get('sched'),
                          user_id,
                          user_ip,
                          external_uid,
                          offset,
                          limit,
                          orderby=orderby,
                          desc=desc)
    return task
Beispiel #9
0
def _retrieve_new_task(project_id):

    project = project_repo.get(project_id)

    if project is None:
        raise NotFound

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

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

    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')
    task = sched.new_task(project_id, project.info.get('sched'),
                          user_id,
                          user_ip,
                          external_uid,
                          offset,
                          limit,
                          orderby=orderby,
                          desc=desc)
    return task
Beispiel #10
0
def _retrieve_new_task(app_id):
    app = db.session.query(model.app.App).get(app_id)
    if app is None:
        raise NotFound
    if request.args.get('offset'):
        offset = int(request.args.get('offset'))
    else:
        offset = 0
    user_id = None if current_user.is_anonymous() else current_user.id
    user_ip = request.remote_addr if current_user.is_anonymous() else None
    task = sched.new_task(app_id, user_id, user_ip, offset)
    return task
Beispiel #11
0
def _retrieve_new_task(app_id):
    app = db.session.query(model.app.App).get(app_id)
    if app is None:
        raise NotFound
    if request.args.get('offset'):
        offset = int(request.args.get('offset'))
    else:
        offset = 0
    user_id = None if current_user.is_anonymous() else current_user.id
    user_ip = request.remote_addr if current_user.is_anonymous() else None
    task = sched.new_task(app_id, user_id, user_ip, offset)
    return task
Beispiel #12
0
def new_task(app_id):
    # Check if the request has an arg:
    if request.args.get('offset'):
        offset = int(request.args.get('offset'))
    else:
        offset = 0

    user_id = None if current_user.is_anonymous() else current_user.id
    user_ip = request.remote_addr if current_user.is_anonymous() else None
    task = sched.new_task(app_id, user_id, user_ip, offset)
    # If there is a task for the user, return it
    if task:
        return Response(json.dumps(task.dictize()), mimetype="application/json")
    else:
        return Response(json.dumps({}), mimetype="application/json")
Beispiel #13
0
def _retrieve_new_task(project_id):
    project = project_repo.get(project_id)
    if project is None:
        raise NotFound
    if not project.allow_anonymous_contributors and current_user.is_anonymous():
        info = dict(
            error="This project does not allow anonymous contributors")
        error = model.task.Task(info=info)
        return error
    if request.args.get('offset'):
        offset = int(request.args.get('offset'))
    else:
        offset = 0
    user_id = None if current_user.is_anonymous() else current_user.id
    user_ip = request.remote_addr if current_user.is_anonymous() else None
    task = sched.new_task(project_id, project.info.get('sched'), user_id, user_ip, offset)
    return task
Beispiel #14
0
def _retrieve_new_task(project_id):
    project = project_repo.get(project_id)
    if project is None:
        raise NotFound
    if not project.allow_anonymous_contributors and current_user.is_anonymous():
        info = dict(
            error="This project does not allow anonymous contributors")
        error = model.task.Task(info=info)
        return error
    if request.args.get('offset'):
        offset = int(request.args.get('offset'))
    else:
        offset = 0
    user_id = None if current_user.is_anonymous() else current_user.id
    user_ip = request.remote_addr if current_user.is_anonymous() else None
    task = sched.new_task(project_id, project.info.get('sched'), user_id, user_ip, offset)
    return task
Beispiel #15
0
def new_task(app_id):
    # Check if the request has an arg:
    try:
        app = db.session.query(model.App).get(app_id)
        if app is None:
            raise NotFound
        if request.args.get('offset'):
            offset = int(request.args.get('offset'))
        else:
            offset = 0
        user_id = None if current_user.is_anonymous() else current_user.id
        user_ip = request.remote_addr if current_user.is_anonymous() else None
        task = sched.new_task(app_id, user_id, user_ip, offset)
        # If there is a task for the user, return it
        if task:
            return Response(json.dumps(task.dictize()), mimetype="application/json")
        else:
            return Response(json.dumps({}), mimetype="application/json")
    except Exception as e:
        return error.format_exception(e, target='app', action='GET')
Beispiel #16
0
def new_task(app_id):
    """Return a new task for a project."""
    # Check if the request has an arg:
    try:
        app = db.session.query(model.app.App).get(app_id)
        if app is None:
            raise NotFound
        if request.args.get('offset'):
            offset = int(request.args.get('offset'))
        else:
            offset = 0
        user_id = None if current_user.is_anonymous() else current_user.id
        user_ip = request.remote_addr if current_user.is_anonymous() else None
        task = sched.new_task(app_id, user_id, user_ip, offset)
        # If there is a task for the user, return it
        if task:
            r = make_response(json.dumps(task.dictize()))
            r.mimetype = "application/json"
            return r
        else:
            return Response(json.dumps({}), mimetype="application/json")
    except Exception as e:
        return error.format_exception(e, target='app', action='GET')
Beispiel #17
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
Beispiel #18
0
def _retrieve_new_task(project_id):

    project = project_repo.get(project_id)

    if project is None:
        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

    # 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)
    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)

    handler = partial(pwd_manager.update_response,
                      project=project,
                      user=user_id_or_ip)
    return task, project.info.get('timeout'), handler
Beispiel #19
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