示例#1
0
def _challenges():

    try:
        user_id = current_user.id
        r = get_request_retry(challenges_endpoint(user_id))

        code = r.status_code
        if code == 200:
            results = r.json()

            for r in results:
                r['start_date'] = timestamp_to_date(r['start_date'])
                run_challenged_id = r['run_challenged_id']
                run_challenger_id = r['run_challenger_id']
                r['run_challenged_name'] = get_run_name(run_challenged_id)
                r['run_challenger_name'] = get_run_name(run_challenger_id)

            return render_template("challenges.html",
                                   results=results,
                                   challenge_id=None)
        else:
            abort(code)
    except requests.exceptions.RequestException as err:
        print(err)
        return abort(503)

    return abort(400)
示例#2
0
def challenge(id):

    try:
        user_id = current_user.id
        r = get_request_retry(challenges_endpoint(user_id), id)

        code = r.status_code
        if code == 200:

            challenged_run = None
            challenger_run = None
            runs = None
            won = False

            result = r.json()

            run_challenged_id = result['run_challenged_id']
            run_challenger_id = result['run_challenger_id']
            won = result['result']

            challenged_run = get_run(run_challenged_id)
            challenger_run = get_run(run_challenger_id)

            if run_challenger_id is None:
                start_date_param = timestamp_to_param(result['start_date'])
                params = {'start-date': start_date_param}
                try:
                    r = get_request_retry(runs_endpoint(user_id),
                                          params=params)

                    code = r.status_code
                    if code == 200:
                        runs = r.json()

                        for run in runs:
                            date = run['start_date']
                            run['start_date'] = timestamp_to_date(date)
                    else:
                        return abort(code)
                except requests.exceptions.RequestException as err:
                    print(err)
                    return abort(503)

            return render_template("challenge.html",
                                   id=id,
                                   challenged_run=challenged_run,
                                   challenger_run=challenger_run,
                                   runs=runs,
                                   won=won)
        else:
            abort(code)
    except requests.exceptions.RequestException as err:
        print(err)
        return abort(503)

    return abort(400)
def challenge_complete(id, challenger_id):

    try:
        user_id = current_user.id
        body = {'run_challenger_id': int(challenger_id)}
        r = put_request_retry(challenges_endpoint(user_id), id, body=body)

        code = r.status_code
        if code == 200:
            return redirect(url_for('challenges.challenge', id=id))
        else:
            return abort(code)
    except requests.exceptions.RequestException as err:
        print(err)
        return abort(503)
示例#4
0
def delete_single_user(user_id):
    q = db.session.query(User).filter(User.id == user_id)
    if q.count() == 0:
        return bad_response(404, 'No user with ID ' + str(user_id))
    u = q.first()
    try:
        request_utils.delete_request_retry(
            request_utils.challenges_endpoint(u.id))
        request_utils.delete_request_retry(
            request_utils.objectives_endpoint(u.id))
    except RequestException:
        return bad_response(400, 'Error removing the user')
    if u.strava_token is not None:
        c = client.Client(access_token=u.strava_token)
        c.deauthorize()
    db.session.delete(q.first())
    db.session.commit()
    return "", 204
示例#5
0
def challenge_create(id):

    try:
        user_id = current_user.id
        params = {'run_challenged_id': int(id)}
        r = post_request_retry(challenges_endpoint(user_id), params=params)

        code = r.status_code
        if code == 200:
            result = r.json()
            challenge_id = result
            return redirect(url_for('challenges.challenge', id=challenge_id))
        else:
            return abort(code)
    except requests.exceptions.RequestException as err:
        print(err)
        return abort(503)

    return abort(400)