API: refactor
This commit is contained in:
parent
027fa8f699
commit
0a1a2082f4
@ -23,6 +23,21 @@ class ActivityException(Exception):
|
|||||||
self.e = e
|
self.e = e
|
||||||
|
|
||||||
|
|
||||||
|
def get_datetime_with_tz(auth_user_id, 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_tz = None
|
||||||
|
if user.timezone and not gpx_data:
|
||||||
|
user_tz = pytz.timezone(user.timezone)
|
||||||
|
activity_date_tz = user_tz.localize(activity_date)
|
||||||
|
if not gpx_data:
|
||||||
|
# make datetime 'naive' like in gpx file
|
||||||
|
activity_date = activity_date_tz.astimezone(pytz.utc)
|
||||||
|
activity_date = activity_date.replace(tzinfo=None)
|
||||||
|
|
||||||
|
return activity_date_tz, activity_date
|
||||||
|
|
||||||
|
|
||||||
def update_activity_data(activity, gpx_data):
|
def update_activity_data(activity, gpx_data):
|
||||||
"""activity could be a complete activity or an activity segment"""
|
"""activity could be a complete activity or an activity segment"""
|
||||||
activity.pauses = gpx_data['stop_time']
|
activity.pauses = gpx_data['stop_time']
|
||||||
@ -39,19 +54,10 @@ def update_activity_data(activity, gpx_data):
|
|||||||
def create_activity(
|
def create_activity(
|
||||||
auth_user_id, activity_data, gpx_data=None
|
auth_user_id, activity_data, gpx_data=None
|
||||||
):
|
):
|
||||||
user = User.query.filter_by(id=auth_user_id).first()
|
|
||||||
|
|
||||||
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 = None
|
activity_date_tz, activity_date = get_datetime_with_tz(
|
||||||
# activity date in gpx are directly in UTC
|
auth_user_id, activity_date, gpx_data)
|
||||||
if user.timezone and not gpx_data:
|
|
||||||
user_tz = pytz.timezone(user.timezone)
|
|
||||||
activity_date_tz = user_tz.localize(activity_date)
|
|
||||||
if not gpx_data:
|
|
||||||
# make datetime 'naive' like in gpx file
|
|
||||||
activity_date = activity_date_tz.astimezone(pytz.utc)
|
|
||||||
activity_date = activity_date.replace(tzinfo=None)
|
|
||||||
|
|
||||||
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'))
|
||||||
@ -113,20 +119,17 @@ def edit_activity(activity, activity_data, auth_user_id):
|
|||||||
if activity_data.get('activity_date'):
|
if activity_data.get('activity_date'):
|
||||||
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')
|
||||||
user = User.query.filter_by(id=auth_user_id).first()
|
_, activity.activity_date = get_datetime_with_tz(
|
||||||
if user.timezone:
|
auth_user_id, activity_date)
|
||||||
# make datetime 'naive' like in gpx file
|
|
||||||
user_tz = pytz.timezone(user.timezone)
|
|
||||||
activity_date_tz = user_tz.localize(activity_date)
|
|
||||||
activity_date = activity_date_tz.astimezone(pytz.utc)
|
|
||||||
activity_date = activity_date.replace(tzinfo=None)
|
|
||||||
activity.activity_date = activity_date
|
|
||||||
if activity_data.get('duration'):
|
if activity_data.get('duration'):
|
||||||
activity.duration = timedelta(
|
activity.duration = timedelta(
|
||||||
seconds=activity_data.get('duration'))
|
seconds=activity_data.get('duration'))
|
||||||
activity.moving = activity.duration
|
activity.moving = activity.duration
|
||||||
|
|
||||||
if activity_data.get('distance'):
|
if activity_data.get('distance'):
|
||||||
activity.distance = activity_data.get('distance')
|
activity.distance = activity_data.get('distance')
|
||||||
|
|
||||||
activity.ave_speed = (None if activity.duration.seconds == 0
|
activity.ave_speed = (None if activity.duration.seconds == 0
|
||||||
else float(activity.distance) /
|
else float(activity.distance) /
|
||||||
(activity.duration.seconds / 3600))
|
(activity.duration.seconds / 3600))
|
||||||
|
Loading…
Reference in New Issue
Block a user