API minor change and fix

This commit is contained in:
Sam 2018-05-29 11:52:48 +02:00
parent c1af957e50
commit 3db20eced7
2 changed files with 7 additions and 8 deletions

View File

@ -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)

View File

@ -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(