Merge branch 'feature/stravaauth' into develop
This commit is contained in:
+2
-1
@@ -44,7 +44,8 @@ class RowerInline(admin.StackedInline):
|
|||||||
'polartoken','polartokenexpirydate',
|
'polartoken','polartokenexpirydate',
|
||||||
'polarrefreshtoken','polaruserid',
|
'polarrefreshtoken','polaruserid',
|
||||||
'polar_auto_import',
|
'polar_auto_import',
|
||||||
'stravatoken','stravaexportas','strava_auto_export',
|
'stravatoken','stravatokenexpirydate','stravarefreshtoken',
|
||||||
|
'stravaexportas','strava_auto_export',
|
||||||
'strava_auto_import',
|
'strava_auto_import',
|
||||||
'runkeepertoken','runkeeper_auto_export',)}),
|
'runkeepertoken','runkeeper_auto_export',)}),
|
||||||
('Team',
|
('Team',
|
||||||
|
|||||||
+19
-1
@@ -94,7 +94,7 @@ def imports_open(user,oauth_data):
|
|||||||
tokenname = oauth_data['tokenname']
|
tokenname = oauth_data['tokenname']
|
||||||
refreshtokenname = oauth_data['refreshtokenname']
|
refreshtokenname = oauth_data['refreshtokenname']
|
||||||
expirydatename = oauth_data['expirydatename']
|
expirydatename = oauth_data['expirydatename']
|
||||||
if tokenexpirydate and timezone.now()>tokenexpirydate:
|
if tokenexpirydate and timezone.now()+timedelta(seconds=3599)>tokenexpirydate:
|
||||||
token = imports_token_refresh(
|
token = imports_token_refresh(
|
||||||
user,
|
user,
|
||||||
tokenname,
|
tokenname,
|
||||||
@@ -102,6 +102,15 @@ def imports_open(user,oauth_data):
|
|||||||
expirydatename,
|
expirydatename,
|
||||||
oauth_data,
|
oauth_data,
|
||||||
)
|
)
|
||||||
|
elif tokenexpirydate is None and expirydatename is not None and 'strava' in expirydatename:
|
||||||
|
token = imports_token_refresh(
|
||||||
|
user,
|
||||||
|
tokenname,
|
||||||
|
refreshtokenname,
|
||||||
|
expirydatename,
|
||||||
|
oauth_data,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
return token
|
return token
|
||||||
|
|
||||||
@@ -155,6 +164,10 @@ def imports_do_refresh_token(refreshtoken,oauth_data,access_token=''):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
expires_in = token_json['expires_in']
|
expires_in = token_json['expires_in']
|
||||||
|
except KeyError:
|
||||||
|
try:
|
||||||
|
expires_at = arrow.get(token_json['expires_at']).timestamp
|
||||||
|
expires_in = expires_at - arrow.now().timestamp
|
||||||
except KeyError:
|
except KeyError:
|
||||||
expires_in = 0
|
expires_in = 0
|
||||||
try:
|
try:
|
||||||
@@ -267,6 +280,11 @@ def imports_token_refresh(user,tokenname,refreshtokenname,expirydatename,oauth_d
|
|||||||
|
|
||||||
refreshtoken = getattr(r,refreshtokenname)
|
refreshtoken = getattr(r,refreshtokenname)
|
||||||
|
|
||||||
|
# for Strava transition
|
||||||
|
if not refreshtoken:
|
||||||
|
refreshtoken = getattr(r,tokenname)
|
||||||
|
|
||||||
|
|
||||||
res = imports_do_refresh_token(refreshtoken,oauth_data)
|
res = imports_do_refresh_token(refreshtoken,oauth_data)
|
||||||
access_token = res[0]
|
access_token = res[0]
|
||||||
expires_in = res[1]
|
expires_in = res[1]
|
||||||
|
|||||||
@@ -653,6 +653,9 @@ class Rower(models.Model):
|
|||||||
polar_auto_import = models.BooleanField(default=False)
|
polar_auto_import = models.BooleanField(default=False)
|
||||||
|
|
||||||
stravatoken = models.CharField(default='',max_length=200,blank=True,null=True)
|
stravatoken = models.CharField(default='',max_length=200,blank=True,null=True)
|
||||||
|
stravatokenexpirydate = models.DateTimeField(blank=True,null=True)
|
||||||
|
stravarefreshtoken = models.CharField(default='',max_length=1000,
|
||||||
|
blank=True,null=True)
|
||||||
stravaexportas = models.CharField(default="Rowing",
|
stravaexportas = models.CharField(default="Rowing",
|
||||||
max_length=30,
|
max_length=30,
|
||||||
choices=stravatypes,
|
choices=stravatypes,
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ otwtypes = (
|
|||||||
'churchboat'
|
'churchboat'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
checktypes = [i[0] for i in workouttypes]
|
checktypes = [i[0] for i in workouttypes]
|
||||||
|
|
||||||
workoutsources = (
|
workoutsources = (
|
||||||
|
|||||||
+36
-5
@@ -40,11 +40,11 @@ oauth_data = {
|
|||||||
'autorization_uri': "https://www.strava.com/oauth/authorize",
|
'autorization_uri': "https://www.strava.com/oauth/authorize",
|
||||||
'content_type': 'application/json',
|
'content_type': 'application/json',
|
||||||
'tokenname': 'stravatoken',
|
'tokenname': 'stravatoken',
|
||||||
'refreshtokenname': '',
|
'refreshtokenname': 'stravarefreshtoken',
|
||||||
'expirydatename': '',
|
'expirydatename': 'stravatokenexpirydate',
|
||||||
'bearer_auth': True,
|
'bearer_auth': True,
|
||||||
'base_url': "https://www.strava.com/oauth/token",
|
'base_url': "https://www.strava.com/oauth/token",
|
||||||
'grant_type': None,
|
'grant_type': 'refresh_token',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -52,6 +52,27 @@ oauth_data = {
|
|||||||
def get_token(code):
|
def get_token(code):
|
||||||
return imports_get_token(code, oauth_data)
|
return imports_get_token(code, oauth_data)
|
||||||
|
|
||||||
|
def strava_open(user):
|
||||||
|
return imports_open(user, oauth_data)
|
||||||
|
|
||||||
|
def do_refresh_token(refreshtoken):
|
||||||
|
return imports_do_refresh_token(refreshtoken, oauth_data)
|
||||||
|
|
||||||
|
def rower_strava_token_refresh(user):
|
||||||
|
r = Rower.objects.get(user=user)
|
||||||
|
res = do_refresh_token(r.stravarefreshtoken)
|
||||||
|
access_token = res[0]
|
||||||
|
expires_in = res[1]
|
||||||
|
refresh_token = res[2]
|
||||||
|
expirydatetime = timezone.now()+timedelta(seconds=expires_in)
|
||||||
|
|
||||||
|
r.stravatoken = access_token
|
||||||
|
r.stravatokenexpirydate = expirydatetime
|
||||||
|
r.stravarefreshtoken = refresh_token
|
||||||
|
r.save()
|
||||||
|
|
||||||
|
return r.stravatoken
|
||||||
|
|
||||||
# Make authorization URL including random string
|
# Make authorization URL including random string
|
||||||
def make_authorization_url(request):
|
def make_authorization_url(request):
|
||||||
return imports_make_authorization_url(oauth_data)
|
return imports_make_authorization_url(oauth_data)
|
||||||
@@ -62,6 +83,9 @@ def get_strava_workout_list(user,limit_n=0):
|
|||||||
if (r.stravatoken == '') or (r.stravatoken is None):
|
if (r.stravatoken == '') or (r.stravatoken is None):
|
||||||
s = "Token doesn't exist. Need to authorize"
|
s = "Token doesn't exist. Need to authorize"
|
||||||
return custom_exception_handler(401,s)
|
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:
|
else:
|
||||||
# ready to fetch. Hurray
|
# ready to fetch. Hurray
|
||||||
authorizationstring = str('Bearer ' + r.stravatoken)
|
authorizationstring = str('Bearer ' + r.stravatoken)
|
||||||
@@ -78,6 +102,7 @@ def get_strava_workout_list(user,limit_n=0):
|
|||||||
|
|
||||||
s = requests.get(url,headers=headers,params=params)
|
s = requests.get(url,headers=headers,params=params)
|
||||||
|
|
||||||
|
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
@@ -87,9 +112,12 @@ def get_strava_workouts(rower):
|
|||||||
if not isprorower(rower):
|
if not isprorower(rower):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
res = get_strava_workout_list(rower.user,limit_n=10)
|
try:
|
||||||
|
thetoken = strava_open(rower.user)
|
||||||
|
except NoTokenError:
|
||||||
|
return 0
|
||||||
|
|
||||||
print res.status_code
|
res = get_strava_workout_list(rower.user,limit_n=10)
|
||||||
|
|
||||||
if (res.status_code != 200):
|
if (res.status_code != 200):
|
||||||
return 0
|
return 0
|
||||||
@@ -227,6 +255,9 @@ def get_workout(user,stravaid):
|
|||||||
if (r.stravatoken == '') or (r.stravatoken is None):
|
if (r.stravatoken == '') or (r.stravatoken is None):
|
||||||
s = "Token doesn't exist. Need to authorize"
|
s = "Token doesn't exist. Need to authorize"
|
||||||
return custom_exception_handler(401,s)
|
return custom_exception_handler(401,s)
|
||||||
|
elif (r.stravatokenexpirydate is not None and timezone.now()>r.stravatokenexpirydate):
|
||||||
|
s = "Token expired. Needs to refresh."
|
||||||
|
return custom_exception_handler(401,s)
|
||||||
else:
|
else:
|
||||||
# ready to fetch. Hurray
|
# ready to fetch. Hurray
|
||||||
fetchresolution = 'high'
|
fetchresolution = 'high'
|
||||||
|
|||||||
@@ -321,6 +321,15 @@ def mocked_requests(*args, **kwargs):
|
|||||||
return MockResponse(json_data,200)
|
return MockResponse(json_data,200)
|
||||||
elif stravasummarytester.match(args[0]):
|
elif stravasummarytester.match(args[0]):
|
||||||
return MockResponse(stravasummaryjson,200)
|
return MockResponse(stravasummaryjson,200)
|
||||||
|
elif 'token' in args[0]:
|
||||||
|
json_data = {
|
||||||
|
"token_type": "Bearer",
|
||||||
|
"access_token": "987654321234567898765432123456789",
|
||||||
|
"refresh_token": "1234567898765432112345678987654321",
|
||||||
|
"expires_at": 1531385304
|
||||||
|
}
|
||||||
|
return MockResponse(json_data,200)
|
||||||
|
|
||||||
|
|
||||||
if c2tester.match(args[0]):
|
if c2tester.match(args[0]):
|
||||||
if c2uploadtester.match(args[0]):
|
if c2uploadtester.match(args[0]):
|
||||||
|
|||||||
+15
-1
@@ -116,6 +116,7 @@ from sporttracksstuff import sporttracks_open
|
|||||||
from tpstuff import tp_open
|
from tpstuff import tp_open
|
||||||
from iso8601 import ParseError
|
from iso8601 import ParseError
|
||||||
import stravastuff
|
import stravastuff
|
||||||
|
from stravastuff import strava_open
|
||||||
import polarstuff
|
import polarstuff
|
||||||
import sporttracksstuff
|
import sporttracksstuff
|
||||||
import underarmourstuff
|
import underarmourstuff
|
||||||
@@ -2421,15 +2422,22 @@ def rower_process_stravacallback(request):
|
|||||||
|
|
||||||
if res[0]:
|
if res[0]:
|
||||||
access_token = res[0]
|
access_token = res[0]
|
||||||
|
expires_in = res[1]
|
||||||
|
refresh_token = res[2]
|
||||||
|
|
||||||
|
expirydatetime = timezone.now()+datetime.timedelta(seconds=expires_in)
|
||||||
|
|
||||||
r = getrower(request.user)
|
r = getrower(request.user)
|
||||||
r.stravatoken = access_token
|
r.stravatoken = access_token
|
||||||
|
r.stravatokenexpirydate = expirydatetime
|
||||||
|
r.stravarefreshtoken = refresh_token
|
||||||
|
|
||||||
r.save()
|
r.save()
|
||||||
|
|
||||||
successmessage = "Tokens stored. Good to go"
|
successmessage = "Tokens stored. Good to go"
|
||||||
messages.info(request,successmessage)
|
messages.info(request,successmessage)
|
||||||
return imports_view(request)
|
url = reverse(workouts_view)
|
||||||
|
return HttpResponseRedirect(url)
|
||||||
else:
|
else:
|
||||||
message = "Something went wrong with the Strava authorization"
|
message = "Something went wrong with the Strava authorization"
|
||||||
messages.error(request,message)
|
messages.error(request,message)
|
||||||
@@ -10268,6 +10276,11 @@ def workout_add_chart_view(request,id,plotnr=1):
|
|||||||
# The page where you select which Strava workout to import
|
# The page where you select which Strava workout to import
|
||||||
@login_required()
|
@login_required()
|
||||||
def workout_stravaimport_view(request,message="",userid=0):
|
def workout_stravaimport_view(request,message="",userid=0):
|
||||||
|
try:
|
||||||
|
thetoken = strava_open(request.user)
|
||||||
|
except NoTokenError:
|
||||||
|
return HttpResponseRedirect("/rowers/me/stravaauthorize/")
|
||||||
|
|
||||||
res = stravastuff.get_strava_workout_list(request.user)
|
res = stravastuff.get_strava_workout_list(request.user)
|
||||||
|
|
||||||
r = getrequestrower(request,userid=userid)
|
r = getrequestrower(request,userid=userid)
|
||||||
@@ -10277,6 +10290,7 @@ def workout_stravaimport_view(request,message="",userid=0):
|
|||||||
|
|
||||||
r = getrower(request.user)
|
r = getrower(request.user)
|
||||||
|
|
||||||
|
|
||||||
if (res.status_code != 200):
|
if (res.status_code != 200):
|
||||||
if (res.status_code == 401):
|
if (res.status_code == 401):
|
||||||
r = getrower(request.user)
|
r = getrower(request.user)
|
||||||
|
|||||||
Reference in New Issue
Block a user