fit file download ok
This commit is contained in:
@@ -2244,6 +2244,9 @@ class PlannedSession(models.Model):
|
|||||||
max_length=150,
|
max_length=150,
|
||||||
verbose_name='Session Type')
|
verbose_name='Session Type')
|
||||||
|
|
||||||
|
sessionsport = models.CharField(default='water',choices=mytypes.workouttypes,
|
||||||
|
max_length=50,verbose_name='Sport')
|
||||||
|
|
||||||
sessionvalue = models.IntegerField(default=60,verbose_name='Value')
|
sessionvalue = models.IntegerField(default=60,verbose_name='Value')
|
||||||
|
|
||||||
approximate_distance = models.IntegerField(default=10000,verbose_name='Approximate Distance')
|
approximate_distance = models.IntegerField(default=10000,verbose_name='Approximate Distance')
|
||||||
@@ -2322,12 +2325,14 @@ class PlannedSession(models.Model):
|
|||||||
"You must be a Self-Coach user or higher to create a planned session"
|
"You must be a Self-Coach user or higher to create a planned session"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# interval string
|
# interval string
|
||||||
if self.interval_string:
|
if self.interval_string:
|
||||||
dct = trainingparser.parsetodict(interval_string)
|
dct = trainingparser.parsetodict(self.interval_string)
|
||||||
dct = [item for item in dct if item['value'] != 0]
|
dct = [item for item in dct if item['value'] != 0]
|
||||||
dct = trainingparser.tofitdict(dct)
|
dct = trainingparser.tofitdict(dct)
|
||||||
ps.steps_json = json.dumps(dct)
|
self.steps_json = json.dumps(dct)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -2370,6 +2375,7 @@ class PlannedSession(models.Model):
|
|||||||
|
|
||||||
#super(PlannedSession,self).save(*args, **kwargs)
|
#super(PlannedSession,self).save(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
# read file
|
# read file
|
||||||
if self.fitfile:
|
if self.fitfile:
|
||||||
if self.fitfile.name == 'False':
|
if self.fitfile.name == 'False':
|
||||||
@@ -2379,8 +2385,12 @@ class PlannedSession(models.Model):
|
|||||||
self.steps_json = json.dumps(steps)
|
self.steps_json = json.dumps(steps)
|
||||||
|
|
||||||
if self.steps_json and not self.fitfile:
|
if self.steps_json and not self.fitfile:
|
||||||
fitfile = steps_write_fit(settings.MEDIA_ROOT+'/session_'+str(self.id)+'.fit',json.loads(self.steps_json))
|
filename = settings.MEDIA_ROOT+'/session'+encoder.encode_hex(self.id)+'.fit'
|
||||||
|
steps = json.loads(self.steps_json)
|
||||||
|
steps['filename'] = filename
|
||||||
|
fitfile = steps_write_fit(steps)
|
||||||
self.fitfile = fitfile
|
self.fitfile = fitfile
|
||||||
|
self.steps_json = json.dumps(steps)
|
||||||
|
|
||||||
|
|
||||||
super(PlannedSession,self).save(*args, **kwargs)
|
super(PlannedSession,self).save(*args, **kwargs)
|
||||||
@@ -2532,6 +2542,7 @@ class PlannedSessionForm(ModelForm):
|
|||||||
'enddate',
|
'enddate',
|
||||||
'preferreddate',
|
'preferreddate',
|
||||||
'name',
|
'name',
|
||||||
|
'sessionsport',
|
||||||
'sessiontype',
|
'sessiontype',
|
||||||
'sessionmode',
|
'sessionmode',
|
||||||
'criterium',
|
'criterium',
|
||||||
|
|||||||
@@ -81,7 +81,6 @@ from rowers.tasks import (
|
|||||||
from rowers.utils import totaltime_sec_to_string
|
from rowers.utils import totaltime_sec_to_string
|
||||||
|
|
||||||
def step_to_string(step):
|
def step_to_string(step):
|
||||||
|
|
||||||
target_dict = {
|
target_dict = {
|
||||||
'heart_rate': {
|
'heart_rate': {
|
||||||
'name':'Heart Rate between',
|
'name':'Heart Rate between',
|
||||||
@@ -136,24 +135,30 @@ def step_to_string(step):
|
|||||||
t = dd['name']
|
t = dd['name']
|
||||||
l = ''
|
l = ''
|
||||||
h = ''
|
h = ''
|
||||||
if dd['low']:
|
try:
|
||||||
v = step[dd['low']]
|
if dd['low']:
|
||||||
if value == 'heart_rate':
|
v = step[dd['low']]
|
||||||
if v<100:
|
if value == 'heart_rate':
|
||||||
v = '{v} \%'.format(v=v)
|
if v<100:
|
||||||
else:
|
v = '{v} \%'.format(v=v)
|
||||||
v = v-100
|
else:
|
||||||
v = '{v}'.format(v=v)
|
v = v-100
|
||||||
l = '{v}'.format(v=v)
|
v = '{v}'.format(v=v)
|
||||||
if dd['high']:
|
l = '{v}'.format(v=v)
|
||||||
v = step[dd['high']]
|
except KeyError:
|
||||||
if value == 'heart_rate':
|
pass
|
||||||
if v<100:
|
try:
|
||||||
v = '{v} \%'.format(v=v)
|
if dd['high']:
|
||||||
else:
|
v = step[dd['high']]
|
||||||
v = v-100
|
if value == 'heart_rate':
|
||||||
v = '{v}'.format(v=v)
|
if v<100:
|
||||||
h = 'and {v}'.format(v=v)
|
v = '{v} \%'.format(v=v)
|
||||||
|
else:
|
||||||
|
v = v-100
|
||||||
|
v = '{v}'.format(v=v)
|
||||||
|
h = 'and {v}'.format(v=v)
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
target = '{t} {l} {h}'.format(
|
target = '{t} {l} {h}'.format(
|
||||||
t = t,
|
t = t,
|
||||||
@@ -1137,9 +1142,12 @@ def update_plannedsession(ps,cd):
|
|||||||
|
|
||||||
if cd['fitfile']:
|
if cd['fitfile']:
|
||||||
f = cd['fitfile']
|
f = cd['fitfile']
|
||||||
filename, path_and_filename = handle_uploaded_file(f)
|
try:
|
||||||
ps.fitfile.name = filename
|
filename, path_and_filename = handle_uploaded_file(f)
|
||||||
|
ps.fitfile.name = filename
|
||||||
|
ps.steps_json = ''
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
|
||||||
ps.save()
|
ps.save()
|
||||||
|
|
||||||
|
|||||||
@@ -562,7 +562,7 @@ def steps_read_fit(filename,name='',sport='Custom'):
|
|||||||
|
|
||||||
return d
|
return d
|
||||||
|
|
||||||
def steps_write_fit(filename,steps):
|
def steps_write_fit(steps):
|
||||||
authorizationstring = 'Bearer '+settings.WORKOUTS_FIT_TOKEN
|
authorizationstring = 'Bearer '+settings.WORKOUTS_FIT_TOKEN
|
||||||
url = settings.WORKOUTS_FIT_URL+"/tofit"
|
url = settings.WORKOUTS_FIT_URL+"/tofit"
|
||||||
headers = {'Authorization':authorizationstring}
|
headers = {'Authorization':authorizationstring}
|
||||||
|
|||||||
@@ -9,6 +9,32 @@ from rq import Queue
|
|||||||
from redis import Redis
|
from redis import Redis
|
||||||
from rq.job import Job
|
from rq.job import Job
|
||||||
|
|
||||||
|
@login_required()
|
||||||
|
def download_fit(request,filename=''):
|
||||||
|
r = getrower(request.user)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
pss = PlannedSession.objects.filter(fitfile=filename)
|
||||||
|
print(filename,pss)
|
||||||
|
if len(pss) != 1:
|
||||||
|
raise Http404("Could not find the required file")
|
||||||
|
|
||||||
|
ps = pss[0]
|
||||||
|
owns = False
|
||||||
|
if ps.manager == request.user or request.user.rower in ps.rower.all():
|
||||||
|
owns = True
|
||||||
|
|
||||||
|
if not owns:
|
||||||
|
raise PermissionDenied("You are not allowed to download this file")
|
||||||
|
|
||||||
|
fitfile = ps.fitfile
|
||||||
|
response = HttpResponse(fitfile)
|
||||||
|
response['Content-Disposition'] = 'attachment; filename="%s"' % filename
|
||||||
|
response['Content-Type'] = 'application/octet-stream'
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
@login_required()
|
@login_required()
|
||||||
def failed_queue_view(request):
|
def failed_queue_view(request):
|
||||||
if not request.user.is_staff:
|
if not request.user.is_staff:
|
||||||
|
|||||||
@@ -435,6 +435,7 @@ def plannedsession_create_view(request,
|
|||||||
sessiontype = cd['sessiontype']
|
sessiontype = cd['sessiontype']
|
||||||
sessionmode = cd['sessionmode']
|
sessionmode = cd['sessionmode']
|
||||||
criterium = cd['criterium']
|
criterium = cd['criterium']
|
||||||
|
sessionsport = cd['sessionsport']
|
||||||
sessionvalue = cd['sessionvalue']
|
sessionvalue = cd['sessionvalue']
|
||||||
sessionunit = cd['sessionunit']
|
sessionunit = cd['sessionunit']
|
||||||
comment = cd['comment']
|
comment = cd['comment']
|
||||||
@@ -461,6 +462,7 @@ def plannedsession_create_view(request,
|
|||||||
sessionmode=sessionmode,
|
sessionmode=sessionmode,
|
||||||
sessionvalue=sessionvalue,
|
sessionvalue=sessionvalue,
|
||||||
sessionunit=sessionunit,
|
sessionunit=sessionunit,
|
||||||
|
sessionsport=sessionsport,
|
||||||
comment=comment,
|
comment=comment,
|
||||||
criterium=criterium,
|
criterium=criterium,
|
||||||
interval_string=interval_string,
|
interval_string=interval_string,
|
||||||
@@ -761,6 +763,7 @@ def plannedsession_teamcreate_view(request,
|
|||||||
criterium = cd['criterium']
|
criterium = cd['criterium']
|
||||||
sessionvalue = cd['sessionvalue']
|
sessionvalue = cd['sessionvalue']
|
||||||
sessionunit = cd['sessionunit']
|
sessionunit = cd['sessionunit']
|
||||||
|
sessionsport = cd['sessionsport']
|
||||||
comment = cd['comment']
|
comment = cd['comment']
|
||||||
course = cd['course']
|
course = cd['course']
|
||||||
name = cd['name']
|
name = cd['name']
|
||||||
@@ -780,6 +783,7 @@ def plannedsession_teamcreate_view(request,
|
|||||||
preferreddate=preferreddate,
|
preferreddate=preferreddate,
|
||||||
sessiontype=sessiontype,
|
sessiontype=sessiontype,
|
||||||
sessionmode=sessionmode,
|
sessionmode=sessionmode,
|
||||||
|
sessionsport=sessionsport,
|
||||||
sessionvalue=sessionvalue,
|
sessionvalue=sessionvalue,
|
||||||
sessionunit=sessionunit,
|
sessionunit=sessionunit,
|
||||||
comment=comment,
|
comment=comment,
|
||||||
@@ -2125,6 +2129,7 @@ def plannedsession_view(request,id=0,userid=0):
|
|||||||
'psdict': psdict,
|
'psdict': psdict,
|
||||||
'attrs':[
|
'attrs':[
|
||||||
'name','startdate','enddate','preferreddate',
|
'name','startdate','enddate','preferreddate',
|
||||||
|
'sessionsport',
|
||||||
'sessiontype',
|
'sessiontype',
|
||||||
'sessionmode','criterium',
|
'sessionmode','criterium',
|
||||||
'sessionvalue','sessionunit','comment',
|
'sessionvalue','sessionunit','comment',
|
||||||
|
|||||||
@@ -82,6 +82,7 @@ urlpatterns += [
|
|||||||
re_path(r'^i18n/', include('django.conf.urls.i18n')),
|
re_path(r'^i18n/', include('django.conf.urls.i18n')),
|
||||||
re_path(r'^tz_detect/', include('tz_detect.urls')),
|
re_path(r'^tz_detect/', include('tz_detect.urls')),
|
||||||
re_path(r'^logo/',logoview),
|
re_path(r'^logo/',logoview),
|
||||||
|
re_path(r'^media/(?P<filename>.*\.fit)$',rowersviews.download_fit),
|
||||||
path('django-rq/', include('django_rq.urls')),
|
path('django-rq/', include('django_rq.urls')),
|
||||||
# path('500/', rowersviews.error500_view),
|
# path('500/', rowersviews.error500_view),
|
||||||
# re_path(r'^jsi18n/', django.views.i18n.javascript_catalog,name='jsi18n'),
|
# re_path(r'^jsi18n/', django.views.i18n.javascript_catalog,name='jsi18n'),
|
||||||
|
|||||||
Reference in New Issue
Block a user