Private
Public Access
1
0

removing some duplicate code

This commit is contained in:
Sander Roosendaal
2018-06-26 12:14:00 +02:00
parent 2350d5aedf
commit 0152c8d20b
14 changed files with 99 additions and 333 deletions

View File

@@ -46,30 +46,6 @@ from utils import geo_distance
from rowers.courseutils import coursetime_paths, coursetime_first,time_in_path
def get_course_timezone(course):
polygons = GeoPolygon.objects.filter(course = course)
points = GeoPoint.objects.filter(polygon = polygons[0])
lat = points[0].latitude
lon = points[0].longitude
tf = TimezoneFinder()
try:
timezone_str = tf.timezone_at(lng=lon,lat=lat)
except ValueError:
timezone_str = 'UTC'
if timezone_str is None:
timezone_str = tf.closest_timezone_at(lng=lon,lat=lat)
if timezone_str is None:
timezone_str = 'UTC'
return timezone_str
def crewnerdcourse(doc):
courses = []
for course in doc:

18
rowers/database.py Normal file
View File

@@ -0,0 +1,18 @@
from django.conf import settings
user = settings.DATABASES['default']['USER']
password = settings.DATABASES['default']['PASSWORD']
database_name = settings.DATABASES['default']['NAME']
host = settings.DATABASES['default']['HOST']
port = settings.DATABASES['default']['PORT']
database_url = 'mysql://{user}:{password}@{host}:{port}/{database_name}'.format(
user=user,
password=password,
database_name=database_name,
host=host,
port=port,
)
if settings.DEBUG or user=='':
database_url = 'sqlite:///db.sqlite3'

View File

@@ -71,24 +71,8 @@ queuehigh = django_rq.get_queue('default')
from rowsandall_app.settings import SITE_URL
from rowers.types import otwtypes
user = settings.DATABASES['default']['USER']
password = settings.DATABASES['default']['PASSWORD']
database_name = settings.DATABASES['default']['NAME']
host = settings.DATABASES['default']['HOST']
port = settings.DATABASES['default']['PORT']
from rowers.database import *
database_url = 'mysql://{user}:{password}@{host}:{port}/{database_name}'.format(
user=user,
password=password,
database_name=database_name,
host=host,
port=port,
)
# Use SQLite local database when we're in debug mode
if settings.DEBUG or user == '':
# database_url = 'sqlite:///db.sqlite3'
database_url = 'sqlite:///' + database_name
# mapping the DB column names to the CSV file column names

View File

@@ -133,34 +133,8 @@ def rdata(file,rower=rrower()):
return res
from utils import totaltime_sec_to_string
def totaltime_sec_to_string(totaltime):
hours = int(totaltime / 3600.)
if hours > 23:
message = 'Warning: The workout duration was longer than 23 hours. '
hours = 23
minutes = int((totaltime - 3600. * hours) / 60.)
if minutes > 59:
minutes = 59
if not message:
message = 'Warning: there is something wrong with the workout duration'
seconds = int(totaltime - 3600. * hours - 60. * minutes)
if seconds > 59:
seconds = 59
if not message:
message = 'Warning: there is something wrong with the workout duration'
tenths = int(10 * (totaltime - 3600. * hours - 60. * minutes - seconds))
if tenths > 9:
tenths = 9
if not message:
message = 'Warning: there is something wrong with the workout duration'
duration = "%s:%s:%s.%s" % (hours, minutes, seconds, tenths)
return duration
# Creates C2 stroke data
def create_c2_stroke_data_db(

View File

@@ -49,22 +49,7 @@ tweetapi = twitter.Api(consumer_key=TWEET_CONSUMER_KEY,
access_token_key=TWEET_ACCESS_TOKEN_KEY,
access_token_secret=TWEET_ACCESS_TOKEN_SECRET)
user = settings.DATABASES['default']['USER']
password = settings.DATABASES['default']['PASSWORD']
database_name = settings.DATABASES['default']['NAME']
host = settings.DATABASES['default']['HOST']
port = settings.DATABASES['default']['PORT']
database_url = 'mysql://{user}:{password}@{host}:{port}/{database_name}'.format(
user=user,
password=password,
database_name=database_name,
host=host,
port=port,
)
if settings.DEBUG or user=='':
database_url = 'sqlite:///db.sqlite3'
from rowers.database import *
timezones = (
(x,x) for x in pytz.common_timezones

View File

@@ -2,43 +2,8 @@ from matplotlib.ticker import MultipleLocator,FuncFormatter,NullFormatter
import matplotlib.pyplot as plt
import numpy as np
from rows import format_pace_tick, format_pace, format_time, format_time_tick
# Make pace ticks for pace axis '2:00', '1:50', etc
def format_pace_tick(x,pos=None):
min=int(x/60)
sec=int(x-min*60.)
sec_str=str(sec).zfill(2)
template='%d:%s'
return template % (min,sec_str)
# Returns a pace string from a pace in seconds/500m, '1:45.4'
def format_pace(x,pos=None):
if isinf(x) or isnan(x):
x=0
min=int(x/60)
sec=(x-min*60.)
str1 = "{min:0>2}:{sec:0>4.1f}".format(
min = min,
sec = sec
)
return str1
# Returns a time string from a time in seconds
def format_time(x,pos=None):
min = int(x/60.)
sec = int(x-min*60)
str1 = "{min:0>2}:{sec:0>4.1f}".format(
min=min,
sec=sec,
)
return str1
# Formatting the distance tick marks
def format_dist_tick(x,pos=None):
@@ -46,13 +11,6 @@ def format_dist_tick(x,pos=None):
template='%6.3f'
return template % (km)
# Formatting the time tick marks (h:mm)
def format_time_tick(x,pos=None):
hour=int(x/3600)
min=int((x-hour*3600.)/60)
min_str=str(min).zfill(2)
template='%d:%s'
return template % (hour,min_str)
# Utility to select reasonable y axis range
# Basically the data range plus some padding, but with ultimate

View File

@@ -36,63 +36,15 @@ from rowsandall_app.settings import (
RUNKEEPER_CLIENT_ID, RUNKEEPER_CLIENT_SECRET,RUNKEEPER_REDIRECT_URI,
)
# Custom error class - to raise a NoTokenError
class RunKeeperNoTokenError(Exception):
def __init__(self,value):
self.value=value
from utils import geo_distance,ewmovingaverage,NoTokenError, custom_exception_handler
def __str__(self):
return repr(self.value)
# Exponentially weighted moving average
# Used for data smoothing of the jagged data obtained by Strava
# See bitbucket issue 72
def ewmovingaverage(interval,window_size):
# Experimental code using Exponential Weighted moving average
try:
intervaldf = pd.DataFrame({'v':interval})
idf_ewma1 = intervaldf.ewm(span=window_size)
idf_ewma2 = intervaldf[::-1].ewm(span=window_size)
i_ewma1 = idf_ewma1.mean().ix[:,'v']
i_ewma2 = idf_ewma2.mean().ix[:,'v']
interval2 = np.vstack((i_ewma1,i_ewma2[::-1]))
interval2 = np.mean( interval2, axis=0) # average
except ValueError:
interval2 = interval
return interval2
from utils import geo_distance
# Custom exception handler, returns a 401 HTTP message
# with exception details in the json data
def custom_exception_handler(exc,message):
response = {
"errors": [
{
"code": str(exc),
"detail": message,
}
]
}
res = HttpResponse(message)
res.status_code = 401
res.json = json.dumps(response)
return res
# 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")
raise NoTokenError("User has no token")
else:
thetoken = r.runkeepertoken

View File

@@ -34,40 +34,15 @@ from rowers.models import Rower,Workout,checkworkoutuser
from rowsandall_app.settings import C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET, STRAVA_CLIENT_ID, STRAVA_REDIRECT_URI, STRAVA_CLIENT_SECRET, SPORTTRACKS_CLIENT_SECRET, SPORTTRACKS_CLIENT_ID, SPORTTRACKS_REDIRECT_URI
# Custom error class - to raise a NoTokenError
class SportTracksNoTokenError(Exception):
def __init__(self,value):
self.value=value
from utils import NoTokenError, custom_exception_handler
def __str__(self):
return repr(self.value)
# Custom exception handler, returns a 401 HTTP message
# with exception details in the json data
def custom_exception_handler(exc,message):
response = {
"errors": [
{
"code": str(exc),
"detail": message,
}
]
}
res = HttpResponse(message)
res.status_code = 401
res.json = json.dumps(response)
return res
# 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")
raise NoTokenError("User has no token")
else:
if (timezone.now()>r.sporttrackstokenexpirydate):
thetoken = rower_sporttracks_token_refresh(user)

View File

@@ -275,6 +275,7 @@ def create_async_workout(alldata,user,stravaid):
return w.id
from utils import get_strava_stream
# Get a Strava workout summary data and stroke data by ID
def get_strava_workout(user,stravaid):
@@ -297,18 +298,12 @@ def get_strava_workout(user,stravaid):
workoutsummary['timezone'] = "Etc/UTC"
startdatetime = workoutsummary['start_date']
url = "https://www.strava.com/api/v3/activities/"+str(stravaid)+"/streams/cadence?resolution="+fetchresolution+"&series_type="+series_type
spmjson = requests.get(url,headers=headers)
url = "https://www.strava.com/api/v3/activities/"+str(stravaid)+"/streams/heartrate?resolution="+fetchresolution+"&series_type="+series_type
hrjson = requests.get(url,headers=headers)
url = "https://www.strava.com/api/v3/activities/"+str(stravaid)+"/streams/time?resolution="+fetchresolution+"&series_type="+series_type
timejson = requests.get(url,headers=headers)
url = "https://www.strava.com/api/v3/activities/"+str(stravaid)+"/streams/velocity_smooth?resolution="+fetchresolution+"&series_type="+series_type
velojson = requests.get(url,headers=headers)
url = "https://www.strava.com/api/v3/activities/"+str(stravaid)+"/streams/distance?resolution="+fetchresolution+"&series_type="+series_type
distancejson = requests.get(url,headers=headers)
url = "https://www.strava.com/api/v3/activities/"+str(stravaid)+"/streams/latlng?resolution="+fetchresolution+"&series_type="+series_type
latlongjson = requests.get(url,headers=headers)
spmjson = get_strava_stream(r,'cadence',stravaid)
hrjson = get_strava_stream(r,'heartrate',stravaid)
timejson = get_strava_stream(r,'time',stravaid)
velojson = get_strava_stream(r,'velocity_smooth',stravaid)
distancejson = get_strava_stream(r,'distance',stravaid)
latlongjson = get_strava_stream(r,'latlng',stravaid)
try:
t = np.array(timejson.json()[0]['data'])

View File

@@ -67,6 +67,8 @@ import requests
import longtask
import arrow
from utils import get_strava_stream
siteurl = SITE_URL
# testing task
@@ -98,20 +100,13 @@ def handle_strava_import_stroke_data(stravatoken,
workoutsummary['timezone'] = "Etc/UTC"
startdatetime = workoutsummary['start_date']
url = "https://www.strava.com/api/v3/activities/"+str(stravaid)+"/streams/cadence?resolution="+fetchresolution+"&series_type="+series_type
spmjson = requests.get(url,headers=headers)
url = "https://www.strava.com/api/v3/activities/"+str(stravaid)+"/streams/heartrate?resolution="+fetchresolution+"&series_type="+series_type
hrjson = requests.get(url,headers=headers)
url = "https://www.strava.com/api/v3/activities/"+str(stravaid)+"/streams/time?resolution="+fetchresolution+"&series_type="+series_type
timejson = requests.get(url,headers=headers)
url = "https://www.strava.com/api/v3/activities/"+str(stravaid)+"/streams/velocity_smooth?resolution="+fetchresolution+"&series_type="+series_type
velojson = requests.get(url,headers=headers)
url = "https://www.strava.com/api/v3/activities/"+str(stravaid)+"/streams/distance?resolution="+fetchresolution+"&series_type="+series_type
distancejson = requests.get(url,headers=headers)
url = "https://www.strava.com/api/v3/activities/"+str(stravaid)+"/streams/latlng?resolution="+fetchresolution+"&series_type="+series_type
latlongjson = requests.get(url,headers=headers)
url = "https://www.strava.com/api/v3/activities/"+str(stravaid)+"/streams/watts?resolution="+fetchresolution+"&series_type="+series_type
wattsjson = requests.get(url,headers=headers)
spmjson = get_strava_stream(r,'cadence',stravaid)
hrjson = get_strava_stream(r,'heartrate',stravaid)
timejson = get_strava_stream(r,'time',stravaid)
velojson = get_strava_stream(r,'velocity_smooth',stravaid)
distancejson = get_strava_stream(r,'distance',stravaid)
latlongjson = get_strava_stream(r,'latlng',stravaid)
wattsjson = get_strava_stream(r,'watts',stravaid)
try:
t = np.array(timejson.json()[0]['data'])

View File

@@ -52,63 +52,17 @@ from async_messages import message_user,messages
# Custom error class - to raise a NoTokenError
class TPNoTokenError(Exception):
def __init__(self,value):
self.value=value
def __str__(self):
return repr(self.value)
# Exponentially weighted moving average
# Used for data smoothing of the jagged data obtained by Strava
# See bitbucket issue 72
def ewmovingaverage(interval,window_size):
# Experimental code using Exponential Weighted moving average
try:
intervaldf = pd.DataFrame({'v':interval})
idf_ewma1 = intervaldf.ewm(span=window_size)
idf_ewma2 = intervaldf[::-1].ewm(span=window_size)
i_ewma1 = idf_ewma1.mean().ix[:,'v']
i_ewma2 = idf_ewma2.mean().ix[:,'v']
interval2 = np.vstack((i_ewma1,i_ewma2[::-1]))
interval2 = np.mean( interval2, axis=0) # average
except ValueError:
interval2 = interval
return interval2
from utils import geo_distance
from utils import geo_distance, NoTokenError,ewmovingaverage, custom_exception_handler
# Custom exception handler, returns a 401 HTTP message
# with exception details in the json data
def custom_exception_handler(exc,message):
response = {
"errors": [
{
"code": str(exc),
"detail": message,
}
]
}
res = HttpResponse(message)
res.status_code = 401
res.json = json.dumps(response)
return res
# Checks if user has UnderArmour token, renews them if they are expired
def tp_open(user):
r = Rower.objects.get(user=user)
if (r.tptoken == '') or (r.tptoken is None):
s = "Token doesn't exist. Need to authorize"
raise TPNoTokenError("User has no token")
raise NoTokenError("User has no token")
else:
if (timezone.now()>r.tptokenexpirydate):
res = do_refresh_token(r.tprefreshtoken)
@@ -120,7 +74,7 @@ def tp_open(user):
r.save()
thetoken = r.tptoken
else:
raise TPNoTokenError("Refresh token invalid")
raise NoTokenError("Refresh token invalid")
else:
thetoken = r.tptoken

View File

@@ -39,63 +39,18 @@ from rowsandall_app.settings import (
UNDERARMOUR_REDIRECT_URI,UNDERARMOUR_CLIENT_KEY,
)
# Custom error class - to raise a NoTokenError
class UnderArmourNoTokenError(Exception):
def __init__(self,value):
self.value=value
from utils import NoTokenError,ewmovingaverage
def __str__(self):
return repr(self.value)
# Exponentially weighted moving average
# Used for data smoothing of the jagged data obtained by Strava
# See bitbucket issue 72
def ewmovingaverage(interval,window_size):
# Experimental code using Exponential Weighted moving average
try:
intervaldf = pd.DataFrame({'v':interval})
idf_ewma1 = intervaldf.ewm(span=window_size)
idf_ewma2 = intervaldf[::-1].ewm(span=window_size)
i_ewma1 = idf_ewma1.mean().ix[:,'v']
i_ewma2 = idf_ewma2.mean().ix[:,'v']
interval2 = np.vstack((i_ewma1,i_ewma2[::-1]))
interval2 = np.mean( interval2, axis=0) # average
except ValueError:
interval2 = interval
return interval2
from utils import geo_distance
from utils import geo_distance, custom_exception_handler
# Custom exception handler, returns a 401 HTTP message
# with exception details in the json data
def custom_exception_handler(exc,message):
response = {
"errors": [
{
"code": str(exc),
"detail": message,
}
]
}
res = HttpResponse(message)
res.status_code = 401
res.json = json.dumps(response)
return res
# Checks if user has UnderArmour token, renews them if they are expired
def underarmour_open(user):
r = Rower.objects.get(user=user)
if (r.underarmourtoken == '') or (r.underarmourtoken is None):
s = "Token doesn't exist. Need to authorize"
raise UnderArmourNoTokenError("User has no token")
raise NoTokenError("User has no token")
else:
if (timezone.now()>r.underarmourtokenexpirydate):
res = do_refresh_token(

View File

@@ -7,6 +7,7 @@ from django.conf import settings
import uuid
import datetime
import requests
lbstoN = 4.44822
@@ -398,3 +399,47 @@ def ewmovingaverage(interval,window_size):
interval2 = interval
return interval2
# Exceptions
# Custom error class - to raise a NoTokenError
class NoTokenError(Exception):
def __init__(self,value):
self.value=value
def __str__(self):
return repr(self.value)
# Custom exception handler, returns a 401 HTTP message
# with exception details in the json data
def custom_exception_handler(exc,message):
response = {
"errors": [
{
"code": str(exc),
"detail": message,
}
]
}
res = HttpResponse(message)
res.status_code = 401
res.json = json.dumps(response)
return res
def get_strava_stream(r,metric,stravaid,series_type='time',fetchresolution='high'):
authorizationstring = str('Bearer ' + r.stravatoken)
headers = {'Authorization': authorizationstring,
'user-agent': 'sanderroosendaal',
'Content-Type': 'application/json',
'resolution': 'medium',}
url = "https://www.strava.com/api/v3/activities/{stravaid}/streams/{metric}?resolution={fetchresolutions}&series_type={series_type}".format(
stravaid=stravid,
fetchresolution=fetchresolution,
series_type=series_type,
metric=metric
)
return requests.get(url,headers=headers)

View File

@@ -90,10 +90,10 @@ import os,sys
import datetime
import iso8601
import c2stuff
from c2stuff import C2NoTokenError,c2_open
from runkeeperstuff import RunKeeperNoTokenError,runkeeper_open
from sporttracksstuff import SportTracksNoTokenError,sporttracks_open
from tpstuff import TPNoTokenError,tp_open
from c2stuff import c2_open
from runkeeperstuff import runkeeper_open
from sporttracksstuff import sporttracks_open
from tpstuff import tp_open
from iso8601 import ParseError
import stravastuff
import polarstuff
@@ -101,7 +101,7 @@ from polarstuff import PolarNoTokenError
from stravastuff import StravaNoTokenError
import sporttracksstuff
import underarmourstuff
from underarmourstuff import UnderArmourNoTokenError,underarmour_open
from underarmourstuff import underarmour_open
import tpstuff
import runkeeperstuff
import ownapistuff
@@ -887,7 +887,7 @@ from utils import (
geo_distance,serialize_list,deserialize_list,uniqify,
str2bool,range_to_color_hex,absolute,myqueue,get_call,
calculate_age,rankingdistances,rankingdurations,
is_ranking_piece,my_dict_from_instance,wavg
is_ranking_piece,my_dict_from_instance,wavg,NoTokenError
)
import datautils