API: refactor
remove user query in 'get_datetime_with_tz' to avoid multiple queries when importing a zip file
This commit is contained in:
parent
489a123671
commit
3ffc629e55
@ -7,7 +7,7 @@ from fittrackee_api import appLog, db
|
|||||||
from flask import Blueprint, current_app, jsonify, request, send_file
|
from flask import Blueprint, current_app, jsonify, request, send_file
|
||||||
from sqlalchemy import exc
|
from sqlalchemy import exc
|
||||||
|
|
||||||
from ..users.utils import authenticate, verify_extension
|
from ..users.utils import User, authenticate, verify_extension
|
||||||
from .models import Activity
|
from .models import Activity
|
||||||
from .utils import (
|
from .utils import (
|
||||||
ActivityException, create_activity, edit_activity, get_chart_data,
|
ActivityException, create_activity, edit_activity, get_chart_data,
|
||||||
@ -23,17 +23,18 @@ activities_blueprint = Blueprint('activities', __name__)
|
|||||||
def get_activities(auth_user_id):
|
def get_activities(auth_user_id):
|
||||||
"""Get all activities for authenticated user"""
|
"""Get all activities for authenticated user"""
|
||||||
try:
|
try:
|
||||||
|
user = User.query.filter_by(id=auth_user_id).first()
|
||||||
params = request.args.copy()
|
params = request.args.copy()
|
||||||
page = 1 if 'page' not in params.keys() else int(params.get('page'))
|
page = 1 if 'page' not in params.keys() else int(params.get('page'))
|
||||||
date_from = params.get('from')
|
date_from = params.get('from')
|
||||||
if date_from:
|
if date_from:
|
||||||
date_from = datetime.strptime(date_from, '%Y-%m-%d')
|
date_from = datetime.strptime(date_from, '%Y-%m-%d')
|
||||||
_, date_from = get_datetime_with_tz(auth_user_id, date_from)
|
_, date_from = get_datetime_with_tz(user.timezone, date_from)
|
||||||
date_to = params.get('to')
|
date_to = params.get('to')
|
||||||
if date_to:
|
if date_to:
|
||||||
date_to = datetime.strptime(f'{date_to} 23:59:59',
|
date_to = datetime.strptime(f'{date_to} 23:59:59',
|
||||||
'%Y-%m-%d %H:%M:%S')
|
'%Y-%m-%d %H:%M:%S')
|
||||||
_, date_to = get_datetime_with_tz(auth_user_id, date_to)
|
_, date_to = get_datetime_with_tz(user.timezone, date_to)
|
||||||
distance_from = params.get('distance_from')
|
distance_from = params.get('distance_from')
|
||||||
distance_to = params.get('distance_to')
|
distance_to = params.get('distance_to')
|
||||||
duration_from = params.get('duration_from')
|
duration_from = params.get('duration_from')
|
||||||
@ -268,7 +269,8 @@ def post_activity_no_gpx(auth_user_id):
|
|||||||
return jsonify(response_object), 400
|
return jsonify(response_object), 400
|
||||||
|
|
||||||
try:
|
try:
|
||||||
new_activity = create_activity(auth_user_id, activity_data)
|
user = User.query.filter_by(id=auth_user_id).first()
|
||||||
|
new_activity = create_activity(user, activity_data)
|
||||||
db.session.add(new_activity)
|
db.session.add(new_activity)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
@ -26,12 +26,12 @@ def get_activities(user_id, type):
|
|||||||
date_from = params.get('from')
|
date_from = params.get('from')
|
||||||
if date_from:
|
if date_from:
|
||||||
date_from = datetime.strptime(date_from, '%Y-%m-%d')
|
date_from = datetime.strptime(date_from, '%Y-%m-%d')
|
||||||
_, date_from = get_datetime_with_tz(user_id, date_from)
|
_, date_from = get_datetime_with_tz(user.timezone, date_from)
|
||||||
date_to = params.get('to')
|
date_to = params.get('to')
|
||||||
if date_to:
|
if date_to:
|
||||||
date_to = datetime.strptime(f'{date_to} 23:59:59',
|
date_to = datetime.strptime(f'{date_to} 23:59:59',
|
||||||
'%Y-%m-%d %H:%M:%S')
|
'%Y-%m-%d %H:%M:%S')
|
||||||
_, date_to = get_datetime_with_tz(user_id, date_to)
|
_, date_to = get_datetime_with_tz(user.timezone, date_to)
|
||||||
sport_id = params.get('sport_id')
|
sport_id = params.get('sport_id')
|
||||||
time = params.get('time')
|
time = params.get('time')
|
||||||
|
|
||||||
|
@ -23,12 +23,11 @@ class ActivityException(Exception):
|
|||||||
self.e = e
|
self.e = e
|
||||||
|
|
||||||
|
|
||||||
def get_datetime_with_tz(auth_user_id, activity_date, gpx_data=None):
|
def get_datetime_with_tz(timezone, activity_date, gpx_data=None):
|
||||||
user = User.query.filter_by(id=auth_user_id).first()
|
|
||||||
# activity date in gpx are directly in UTC
|
# activity date in gpx are directly in UTC
|
||||||
activity_date_tz = None
|
activity_date_tz = None
|
||||||
if user.timezone and not gpx_data:
|
if timezone and not gpx_data:
|
||||||
user_tz = pytz.timezone(user.timezone)
|
user_tz = pytz.timezone(timezone)
|
||||||
activity_date_tz = user_tz.localize(activity_date)
|
activity_date_tz = user_tz.localize(activity_date)
|
||||||
if not gpx_data:
|
if not gpx_data:
|
||||||
# make datetime 'naive' like in gpx file
|
# make datetime 'naive' like in gpx file
|
||||||
@ -52,12 +51,12 @@ def update_activity_data(activity, gpx_data):
|
|||||||
|
|
||||||
|
|
||||||
def create_activity(
|
def create_activity(
|
||||||
auth_user_id, activity_data, gpx_data=None
|
user, activity_data, gpx_data=None
|
||||||
):
|
):
|
||||||
activity_date = gpx_data['start'] if gpx_data else datetime.strptime(
|
activity_date = gpx_data['start'] if gpx_data else datetime.strptime(
|
||||||
activity_data.get('activity_date'), '%Y-%m-%d %H:%M')
|
activity_data.get('activity_date'), '%Y-%m-%d %H:%M')
|
||||||
activity_date_tz, activity_date = get_datetime_with_tz(
|
activity_date_tz, activity_date = get_datetime_with_tz(
|
||||||
auth_user_id, activity_date, gpx_data)
|
user.timezone, activity_date, gpx_data)
|
||||||
|
|
||||||
duration = gpx_data['duration'] if gpx_data \
|
duration = gpx_data['duration'] if gpx_data \
|
||||||
else timedelta(seconds=activity_data.get('duration'))
|
else timedelta(seconds=activity_data.get('duration'))
|
||||||
@ -67,7 +66,7 @@ def create_activity(
|
|||||||
else activity_data.get('title')
|
else activity_data.get('title')
|
||||||
|
|
||||||
new_activity = Activity(
|
new_activity = Activity(
|
||||||
user_id=auth_user_id,
|
user_id=user.id,
|
||||||
sport_id=activity_data.get('sport_id'),
|
sport_id=activity_data.get('sport_id'),
|
||||||
activity_date=activity_date,
|
activity_date=activity_date,
|
||||||
distance=distance,
|
distance=distance,
|
||||||
@ -111,6 +110,7 @@ def create_segment(activity_id, segment_data):
|
|||||||
|
|
||||||
|
|
||||||
def edit_activity(activity, activity_data, auth_user_id):
|
def edit_activity(activity, activity_data, auth_user_id):
|
||||||
|
user = User.query.filter_by(id=auth_user_id).first()
|
||||||
if activity_data.get('sport_id'):
|
if activity_data.get('sport_id'):
|
||||||
activity.sport_id = activity_data.get('sport_id')
|
activity.sport_id = activity_data.get('sport_id')
|
||||||
if activity_data.get('title'):
|
if activity_data.get('title'):
|
||||||
@ -120,7 +120,7 @@ def edit_activity(activity, activity_data, auth_user_id):
|
|||||||
activity_date = datetime.strptime(
|
activity_date = datetime.strptime(
|
||||||
activity_data.get('activity_date'), '%Y-%m-%d %H:%M')
|
activity_data.get('activity_date'), '%Y-%m-%d %H:%M')
|
||||||
_, activity.activity_date = get_datetime_with_tz(
|
_, activity.activity_date = get_datetime_with_tz(
|
||||||
auth_user_id, activity_date)
|
user.timezone, activity_date)
|
||||||
|
|
||||||
if activity_data.get('duration'):
|
if activity_data.get('duration'):
|
||||||
activity.duration = timedelta(
|
activity.duration = timedelta(
|
||||||
@ -306,8 +306,9 @@ def get_map_hash(map_filepath):
|
|||||||
def process_one_gpx_file(params, filename):
|
def process_one_gpx_file(params, filename):
|
||||||
try:
|
try:
|
||||||
gpx_data, map_data = get_gpx_info(params['file_path'])
|
gpx_data, map_data = get_gpx_info(params['file_path'])
|
||||||
|
auth_user_id = params['user'].id
|
||||||
new_filepath = get_new_file_path(
|
new_filepath = get_new_file_path(
|
||||||
auth_user_id=params['auth_user_id'],
|
auth_user_id=auth_user_id,
|
||||||
activity_date=gpx_data['start'],
|
activity_date=gpx_data['start'],
|
||||||
old_filename=filename,
|
old_filename=filename,
|
||||||
sport=params['sport_label']
|
sport=params['sport_label']
|
||||||
@ -316,7 +317,7 @@ def process_one_gpx_file(params, filename):
|
|||||||
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=params['auth_user_id'],
|
auth_user_id=auth_user_id,
|
||||||
activity_date=gpx_data['start'],
|
activity_date=gpx_data['start'],
|
||||||
extension='.png',
|
extension='.png',
|
||||||
sport=params['sport_label']
|
sport=params['sport_label']
|
||||||
@ -329,7 +330,7 @@ def process_one_gpx_file(params, filename):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
new_activity = create_activity(
|
new_activity = create_activity(
|
||||||
params['auth_user_id'], params['activity_data'], gpx_data)
|
params['user'], 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)
|
||||||
db.session.add(new_activity)
|
db.session.add(new_activity)
|
||||||
@ -373,9 +374,10 @@ def process_files(auth_user_id, activity_data, activity_file, folders):
|
|||||||
f"Sport id: {activity_data.get('sport_id')} does not exist",
|
f"Sport id: {activity_data.get('sport_id')} does not exist",
|
||||||
None
|
None
|
||||||
)
|
)
|
||||||
|
user = User.query.filter_by(id=auth_user_id).first()
|
||||||
|
|
||||||
common_params = {
|
common_params = {
|
||||||
'auth_user_id': auth_user_id,
|
'user': user,
|
||||||
'activity_data': activity_data,
|
'activity_data': activity_data,
|
||||||
'file_path': file_path,
|
'file_path': file_path,
|
||||||
'sport_label': sport.label,
|
'sport_label': sport.label,
|
||||||
|
Loading…
Reference in New Issue
Block a user