Private
Public Access
1
0
This commit is contained in:
2025-10-23 10:27:13 +02:00
parent fd2e815414
commit d96e8f3b3c
6 changed files with 968 additions and 59 deletions

View File

@@ -58,7 +58,7 @@ from uuid import uuid4
def getrower(user):
try:
if user is None or user.is_anonymous:
if user is None or user.is_anonymous: # pragma: no cover
return None
except AttributeError: # pragma: no cover
if User.objects.get(id=user).is_anonymous:
@@ -77,12 +77,12 @@ def generate_job_id():
def valid_uploadoptions(uploadoptions):
fstr = uploadoptions.get('file', None)
if fstr is None:
if fstr is None: # pragma: no cover
return False, "Missing file in upload options."
# check if file can be found
if isinstance(fstr, str):
if not os.path.isfile(fstr):
if not os.path.isfile(fstr): # pragma: no cover
return False, f"File not found: {fstr}"
@@ -108,19 +108,19 @@ def is_invalid_file(file_path):
return False, "Image files are not supported for upload."
if fileformat == "json":
return False, "JSON files are not supported for upload."
if fileformat == "c2log":
if fileformat == "c2log": # pragma: no cover
return False, "Concept2 log files are not supported for upload."
if fileformat == "nostrokes":
if fileformat == "nostrokes": # pragma: no cover
return False, "No stroke data found in the file."
if fileformat == "kml":
return False, "KML files are not supported for upload."
if fileformat == "notgzip":
if fileformat == "notgzip": # pragma: no cover
return False, "The gzip file appears to be corrupted."
if fileformat == "rowprolog":
if fileformat == "rowprolog": # pragma: no cover
return False, "RowPro logbook summary files are not supported for upload."
if fileformat == "gpx":
return False, "GPX files are not supported for upload."
if fileformat == "unknown":
if fileformat == "unknown": # pragma: no cover
extension = os.path.splitext(f2)[1]
filename = os.path.splitext(f2)[0]
if extension == '.gz':
@@ -139,14 +139,14 @@ def is_invalid_file(file_path):
def upload_handler(uploadoptions, filename):
valid, message = valid_uploadoptions(uploadoptions)
if not valid:
if not valid: # pragma: no cover
return {
"status": "error",
"job_id": None,
"message": message
}
is_valid, message = is_invalid_file(filename)
if not is_valid:
if not is_valid: # pragma: no cover
os.remove(filename)
return {
"status": "error",
@@ -203,7 +203,7 @@ def unzip_and_process(zip_filepath, uploadoptions, parent_job_id, debug=False, *
def get_rower_from_uploadoptions(uploadoptions):
rowerform = TeamInviteForm(uploadoptions)
if not rowerform.is_valid():
if not rowerform.is_valid(): # pragma: no cover
return None
try:
u = rowerform.cleaned_data['user']
@@ -214,7 +214,7 @@ def get_rower_from_uploadoptions(uploadoptions):
if len(us):
u = us[0]
r = getrower(u)
else:
else: # pragma: no cover
r = None
for rwr in Rower.objects.all():
if rwr.emailalternatives is not None:
@@ -322,7 +322,7 @@ def update_workout_attributes(w, row, file_path, uploadoptions,
empowerside = 'starboard'
stravaid = uploadoptions.get('stravaid',0)
if stravaid != 0:
if stravaid != 0: # pragma: no cover
workoutsource = 'strava'
w.uploadedtostrava = stravaid
@@ -347,10 +347,10 @@ def update_workout_attributes(w, row, file_path, uploadoptions,
totaltime = row.df['TimeStamp (sec)'].max() - row.df['TimeStamp (sec)'].min()
try:
totaltime = totaltime + row.df.loc[:, ' ElapsedTime (sec)'].iloc[0]
except KeyError:
except KeyError: # pragma: no cover
pass
if np.isnan(totaltime):
if np.isnan(totaltime): # pragma: no cover
totaltime = 0
if uploadoptions.get('summary', '') == '':
@@ -358,11 +358,11 @@ def update_workout_attributes(w, row, file_path, uploadoptions,
else:
summary = uploadoptions.get('summary', '')
if uploadoptions.get('makeprivate', False):
if uploadoptions.get('makeprivate', False): # pragma: no cover
privacy = 'hidden'
elif workoutsource != 'strava':
privacy = 'visible'
else:
else: # pragma: no cover
privacy = 'hidden'
# checking for in values
@@ -378,7 +378,7 @@ def update_workout_attributes(w, row, file_path, uploadoptions,
try:
workoutenddatetime = startdatetime+delta
except AttributeError as e:
except AttributeError as e: # pragma: no cover
workoutstartdatetime = pendulum.parse(str(startdatetime))
workoutenddatetime = startdatetime+delta
@@ -386,7 +386,7 @@ def update_workout_attributes(w, row, file_path, uploadoptions,
# check for duplicate start times and duration
duplicate = checkduplicates(
w.user, startdate, startdatetime, workoutenddatetime)
if duplicate:
if duplicate: # pragma: no cover
rankingpiece = False
# test title length
@@ -431,7 +431,7 @@ def update_workout_attributes(w, row, file_path, uploadoptions,
w.save()
# check for registrationid
if registrationid != 0:
if registrationid != 0: # pragma: no cover
races = VirtualRace.objects.filter(
registration_closure__gt=tz.now(),
id=raceid,
@@ -495,11 +495,11 @@ def update_running_wps(r, w, row):
duplicate=False).count()
new_value = (cntr*r.running_wps_erg + row.df['driveenergy'].mean())/(cntr+1.0)
# if new_value is not zero or infinite or -inf, r.running_wps can be set to value
if not (math.isnan(new_value) or math.isinf(new_value) or new_value == 0):
if not (math.isnan(new_value) or math.isinf(new_value) or new_value == 0): # pragma: no cover
r.running_wps_erg = new_value
elif not (math.isnan(r.running_wps_erg) or math.isinf(r.running_wps_erg) or r.running_wps_erg == 0):
pass
else:
else: # pragma: no cover
r.running_wps_erg = 600.
r.save()
@@ -509,13 +509,13 @@ def update_running_wps(r, w, row):
duplicate=False).count()
try:
new_value = (cntr*r.running_wps_erg + row.df['driveenergy'].mean())/(cntr+1.0)
except TypeError:
except TypeError: # pragma: no cover
new_value = r.running_wps
if not (math.isnan(new_value) or math.isinf(new_value) or new_value == 0):
r.running_wps = new_value
elif not (math.isnan(r.running_wps) or math.isinf(r.running_wps) or r.running_wps == 0):
pass
else:
else: # pragma: no cover
r.running_wps = 400.
r.save()
@@ -531,7 +531,7 @@ def process_single_file(file_path, uploadoptions, job_id, debug=False, **kwargs)
f1 = uuid4().hex[:10]+'-'+strftime('%Y%m%d-%H%M%S')+ext
f2 = 'media/'+f1
copyfile(file_path, f2)
except FileNotFoundError:
except FileNotFoundError: # pragma: no cover
return {
"status": "error",
"job_id": job_id,
@@ -540,7 +540,7 @@ def process_single_file(file_path, uploadoptions, job_id, debug=False, **kwargs)
# determine the user
r = get_rower_from_uploadoptions(uploadoptions)
if r is None:
if r is None: # pragma: no cover
os.remove(f2)
return {
"status": "error",
@@ -550,7 +550,7 @@ def process_single_file(file_path, uploadoptions, job_id, debug=False, **kwargs)
try:
fileformat = get_file_type(f2)
except Exception as e:
except Exception as e: # pragma: no cover
os.remove(f2)
return {
"status": "error",
@@ -559,16 +559,17 @@ def process_single_file(file_path, uploadoptions, job_id, debug=False, **kwargs)
}
# Get fileformat from fit & tcx
if fileformat == 'fit':
if "fit" in fileformat:
workouttype = get_workouttype_from_fit(f2)
uploadoptions['workouttype'] = workouttype
new_title = get_title_from_fit(f2)
if new_title:
if new_title: # pragma: no cover
uploadoptions['title'] = new_title
new_notes = get_notes_from_fit(f2)
if new_notes:
if new_notes: # pragma: no cover
uploadoptions['notes'] = new_notes
# handle non-Painsled
if fileformat != 'csv':
f2, summary, oarlength, inboard, fileformat, impeller = handle_nonpainsled(
@@ -581,7 +582,7 @@ def process_single_file(file_path, uploadoptions, job_id, debug=False, **kwargs)
uploadoptions['useImpeller'] = impeller
if uploadoptions['workouttype'] != 'strave':
uploadoptions['workoutsource'] = fileformat
if not f2:
if not f2: # pragma: no cover
return {
"status": "error",
"job_id": job_id,
@@ -600,7 +601,7 @@ def process_single_file(file_path, uploadoptions, job_id, debug=False, **kwargs)
powerperc=powerperc, powerzones=r.powerzones)
row = rdata(f2, rower=rr)
if row.df.empty:
if row.df.empty: # pragma: no cover
os.remove(f2)
return {
"status": "error",
@@ -608,7 +609,7 @@ def process_single_file(file_path, uploadoptions, job_id, debug=False, **kwargs)
"message": "No valid data found in the uploaded file."
}
if row == 0:
if row == 0: # pragma: no cover
os.remove(f2)
return {
"status": "error",
@@ -639,7 +640,7 @@ def process_single_file(file_path, uploadoptions, job_id, debug=False, **kwargs)
pass
workoutid = uploadoptions.get('id', None)
if workoutid is not None:
if workoutid is not None: # pragma: no cover
try:
w = Workout.objects.get(id=workoutid)
except Workout.DoesNotExist:
@@ -657,7 +658,7 @@ def process_single_file(file_path, uploadoptions, job_id, debug=False, **kwargs)
if w.privacy == 'visible':
ts = Team.objects.filter(rower=r
)
for t in ts:
for t in ts: # pragma: no cover
w.team.add(t)
# put stroke data in file store through "dataplep"
@@ -686,14 +687,14 @@ def process_single_file(file_path, uploadoptions, job_id, debug=False, **kwargs)
wps_avg = r.median_wps
elif w.workouttype in otetypes:
wps_avg = r.median_wps_erg
else:
else: # pragma: no cover
wps_avg = 0
_ = myqueue(queuehigh, handle_calctrimp, w.id, f2,
r.ftp, r.sex, r.hrftp, r.max, r.rest, wps_avg)
# make plots
if uploadoptions.get('makeplot', False):
if uploadoptions.get('makeplot', False): # pragma: no cover
plottype = uploadoptions.get('plottype', 'timeplot')
res, jobid = uploads.make_plot(r, w, f1, f2, plottype, w.name)
elif r.staticchartonupload != 'None': # pragma: no cover