AP: refactor

This commit is contained in:
Sam 2018-05-31 15:17:40 +02:00
parent f9d188ceb2
commit 809687c9b1
2 changed files with 31 additions and 25 deletions

View File

@ -273,25 +273,23 @@ def get_map_hash(map_filepath):
return md5.hexdigest() return md5.hexdigest()
def process_one_gpx_file(auth_user_id, activity_data, file_path, filename): def process_one_gpx_file(params, filename):
try: try:
gpx_data, map_data = get_gpx_info(file_path) gpx_data, map_data = get_gpx_info(params['file_path'])
sport = Sport.query.filter_by(id=activity_data.get('sport_id')).first()
new_filepath = get_new_file_path( new_filepath = get_new_file_path(
auth_user_id=auth_user_id, auth_user_id=params['auth_user_id'],
activity_date=gpx_data['start'], activity_date=gpx_data['start'],
old_filename=filename, old_filename=filename,
sport=sport.label sport=params['sport_label']
) )
os.rename(file_path, new_filepath) os.rename(params['file_path'], new_filepath)
gpx_data['filename'] = new_filepath gpx_data['filename'] = new_filepath
map_filepath = get_new_file_path( map_filepath = get_new_file_path(
auth_user_id=auth_user_id, auth_user_id=params['auth_user_id'],
activity_date=gpx_data['start'], activity_date=gpx_data['start'],
extension='.png', extension='.png',
sport=sport.label sport=params['sport_label']
) )
generate_map(map_filepath, map_data) generate_map(map_filepath, map_data)
except (gpxpy.gpx.GPXXMLSyntaxException, TypeError) as e: except (gpxpy.gpx.GPXXMLSyntaxException, TypeError) as e:
@ -301,10 +299,9 @@ def process_one_gpx_file(auth_user_id, activity_data, file_path, filename):
try: try:
new_activity = create_activity( new_activity = create_activity(
auth_user_id, activity_data, gpx_data) params['auth_user_id'], params['activity_data'], gpx_data)
new_activity.map = map_filepath new_activity.map = map_filepath
new_activity.map_id = get_map_hash(map_filepath) new_activity.map_id = get_map_hash(map_filepath)
print(new_activity.map_id)
db.session.add(new_activity) db.session.add(new_activity)
db.session.flush() db.session.flush()
@ -314,13 +311,11 @@ def process_one_gpx_file(auth_user_id, activity_data, file_path, filename):
db.session.commit() db.session.commit()
return new_activity return new_activity
except (exc.IntegrityError, ValueError) as e: except (exc.IntegrityError, ValueError) as e:
raise ActivityException( raise ActivityException('fail', 'Error during activity save.', e)
'fail', 'Error during activity save.', e
)
def process_zip_archive(auth_user_id, activity_data, zip_path, extract_dir): def process_zip_archive(common_params, extract_dir):
with zipfile.ZipFile(zip_path, "r") as zip_ref: with zipfile.ZipFile(common_params['file_path'], "r") as zip_ref:
zip_ref.extractall(extract_dir) zip_ref.extractall(extract_dir)
new_activities = [] new_activities = []
@ -329,8 +324,9 @@ def process_zip_archive(auth_user_id, activity_data, zip_path, extract_dir):
if ('.' in gpx_file and gpx_file.rsplit('.', 1)[1].lower() if ('.' in gpx_file and gpx_file.rsplit('.', 1)[1].lower()
in current_app.config.get('ACTIVITY_ALLOWED_EXTENSIONS')): in current_app.config.get('ACTIVITY_ALLOWED_EXTENSIONS')):
file_path = os.path.join(extract_dir, gpx_file) file_path = os.path.join(extract_dir, gpx_file)
new_activity = process_one_gpx_file(auth_user_id, activity_data, params = common_params
file_path, gpx_file) params['file_path'] = file_path
new_activity = process_one_gpx_file(params, gpx_file)
new_activities.append(new_activity) new_activities.append(new_activity)
return new_activities return new_activities
@ -340,6 +336,20 @@ def process_files(auth_user_id, activity_data, activity_file, folders):
filename = secure_filename(activity_file.filename) filename = secure_filename(activity_file.filename)
extension = f".{filename.rsplit('.', 1)[1].lower()}" extension = f".{filename.rsplit('.', 1)[1].lower()}"
file_path = get_file_path(auth_user_id, folders['tmp_dir'], filename) file_path = get_file_path(auth_user_id, folders['tmp_dir'], filename)
sport = Sport.query.filter_by(id=activity_data.get('sport_id')).first()
if not sport:
raise ActivityException(
'error',
f"Sport id: {activity_data.get('sport_id')} does not exist",
None
)
common_params = {
'auth_user_id': auth_user_id,
'activity_data': activity_data,
'file_path': file_path,
'sport_label': sport.label,
}
try: try:
activity_file.save(file_path) activity_file.save(file_path)
@ -347,9 +357,6 @@ def process_files(auth_user_id, activity_data, activity_file, folders):
raise ActivityException('error', 'Error during activity file save.', e) raise ActivityException('error', 'Error during activity file save.', e)
if extension == ".gpx": if extension == ".gpx":
return [process_one_gpx_file( return [process_one_gpx_file(common_params, filename)]
auth_user_id, activity_data, file_path, filename
)]
else: else:
return process_zip_archive( return process_zip_archive(common_params, folders['extract_dir'])
auth_user_id, activity_data, file_path, folders['extract_dir'])

View File

@ -463,8 +463,7 @@ def test_add_an_activity_gpx_incorrect_sport_id(
assert response.status_code == 500 assert response.status_code == 500
assert data['status'] == 'error' assert data['status'] == 'error'
assert data['message'] == \ assert data['message'] == 'Sport id: 2 does not exist'
'Error during activity file save.'
def test_add_an_activity_gpx_no_file(app, user_1, sport_1_cycling): def test_add_an_activity_gpx_no_file(app, user_1, sport_1_cycling):