@@ -357,80 +357,6 @@ def correct_intensity(workout):
|
||||
|
||||
return workout
|
||||
|
||||
import io
|
||||
import zipfile
|
||||
|
||||
@app.task
|
||||
def email_all_user_workouts_zip(rower, start_date=None, end_date=None, debug=False, **kwargs):
|
||||
# Get all workouts for this user, optionally filtered by date range
|
||||
workouts = Workout.objects.filter(user=rower).order_by('-date')
|
||||
|
||||
# Apply date filters if provided
|
||||
if start_date:
|
||||
workouts = workouts.filter(date__gte=start_date)
|
||||
if end_date:
|
||||
workouts = workouts.filter(date__lte=end_date)
|
||||
|
||||
# for debug, limit to 5 workouts
|
||||
if settings.DEBUG:
|
||||
workouts = workouts[:5]
|
||||
|
||||
if not workouts.exists():
|
||||
dologging('export_all_workouts.log', f"No workouts found for user {rower.user.id} in date range {start_date} to {end_date}")
|
||||
return 0
|
||||
|
||||
# Create ZIP file in memory
|
||||
zip_buffer = io.BytesIO()
|
||||
|
||||
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
|
||||
for workout in workouts:
|
||||
try:
|
||||
rowdata = rdata(csvfile=workout.csvfilename)
|
||||
rowdate = rowdata.rowdatetime
|
||||
starttimeunix = arrow.get(rowdate).timestamp()
|
||||
df = rowdata.df
|
||||
|
||||
try:
|
||||
df[' ElapsedTime (sec)'] = df['TimeStamp (sec)']
|
||||
df['TimeStamp (sec)'] = df['TimeStamp (sec)'] + starttimeunix
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
csv_filename = f"workout_{workout.id}_{workout.date.strftime('%Y%m%d')}.csv"
|
||||
zip_file.writestr(csv_filename, df.to_csv(index=False))
|
||||
except Exception as e: # pragma: no cover
|
||||
dologging('export_all_workouts.log', f"Error exporting workout {workout.id}: {str(e)}")
|
||||
continue
|
||||
|
||||
# Save ZIP file to disk
|
||||
export_date = datetime.datetime.now().strftime('%Y%m%d')
|
||||
filename = f"{rower.user.username}_workouts_{export_date}_from_{start_date}_to_{end_date}_{uuid4().hex[:8]}.zip"
|
||||
zip_file_path = os.path.join(settings.MEDIA_ROOT, filename)
|
||||
|
||||
try:
|
||||
with open(zip_file_path, 'wb') as f:
|
||||
f.write(zip_buffer.getvalue())
|
||||
except Exception as e: # pragma: no cover
|
||||
dologging('export_all_workouts.log', f"Error saving ZIP file: {str(e)}")
|
||||
return 0
|
||||
|
||||
# Send email with download link
|
||||
subject = "Rowsandall Workouts Export"
|
||||
from_email = 'Rowsandall <info@rowsandall.com>'
|
||||
useremail = rower.user.email
|
||||
|
||||
# Generate download URL
|
||||
download_url = f"{SITE_URL}/rowers/workouts/download/?file={filename}"
|
||||
|
||||
_ = send_template_email(
|
||||
from_email, [useremail],
|
||||
subject,
|
||||
'workouts_export_email.html',
|
||||
{'download_url': download_url, 'filename': filename},
|
||||
)
|
||||
|
||||
return 1
|
||||
|
||||
|
||||
@app.task
|
||||
def handle_loadnextweek(rower, debug=False, **kwargs):
|
||||
@@ -4596,4 +4522,3 @@ def fetch_strava_workout(stravatoken, oauth_data, stravaid, csvfilename, userid,
|
||||
stravaid=stravaid, userid=userid))
|
||||
|
||||
return 1
|
||||
|
||||
|
||||
Reference in New Issue
Block a user