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 _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)
def _statistics(): try: user_id = current_user.id r = get_request_retry(statistics_endpoint(user_id)) code = r.status_code if code == 200: results = r.json() distances = check_values(results["distances"]) average_speeds = check_values(results["average_speeds"]) average_heart_rates = check_values(results["average_heart_rates"]) elevation_gains = check_values(results["elevation_gains"]) elapsed_times = check_values(results["elapsed_times"]) run_names = results["run_names"] run_ids = results["run_ids"] run_names = concatenate_run_name_id(run_names, run_ids) return render_template("statistics.html", distances=distances, average_speeds=average_speeds, average_heart_rates=average_heart_rates, elevation_gains=elevation_gains, elapsed_times=elapsed_times, run_names=run_names) except requests.exceptions.RequestException as err: print(err) return abort(503) return abort(404)
def _training_objectives(): user_id = current_user.id setter_form = TrainingObjectiveSetterForm() visualizer_form = TrainingObjectiveVisualizerForm() results = None if request.method == 'POST': if setter_form.validate_on_submit(): start_date = date_to_utc_timestamp(setter_form.start_date.data) end_date = date_to_utc_timestamp(setter_form.end_date.data) km_to_run = setter_form.km_to_run.data params = { 'start_date': start_date, 'end_date': end_date, 'kilometers_to_run': km_to_run } print(params) try: r = post_request_retry(objectives_endpoint(user_id), params=params) code = r.status_code if code != 201: return abort(code) except requests.exceptions.RequestException as err: print(err) return abort(503) try: r = get_request_retry(objectives_endpoint(user_id)) code = r.status_code if code == 200: results = r.json() now_utc = datetime.utcnow() for result in results: end_date_utc = timestamp_to_utc(result['end_date']) result['is_expired'] = end_date_utc < now_utc result['start_date'] = timestamp_to_date(result['start_date']) result['end_date'] = timestamp_to_date(result['end_date']) km_to_run = result['kilometers_to_run'] travelled_kilometers = result['travelled_kilometers'] result['km_left'] = km_to_run - travelled_kilometers else: return abort(code) except requests.exceptions.RequestException as err: print(err) return abort(503) return render_template("training_objectives.html", training_objectives_list=results, setter_form=setter_form, visualizer_form=visualizer_form)
def run(id): try: user_id = current_user.id r = get_request_retry(runs_endpoint(user_id), id) code = r.status_code if code == 200: run = r.json() run_id = run['id'] name = run['title'] timestamp = datetime.fromtimestamp(run['start_date']) date = timestamp.strftime('%A %d/%m/%y at %H:%M') headers = [ 'Distance (m)', 'AVG Speed (m/s)', 'Elapsed Time (s)', 'Elevation (m)' ] values = [ run['distance'], run['average_speed'], run['elapsed_time'], run['total_elevation_gain'] ] return render_template("runs.html", name=name, date=date, headers=headers, values=values, id=run_id) except requests.exceptions.RequestException as err: print(err) return abort(503) return abort(404)
def index(): strava_auth_url = _strava_auth_url(home.app.config) average_speed = None runs = None if current_user is not None and hasattr(current_user, 'id'): user_id = current_user.id # Average Speed try: r = get_request_retry(users_endpoint(user_id), 'average') code = r.status_code if code == 200: result = r.json() average_speed = result['average_speed'] else: return abort(code) except requests.exceptions.RequestException as err: print(err) return abort(503) # Runs try: params = {'page': 0, "per_page": 10} r = get_request_retry(runs_endpoint(user_id), params=params) code = r.status_code if code == 200: runs = r.json() else: return abort(code) except requests.exceptions.RequestException as err: print(err) return abort(503) return render_template('index.html', strava_auth_url=strava_auth_url, average_speed=average_speed, runs=runs)
def get_run(run_id): if run_id is not None: user_id = current_user.id try: r = get_request_retry(runs_endpoint(user_id), run_id) code = r.status_code if code == 200: result = r.json() result['start_date'] = timestamp_to_date(result['start_date']) return result except requests.exceptions.RequestException as err: print(err) return None
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 update_distance(training_objectives, runner_id): lastRunId = db.session.query(Last_Run.lastRunId).filter( Last_Run.runner_id == runner_id).first().lastRunId #we take the id of the last fetched run dict_to = {} user = db.session.query(Last_Run).filter( Last_Run.runner_id == runner_id).first() maxRunId = user.lastRunId if user.lastRunId is not None else -1 for to in training_objectives: id_ = to.id start_date = to.start_date end_date = to.end_date travelled_kilometers = to.travelled_kilometers params = { "start-date": str(start_date).replace(" ", "T") + "Z", "finish-date": str(end_date).replace(" ", "T") + "Z" } if lastRunId is not None: params["from-id"] = str(lastRunId) try: runs_response = get_request_retry( runs_endpoint(runner_id), params=params) #request to data service except requests.exceptions.RequestException as ex: abort(503, str(ex)) list_of_runs = runs_response.json() status_code = runs_response.status_code if status_code != 200: abort(status_code, runs_response.json().get('message')) partial_sum = 0 for run in list_of_runs: partial_sum += run['distance'] if run['id'] > maxRunId: #here we pick the max id of the runs maxRunId = run['id'] partial_sum /= 1000 travelled_kilometers += partial_sum dict_to[id_] = travelled_kilometers for to in training_objectives: to.travelled_kilometers = dict_to[to.id] user.lastRunId = maxRunId #here we update the lastRunId of the user (lastRunId is the id of the last fetched run by the user) db.session.commit()
def check_runner_id(runner_id, send_get=True): if int(runner_id) <= 0: abort(400, 'Invalid runner_id') if send_get: if db.session.query(Last_Run).filter( Last_Run.runner_id == runner_id).count() != 0: return try: response = get_request_retry(users_endpoint(runner_id)) status_code = response.status_code except requests.exceptions.RequestException as ex: abort(503, str(ex)) if status_code != 200: abort(status_code, response.json().get('message')) db_last_run = Last_Run() db_last_run.runner_id = runner_id db.session.add(db_last_run) db.session.commit()
def get_single_run(runner_id, run_id): r = get_request_retry(runs_endpoint(runner_id), run_id) result = r.json() return result
def check_user(runner_id): r = get_request_retry(users_endpoint(), runner_id) result = r.json() return 'id' in result