Esempio n. 1
0
def _profile():

    user_id = current_user.id
    form = ProfileForm()
    form.periodicity.choices = [(p.name, p.value) for p in ReportPeriodicity]
    form.password.render_kw = {'placeholder': 'YOUR OLD PASSWORD'}

    if request.method == 'POST' and form.validate_on_submit():
        new_data = get_modified_data(form)
        if new_data is not {}:
            try:
                r = put_request_retry(users_endpoint(), user_id, new_data)
                code = r.status_code
                if code != 204:
                    return abort(code)
            except requests.exceptions.RequestException as err:
                print(err)
                return abort(503)

        new_password = form.password.data
        if new_password:
            current_user.set_password(new_password)
            db.session.commit()

    try:
        r = get_request_retry(users_endpoint(), user_id)

        code = r.status_code
        if code == 200:

            user = r.json()
            form.email.data = user['email']
            form.firstname.data = user['firstname']
            form.lastname.data = user['lastname']
            form.age.data = user['age']
            form.weight.data = user['weight']
            form.max_hr.data = user['max_hr']
            form.rest_hr.data = user['rest_hr']
            form.vo2max.data = user['vo2max']
            form.periodicity.data = user['report_periodicity']

            return render_template("profile.html", form=form)
        else:
            return 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)
Esempio n. 3
0
def _strava_auth():
    code = request.args.get('code')
    client = Client()
    xc = client.exchange_code_for_token
    strava_token = xc(client_id=auth.app.config['STRAVA_CLIENT_ID'],
                      client_secret=auth.app.config['STRAVA_CLIENT_SECRET'],
                      code=code)

    try:
        user_id = current_user.id
        params = {'id': user_id, 'strava_token': strava_token}
        r = put_request_retry(users_endpoint(), user_id, body=params)
        code = r.status_code
        if code == 204:
            current_user.set_strava_authorized()
            db.session.commit()
            return redirect(url_for('home.index'))
        else:
            return abort(400)
    except requests.exceptions.RequestException as err:
        print(err)
        return abort(503)
    return redirect('/')