From 5e023a0e075de5abe0df5e4cada5a8ad636b6281 Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Sun, 12 Jul 2020 10:11:52 +0200 Subject: [PATCH] gets strava owner id thru api --- rowers/models.py | 1 + rowers/stravastuff.py | 25 ++++++++++++++++++++++++ rowers/tests/mocks.py | 10 ++++++++++ rowers/tests/testdata/strava_athlete.txt | 1 + 4 files changed, 37 insertions(+) create mode 100644 rowers/tests/testdata/strava_athlete.txt diff --git a/rowers/models.py b/rowers/models.py index 2a317423..b676f4a7 100644 --- a/rowers/models.py +++ b/rowers/models.py @@ -849,6 +849,7 @@ class Rower(models.Model): max_length=30, choices=stravatypes, verbose_name="Export Workouts to Strava as") + strava_owner_id = models.BigIntegerField(default=0) strava_auto_export = models.BooleanField(default=False) strava_auto_import = models.BooleanField(default=False) diff --git a/rowers/stravastuff.py b/rowers/stravastuff.py index f0cbf6e2..36097d9c 100644 --- a/rowers/stravastuff.py +++ b/rowers/stravastuff.py @@ -71,6 +71,8 @@ def get_token(code): return imports_get_token(code, oauth_data) def strava_open(user): + if user.rower.strava_owner_id == 0: + strava_owner_id = set_strava_athlete_id(user) return imports_open(user, oauth_data) def do_refresh_token(refreshtoken): @@ -95,6 +97,29 @@ def rower_strava_token_refresh(user): def make_authorization_url(request): return imports_make_authorization_url(oauth_data) +def set_strava_athlete_id(user): + r = Rower.objects.get(user=user) + if (r.stravatoken == '') or (r.stravatoken is None): + s = "Token doesn't exist. Need to authorize" + return custom_exception_handler(401,s) + elif (r.stravatokenexpirydate is None or timezone.now()+timedelta(seconds=3599)>r.stravatokenexpirydate): + s = "Token expired. Needs to refresh." + return custom_exception_handler(401,s) + else: + authorizationstring = str('Bearer ' + r.stravatoken) + headers = {'Authorization': authorizationstring, + 'user-agent': 'sanderroosendaal', + 'Content-Type': 'application/json'} + url = "https://www.strava.com/api/v3/athlete" + + response = requests.get(url,headers=headers,params={}) + + r.strava_owner_id = response.json()['id'] + r.save() + + return response.json()['id'] + + # Get list of workouts available on Strava def get_strava_workout_list(user,limit_n=0): r = Rower.objects.get(user=user) diff --git a/rowers/tests/mocks.py b/rowers/tests/mocks.py index 5fb400a5..08e90110 100644 --- a/rowers/tests/mocks.py +++ b/rowers/tests/mocks.py @@ -606,6 +606,8 @@ def mocked_requests(*args, **kwargs): uastrokesjson = json.load(open('rowers/tests/testdata/uastrokes.txt','r')) uauserjson = json.load(open('rowers/tests/testdata/uauser.txt','r')) + stravaathletejson = json.load(open('rowers/tests/testdata/strava_athlete.txt')) + class MockResponse: def __init__(self, json_data, status_code): self.json_data = json_data @@ -660,6 +662,10 @@ def mocked_requests(*args, **kwargs): c2workoutlistregex = '.*?concept2\.com\/api\/users\/me\/results\?page=\d' c2workoutlisttester = re.compile(c2workoutlistregex) + stravaathleteregex = '.*?strava\.com\/api\/v3\/athlete$' + stravaathletetester = re.compile(stravaathleteregex) + + stravaworkoutlistregex = '.*?strava\.com\/api\/v3\/athlete\/activities' stravaworkoutlisttester = re.compile(stravaworkoutlistregex) @@ -703,6 +709,10 @@ def mocked_requests(*args, **kwargs): tpuploadregex = '.*?trainingpeaks\.com\/v1\/file' tpuploadtester = re.compile(tpuploadregex) + if stravaathletetester.match(args[0]): + json_data = stravaathletejson + return MockResponse(json_data,200) + if polartester.match(args[0]): json_data = polar_json return MockResponse(json_data,200) diff --git a/rowers/tests/testdata/strava_athlete.txt b/rowers/tests/testdata/strava_athlete.txt new file mode 100644 index 00000000..0108efef --- /dev/null +++ b/rowers/tests/testdata/strava_athlete.txt @@ -0,0 +1 @@ +{"id": 47155909, "username": null, "resource_state": 2, "firstname": "Rowsandall", "lastname": "Testing", "city": "Zliv", "state": "Jiho\u010desk\u00fd kraj", "country": "Czechia", "sex": "M", "premium": false, "summit": false, "created_at": "2019-10-06T06:59:54Z", "updated_at": "2020-05-15T11:52:33Z", "badge_type_id": 0, "profile_medium": "avatar/athlete/medium.png", "profile": "avatar/athlete/large.png", "friend": null, "follower": null} \ No newline at end of file