From 3db20eced75ea0dc1ca933f266b93c1b325670ed Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 29 May 2018 11:52:48 +0200 Subject: [PATCH] API minor change and fix --- mpwo_api/mpwo_api/activities/activities.py | 6 ++++-- mpwo_api/mpwo_api/activities/utils.py | 9 +++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/mpwo_api/mpwo_api/activities/activities.py b/mpwo_api/mpwo_api/activities/activities.py index 87ddb045..bfe6bca7 100644 --- a/mpwo_api/mpwo_api/activities/activities.py +++ b/mpwo_api/mpwo_api/activities/activities.py @@ -4,6 +4,7 @@ import os from flask import Blueprint, jsonify, request from mpwo_api import appLog, db from sqlalchemy import exc +from werkzeug.utils import secure_filename from ..users.utils import authenticate, verify_extension from .models import Activity, Sport @@ -144,7 +145,8 @@ def post_activity(auth_user_id): return jsonify(response_object), 400 activity_file = request.files['file'] - file_path = get_file_path(auth_user_id, activity_file) + filename = secure_filename(activity_file.filename) + file_path = get_file_path(auth_user_id, filename) try: activity_file.save(file_path) @@ -161,7 +163,7 @@ def post_activity(auth_user_id): new_filepath = get_new_file_path( auth_user_id=auth_user_id, activity_date=gpx_data['start'], - activity_file=activity_file, + old_filename=filename, sport=sport.label ) os.rename(file_path, new_filepath) diff --git a/mpwo_api/mpwo_api/activities/utils.py b/mpwo_api/mpwo_api/activities/utils.py index d465f959..09de64cf 100644 --- a/mpwo_api/mpwo_api/activities/utils.py +++ b/mpwo_api/mpwo_api/activities/utils.py @@ -5,7 +5,6 @@ from datetime import datetime, timedelta import gpxpy.gpx from flask import current_app from mpwo_api import appLog -from werkzeug.utils import secure_filename from .models import Activity, ActivitySegment, Sport @@ -218,8 +217,7 @@ def get_chart_data(gpx_file): return chart_data -def get_file_path(auth_user_id, activity_file): - filename = secure_filename(activity_file.filename) +def get_file_path(auth_user_id, filename): dir_path = os.path.join( current_app.config['UPLOAD_FOLDER'], 'activities', @@ -231,11 +229,10 @@ def get_file_path(auth_user_id, activity_file): return file_path -def get_new_file_path(auth_user_id, activity_date, activity_file, sport): - old_filename = secure_filename(activity_file.filename) +def get_new_file_path(auth_user_id, activity_date, old_filename, sport): extension = f".{old_filename.rsplit('.', 1)[1].lower()}" _, new_filename = tempfile.mkstemp( - prefix=f'{activity_date}_sport_', + prefix=f'{activity_date}_{sport}_', suffix=extension ) dir_path = os.path.join(