Private
Public Access
1
0
Files
rowsandall/rowsandall_app/rows.py
Sander Roosendaal 8895f61e73 gzip
2016-12-08 23:07:57 +01:00

84 lines
1.8 KiB
Python

import time
import gzip
import shutil
from django.core.exceptions import ValidationError
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)
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)
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
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
def validate_file_extension(value):
import os
ext = os.path.splitext(value.name)[1]
valid_extensions = ['.tcx','.csv','.TCX','.CSV','.fit','.FIT']
if not ext in valid_extensions:
raise ValidationError(u'File not supported!')
def must_be_csv(value):
import os
ext = os.path.splitext(value.name)[1]
valid_extensions = ['.csv','.CSV']
if not ext in valid_extensions:
raise ValidationError(u'File not supported!')
def handle_uploaded_file(f):
fname = f.name
timestr = time.strftime("%Y%m%d-%H%M%S")
fname = timestr+'-'+fname
fname2 = 'media/'+fname
with open(fname2,'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
return fname,fname2
# this might work on windows
#import magic
#def validate_mime_type(value):
# supported_types=['text/csv','application/vnd.garmin.tcx+xml']
# with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m:
# mime_type=m.id_buffer(value.file.read())
# value.file.seek(0)
# if mime_type not in supported_types:
# raise ValidationError(u'Unsupported file type.')