gets strava owner id thru api
This commit is contained in:
@@ -849,6 +849,7 @@ class Rower(models.Model):
|
|||||||
max_length=30,
|
max_length=30,
|
||||||
choices=stravatypes,
|
choices=stravatypes,
|
||||||
verbose_name="Export Workouts to Strava as")
|
verbose_name="Export Workouts to Strava as")
|
||||||
|
strava_owner_id = models.BigIntegerField(default=0)
|
||||||
|
|
||||||
strava_auto_export = models.BooleanField(default=False)
|
strava_auto_export = models.BooleanField(default=False)
|
||||||
strava_auto_import = models.BooleanField(default=False)
|
strava_auto_import = models.BooleanField(default=False)
|
||||||
|
|||||||
@@ -71,6 +71,8 @@ def get_token(code):
|
|||||||
return imports_get_token(code, oauth_data)
|
return imports_get_token(code, oauth_data)
|
||||||
|
|
||||||
def strava_open(user):
|
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)
|
return imports_open(user, oauth_data)
|
||||||
|
|
||||||
def do_refresh_token(refreshtoken):
|
def do_refresh_token(refreshtoken):
|
||||||
@@ -95,6 +97,29 @@ def rower_strava_token_refresh(user):
|
|||||||
def make_authorization_url(request):
|
def make_authorization_url(request):
|
||||||
return imports_make_authorization_url(oauth_data)
|
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
|
# Get list of workouts available on Strava
|
||||||
def get_strava_workout_list(user,limit_n=0):
|
def get_strava_workout_list(user,limit_n=0):
|
||||||
r = Rower.objects.get(user=user)
|
r = Rower.objects.get(user=user)
|
||||||
|
|||||||
@@ -606,6 +606,8 @@ def mocked_requests(*args, **kwargs):
|
|||||||
uastrokesjson = json.load(open('rowers/tests/testdata/uastrokes.txt','r'))
|
uastrokesjson = json.load(open('rowers/tests/testdata/uastrokes.txt','r'))
|
||||||
uauserjson = json.load(open('rowers/tests/testdata/uauser.txt','r'))
|
uauserjson = json.load(open('rowers/tests/testdata/uauser.txt','r'))
|
||||||
|
|
||||||
|
stravaathletejson = json.load(open('rowers/tests/testdata/strava_athlete.txt'))
|
||||||
|
|
||||||
class MockResponse:
|
class MockResponse:
|
||||||
def __init__(self, json_data, status_code):
|
def __init__(self, json_data, status_code):
|
||||||
self.json_data = json_data
|
self.json_data = json_data
|
||||||
@@ -660,6 +662,10 @@ def mocked_requests(*args, **kwargs):
|
|||||||
c2workoutlistregex = '.*?concept2\.com\/api\/users\/me\/results\?page=\d'
|
c2workoutlistregex = '.*?concept2\.com\/api\/users\/me\/results\?page=\d'
|
||||||
c2workoutlisttester = re.compile(c2workoutlistregex)
|
c2workoutlisttester = re.compile(c2workoutlistregex)
|
||||||
|
|
||||||
|
stravaathleteregex = '.*?strava\.com\/api\/v3\/athlete$'
|
||||||
|
stravaathletetester = re.compile(stravaathleteregex)
|
||||||
|
|
||||||
|
|
||||||
stravaworkoutlistregex = '.*?strava\.com\/api\/v3\/athlete\/activities'
|
stravaworkoutlistregex = '.*?strava\.com\/api\/v3\/athlete\/activities'
|
||||||
stravaworkoutlisttester = re.compile(stravaworkoutlistregex)
|
stravaworkoutlisttester = re.compile(stravaworkoutlistregex)
|
||||||
|
|
||||||
@@ -703,6 +709,10 @@ def mocked_requests(*args, **kwargs):
|
|||||||
tpuploadregex = '.*?trainingpeaks\.com\/v1\/file'
|
tpuploadregex = '.*?trainingpeaks\.com\/v1\/file'
|
||||||
tpuploadtester = re.compile(tpuploadregex)
|
tpuploadtester = re.compile(tpuploadregex)
|
||||||
|
|
||||||
|
if stravaathletetester.match(args[0]):
|
||||||
|
json_data = stravaathletejson
|
||||||
|
return MockResponse(json_data,200)
|
||||||
|
|
||||||
if polartester.match(args[0]):
|
if polartester.match(args[0]):
|
||||||
json_data = polar_json
|
json_data = polar_json
|
||||||
return MockResponse(json_data,200)
|
return MockResponse(json_data,200)
|
||||||
|
|||||||
1
rowers/tests/testdata/strava_athlete.txt
vendored
Normal file
1
rowers/tests/testdata/strava_athlete.txt
vendored
Normal file
@@ -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}
|
||||||
Reference in New Issue
Block a user