Strava, ST, RK done
This commit is contained in:
272
rowers/views.py
272
rowers/views.py
@@ -51,12 +51,13 @@ import os,sys
|
||||
import datetime
|
||||
import iso8601
|
||||
import c2stuff
|
||||
from c2stuff import C2NoTokenError
|
||||
from runkeeperstuff import RunKeeperNoTokenError
|
||||
from sporttracksstuff import SportTracksNoTokenError
|
||||
from c2stuff import C2NoTokenError,c2_open
|
||||
from runkeeperstuff import RunKeeperNoTokenError,runkeeper_open
|
||||
from sporttracksstuff import SportTracksNoTokenError,sporttracks_open
|
||||
from tpstuff import TPNoTokenError
|
||||
from iso8601 import ParseError
|
||||
import stravastuff
|
||||
from stravastuff import StravaNoTokenError
|
||||
import sporttracksstuff
|
||||
import underarmourstuff
|
||||
import tpstuff
|
||||
@@ -261,6 +262,7 @@ def splitstdata(lijst):
|
||||
return [np.array(t),np.array(latlong)]
|
||||
|
||||
from utils import geo_distance,serialize_list,deserialize_list
|
||||
from rowers.models import checkworkoutuser
|
||||
|
||||
# Check if a user is a Coach member
|
||||
def iscoachmember(user):
|
||||
@@ -369,14 +371,6 @@ def sendmail(request):
|
||||
else:
|
||||
return HttpResponseRedirect('/rowers/email/')
|
||||
|
||||
# Check if workout belongs to this user
|
||||
def checkworkoutuser(user,workout):
|
||||
try:
|
||||
r = Rower.objects.get(user=user)
|
||||
managers = [team.manager for team in workout.team.all()]
|
||||
return (workout.user == r or user in managers)
|
||||
except Rower.DoesNotExist:
|
||||
return(False)
|
||||
|
||||
# Create workout data from Strava or Concept2
|
||||
# data and create the associated Workout object and save it
|
||||
@@ -1039,38 +1033,7 @@ def add_workout_from_underarmourdata(user,importid,data):
|
||||
|
||||
return (id,message)
|
||||
|
||||
# Checks if user has Concept2 tokens, resets tokens if they are
|
||||
# expired.
|
||||
def c2_open(user):
|
||||
r = Rower.objects.get(user=user)
|
||||
if (r.c2token == '') or (r.c2token is None):
|
||||
s = "Token doesn't exist. Need to authorize"
|
||||
raise C2NoTokenError("User has no token")
|
||||
else:
|
||||
if (timezone.now()>r.tokenexpirydate):
|
||||
res = c2stuff.rower_c2_token_refresh(user)
|
||||
if res[0] != None:
|
||||
thetoken = res[0]
|
||||
else:
|
||||
raise C2NoTokenError("User has no token")
|
||||
else:
|
||||
thetoken = r.c2token
|
||||
|
||||
return thetoken
|
||||
|
||||
# Checks if user has SportTracks token, renews them if they are expired
|
||||
def sporttracks_open(user):
|
||||
r = Rower.objects.get(user=user)
|
||||
if (r.sporttrackstoken == '') or (r.sporttrackstoken is None):
|
||||
s = "Token doesn't exist. Need to authorize"
|
||||
raise SportTracksNoTokenError("User has no token")
|
||||
else:
|
||||
if (timezone.now()>r.sporttrackstokenexpirydate):
|
||||
thetoken = sporttracksstuff.rower_sporttracks_token_refresh(user)
|
||||
else:
|
||||
thetoken = r.sporttrackstoken
|
||||
|
||||
return thetoken
|
||||
|
||||
# Checks if user has UnderArmour token, renews them if they are expired
|
||||
def underarmour_open(user):
|
||||
@@ -1109,16 +1072,6 @@ def tp_open(user):
|
||||
|
||||
return thetoken
|
||||
|
||||
# Checks if user has SportTracks token, renews them if they are expired
|
||||
def runkeeper_open(user):
|
||||
r = Rower.objects.get(user=user)
|
||||
if (r.runkeepertoken == '') or (r.runkeepertoken is None):
|
||||
s = "Token doesn't exist. Need to authorize"
|
||||
raise RunKeeperNoTokenError("User has no token")
|
||||
else:
|
||||
thetoken = r.runkeepertoken
|
||||
|
||||
return thetoken
|
||||
|
||||
# Export workout to TCX and send to user's email address
|
||||
@login_required()
|
||||
@@ -1395,76 +1348,20 @@ def workout_c2_upload_view(request,id=0):
|
||||
raise Http404("Workout doesn't exist")
|
||||
|
||||
try:
|
||||
thetoken = c2_open(r.user)
|
||||
message,c2id = c2stuff.workout_c2_upload(request.user,w)
|
||||
except C2NoTokenError:
|
||||
return HttpResponseRedirect("/rowers/me/c2authorize/")
|
||||
return HttpResponseRedirect("/rowers/me/c2authorize/")
|
||||
|
||||
if (checkworkoutuser(request.user,w)):
|
||||
c2userid = c2stuff.get_userid(thetoken)
|
||||
if not c2userid:
|
||||
return HttpResponseRedirect("/rowers/me/c2authorize")
|
||||
|
||||
data = c2stuff.createc2workoutdata(w)
|
||||
if data == 0:
|
||||
message = "Error: No data file. Contact info@rowsandall.com if this problem persists"
|
||||
url = reverse(workout_export_view,
|
||||
kwargs = {
|
||||
'message':str(message),
|
||||
'id':str(w.id),
|
||||
})
|
||||
|
||||
return HttpResponseRedirect(url)
|
||||
|
||||
authorizationstring = str('Bearer ' + thetoken)
|
||||
headers = {'Authorization': authorizationstring,
|
||||
'user-agent': 'sanderroosendaal',
|
||||
'Content-Type': 'application/json'}
|
||||
try:
|
||||
url = "https://log.concept2.com/api/users/%s/results" % (c2userid)
|
||||
response = requests.post(url,headers=headers,data=json.dumps(data))
|
||||
except:
|
||||
message = "Unexpected Error: "+str(sys.exc_info()[0])
|
||||
with open("media/c2errors.log","a") as errorlog:
|
||||
errorstring = str(sys.exc_info()[0])
|
||||
timestr = strftime("%Y%m%d-%H%M%S")
|
||||
errorlog.write(timestr+errorstring+"\r\n")
|
||||
|
||||
# check for duplicate error first
|
||||
if (response.status_code == 409 ):
|
||||
message = "Duplicate error"
|
||||
w.uploadedtoc2 = -1
|
||||
w.save()
|
||||
elif (response.status_code == 201 or response.status_code == 200):
|
||||
try:
|
||||
s= json.loads(response.text)
|
||||
c2id = s['data']['id']
|
||||
w.uploadedtoc2 = c2id
|
||||
w.save()
|
||||
url = "/rowers/workout/"+str(w.id)+"/export"
|
||||
return HttpResponseRedirect(url)
|
||||
except:
|
||||
message = "Something went wrong in workout_c2_upload_view. Response code 200/201 but C2 sync failed: "+response.text
|
||||
with open("media/c2errors.log","a") as errorlog:
|
||||
errorstring = str(sys.exc_info()[0])
|
||||
timestr = strftime("%Y%m%d-%H%M%S")
|
||||
errorlog.write(timestr+errorstring+"\r\n")
|
||||
|
||||
|
||||
else:
|
||||
s = response
|
||||
message = "Something went wrong in workout_c2_upload_view. C2 sync failed."
|
||||
with open("media/c2errors.log","a") as errorlog:
|
||||
errorstring = str(sys.exc_info()[0])
|
||||
timestr = strftime("%Y%m%d-%H%M%S")
|
||||
errorlog.write(timestr+errorstring+"\r\n")
|
||||
|
||||
if message:
|
||||
url = reverse(workout_export_view,
|
||||
kwargs = {
|
||||
'message':str(message),
|
||||
'id':str(w.id),
|
||||
})
|
||||
else:
|
||||
message = "You are not authorized to upload this workout"
|
||||
|
||||
url = reverse(workout_export_view,
|
||||
kwargs = {
|
||||
'message':str(message),
|
||||
'id':str(w.id),
|
||||
url = reverse(workout_export_view,
|
||||
kwargs = {
|
||||
'id':str(w.id),
|
||||
})
|
||||
|
||||
return HttpResponseRedirect(url)
|
||||
@@ -4604,7 +4501,7 @@ def workout_otwpowerplot_view(request,id=0,message="",successmessage=""):
|
||||
'the_div':div,
|
||||
'mayedit':mayedit})
|
||||
|
||||
# the page where you can chose where to export this workout
|
||||
# the page where you can choose where to export this workout
|
||||
@login_required()
|
||||
def workout_export_view(request,id=0, message="", successmessage=""):
|
||||
request.session[translation.LANGUAGE_SESSION_KEY] = USER_LANGUAGE
|
||||
@@ -5610,6 +5507,8 @@ def workout_getrunkeeperworkout_view(request,runkeeperid):
|
||||
id,message = add_workout_from_runkeeperdata(request.user,runkeeperid,data)
|
||||
w = Workout.objects.get(id=id)
|
||||
w.uploadedtorunkeeper=runkeeperid
|
||||
thetoken = runkeeper_open(request.user)
|
||||
w.runkeeperuri = runkeeperstuff.geturifromid(thetoken,runkeeperid)
|
||||
w.save()
|
||||
if message:
|
||||
url = reverse(workout_edit_view,
|
||||
@@ -5820,6 +5719,31 @@ def workout_upload_view(request,message="",
|
||||
except KeyError:
|
||||
upload_toc2 = False
|
||||
|
||||
try:
|
||||
upload_tostrava = uploadoptions['upload_to_Strava']
|
||||
except KeyError:
|
||||
upload_tostrava = False
|
||||
|
||||
try:
|
||||
upload_tost = uploadoptions['upload_to_SportTracks']
|
||||
except KeyError:
|
||||
upload_tost = False
|
||||
|
||||
try:
|
||||
upload_tork = uploadoptions['upload_to_RunKeeper']
|
||||
except KeyError:
|
||||
upload_tork = False
|
||||
|
||||
try:
|
||||
upload_toua = uploadoptions['upload_to_MapMyFitness']
|
||||
except KeyError:
|
||||
upload_toua = False
|
||||
|
||||
try:
|
||||
upload_totp = uploadoptions['upload_to_TrainingPeaks']
|
||||
except KeyError:
|
||||
upload_totp = False
|
||||
|
||||
r = Rower.objects.get(user=request.user)
|
||||
if request.method == 'POST':
|
||||
form = DocumentsForm(request.POST,request.FILES)
|
||||
@@ -5836,6 +5760,11 @@ def workout_upload_view(request,message="",
|
||||
make_plot = optionsform.cleaned_data['make_plot']
|
||||
plottype = optionsform.cleaned_data['plottype']
|
||||
upload_to_c2 = optionsform.cleaned_data['upload_to_C2']
|
||||
upload_to_strava = optionsform.cleaned_data['upload_to_Strava']
|
||||
upload_to_st = optionsform.cleaned_data['upload_to_SportTracks']
|
||||
upload_to_rk = optionsform.cleaned_data['upload_to_RunKeeper']
|
||||
upload_to_ua = optionsform.cleaned_data['upload_to_MapMyFitness']
|
||||
upload_to_tp = optionsform.cleaned_data['upload_to_TrainingPeaks']
|
||||
makeprivate = optionsform.cleaned_data['makeprivate']
|
||||
|
||||
uploadoptions = {
|
||||
@@ -5843,6 +5772,11 @@ def workout_upload_view(request,message="",
|
||||
'make_plot':make_plot,
|
||||
'plottype':plottype,
|
||||
'upload_to_C2':upload_to_c2,
|
||||
'upload_to_Strava':upload_to_strava,
|
||||
'upload_to_SportTracks':upload_to_st,
|
||||
'upload_to_RunKeeper':upload_to_rk,
|
||||
'upload_to_MapMyFitness':upload_to_ua,
|
||||
'upload_to_TrainingPeaks':upload_to_tp,
|
||||
}
|
||||
|
||||
|
||||
@@ -5933,65 +5867,49 @@ def workout_upload_view(request,message="",
|
||||
filename=fullpathimagename)
|
||||
i.save()
|
||||
|
||||
# upload to C2
|
||||
if (upload_to_c2):
|
||||
try:
|
||||
thetoken = c2_open(request.user)
|
||||
except C2NoTokenError:
|
||||
return HttpResponseRedirect("/rowers/me/c2authorize/")
|
||||
try:
|
||||
c2userid = c2stuff.get_userid(thetoken)
|
||||
if not c2userid:
|
||||
return HttpResponseRedirect("/rowers/me/c2authorize")
|
||||
data = c2stuff.createc2workoutdata(w)
|
||||
authorizationstring = str('Bearer ' + thetoken)
|
||||
headers = {'Authorization': authorizationstring,
|
||||
'user-agent': 'sanderroosendaal',
|
||||
'Content-Type': 'application/json'}
|
||||
# upload to C2
|
||||
if (upload_to_c2):
|
||||
try:
|
||||
c2message,c2id = c2stuff.workout_c2_upload(request.user,w)
|
||||
except C2NoTokenError:
|
||||
pass
|
||||
if (upload_to_strava):
|
||||
try:
|
||||
stravamessage,stravaid = stravastuff.workout_strava_upload(
|
||||
request.user,w
|
||||
)
|
||||
except StravaNoTokenError:
|
||||
pass
|
||||
|
||||
url = "https://log.concept2.com/api/users/%s/results" % (c2userid)
|
||||
response = requests.post(url,headers=headers,data=json.dumps(data))
|
||||
if (upload_to_st):
|
||||
try:
|
||||
stmessage,stid = sporttracksstuff.workout_sporttracks_upload(
|
||||
request.user,w
|
||||
)
|
||||
except SportTracksNoTokenError:
|
||||
pass
|
||||
|
||||
if (upload_to_rk):
|
||||
try:
|
||||
rkmessage,rkid = runkeeperstuff.workout_runkeeper_upload(
|
||||
request.user,w
|
||||
)
|
||||
except RunKeeperNoTokenError:
|
||||
pass
|
||||
|
||||
if message:
|
||||
url = reverse(workout_edit_view,
|
||||
kwargs={
|
||||
'message':message,
|
||||
'id':w.id,
|
||||
})
|
||||
|
||||
# response = c2stuff.workout_c2_upload(request.user,w)
|
||||
if (response.status_code != 201 and response.status_code != 200):
|
||||
if settings.DEBUG:
|
||||
return HttpResponse(response)
|
||||
else:
|
||||
message = "C2 upload failed"
|
||||
url = reverse(workout_edit_view,
|
||||
kwargs={
|
||||
'message':message,
|
||||
'id':str(w.id),
|
||||
})
|
||||
return HttpResponseRedirect(url)
|
||||
else:
|
||||
s= json.loads(response.text)
|
||||
c2id = s['data']['id']
|
||||
w.uploadedtoc2 = c2id
|
||||
w.save()
|
||||
except:
|
||||
message = "C2 upload failed"
|
||||
url = reverse(workout_edit_view,
|
||||
kwargs={
|
||||
'message':message,
|
||||
'id':str(w.id),
|
||||
})
|
||||
return HttpResponseRedirect(url)
|
||||
|
||||
|
||||
if message:
|
||||
url = reverse(workout_edit_view,
|
||||
kwargs={
|
||||
'message':message,
|
||||
'id':w.id,
|
||||
})
|
||||
|
||||
else:
|
||||
url = reverse(workout_edit_view,
|
||||
kwargs = {
|
||||
'id':w.id,
|
||||
})
|
||||
return HttpResponseRedirect(url)
|
||||
else:
|
||||
url = reverse(workout_edit_view,
|
||||
kwargs = {
|
||||
'id':w.id,
|
||||
})
|
||||
return HttpResponseRedirect(url)
|
||||
else:
|
||||
response = render(request,
|
||||
'document_form.html',
|
||||
|
||||
Reference in New Issue
Block a user