API: update record on activity creation
This commit is contained in:
parent
12595040d5
commit
fc09766c3a
@ -8,8 +8,8 @@ from sqlalchemy import exc
|
||||
from ..users.utils import authenticate, verify_extension
|
||||
from .models import Activity, Sport
|
||||
from .utils import (
|
||||
create_activity, create_segment, edit_activity, get_file_path,
|
||||
get_gpx_info, get_new_file_path
|
||||
check_records, create_activity, create_segment, edit_activity,
|
||||
get_file_path, get_gpx_info, get_new_file_path
|
||||
)
|
||||
|
||||
activities_blueprint = Blueprint('activities', __name__)
|
||||
@ -171,6 +171,11 @@ def post_activity(auth_user_id):
|
||||
auth_user_id, activity_data, gpx_data)
|
||||
db.session.add(new_activity)
|
||||
db.session.flush()
|
||||
|
||||
records = check_records(new_activity)
|
||||
for record in records:
|
||||
db.session.add(record)
|
||||
|
||||
for segment_data in gpx_data['segments']:
|
||||
new_segment = create_segment(new_activity.id, segment_data)
|
||||
db.session.add(new_segment)
|
||||
|
@ -2,7 +2,7 @@ import datetime
|
||||
|
||||
from mpwo_api import db
|
||||
from sqlalchemy.dialects import postgresql
|
||||
from sqlalchemy.ext.hybrid import hybrid_property
|
||||
from sqlalchemy.ext.hybrid import Comparator, hybrid_property
|
||||
from sqlalchemy.types import Enum
|
||||
|
||||
record_types = [
|
||||
@ -13,6 +13,11 @@ record_types = [
|
||||
]
|
||||
|
||||
|
||||
def convert_timedelta_to_integer(value):
|
||||
hours, minutes, seconds = str(value).split(':')
|
||||
return int(hours) * 3600 + int(minutes) * 60 + int(seconds)
|
||||
|
||||
|
||||
class Sport(db.Model):
|
||||
__tablename__ = "sports"
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||
@ -79,7 +84,11 @@ class Activity(db.Model):
|
||||
single_parent=True))
|
||||
records = db.relationship('Record',
|
||||
lazy=True,
|
||||
backref=db.backref('activities', lazy='joined'))
|
||||
cascade='all, delete',
|
||||
backref=db.backref(
|
||||
'activities',
|
||||
lazy='joined',
|
||||
single_parent=True))
|
||||
|
||||
def __str__(self):
|
||||
return '<Activity \'{}\' - {}>'.format(
|
||||
@ -162,6 +171,13 @@ class ActivitySegment(db.Model):
|
||||
}
|
||||
|
||||
|
||||
class ValueComparator(Comparator):
|
||||
def operate(self, op, other):
|
||||
if isinstance(other, datetime.timedelta):
|
||||
other = convert_timedelta_to_integer(other)
|
||||
return op(self.__clause_element__(), other)
|
||||
|
||||
|
||||
class Record(db.Model):
|
||||
__tablename__ = "records"
|
||||
__table_args__ = (db.UniqueConstraint(
|
||||
@ -215,13 +231,16 @@ class Record(db.Model):
|
||||
@value.setter
|
||||
def value(self, val):
|
||||
if self.record_type == 'LD':
|
||||
hours, minutes, seconds = str(val).split(':')
|
||||
self._value = int(hours) * 3600 + int(minutes) * 60 + int(seconds)
|
||||
self._value = convert_timedelta_to_integer(val)
|
||||
elif self.record_type in ['AS', 'MS']:
|
||||
self._value = int(val * 100)
|
||||
else: # 'FD'
|
||||
self._value = int(val * 1000)
|
||||
|
||||
@value.comparator
|
||||
def value(cls):
|
||||
return ValueComparator(cls._value)
|
||||
|
||||
def serialize(self):
|
||||
if self.value is None:
|
||||
value = None
|
||||
|
@ -7,7 +7,7 @@ from flask import current_app
|
||||
from mpwo_api import appLog
|
||||
from werkzeug.utils import secure_filename
|
||||
|
||||
from .models import Activity, ActivitySegment, Sport
|
||||
from .models import Activity, ActivitySegment, Record, Sport
|
||||
|
||||
|
||||
def update_activity_data(activity, gpx_data):
|
||||
@ -23,8 +23,35 @@ def update_activity_data(activity, gpx_data):
|
||||
return activity
|
||||
|
||||
|
||||
def check_records(activity):
|
||||
record_types_columns = {
|
||||
'AS': 'ave_speed', # 'Average speed'
|
||||
'FD': 'distance', # 'Farthest Distance'
|
||||
'LD': 'duration', # 'Longest Duration'
|
||||
'MS': 'max_speed', # 'Max speed'
|
||||
}
|
||||
records = []
|
||||
for record_type, column in record_types_columns.items():
|
||||
print('{} = {}'.format(record_type, str(column)))
|
||||
record = Activity.query.filter_by(
|
||||
user_id=activity.user_id,
|
||||
sport_id=activity.sport_id,
|
||||
).filter(Record.value > getattr(activity, column)).first()
|
||||
print(record)
|
||||
if not record:
|
||||
new_record = Record(
|
||||
user_id=activity.user_id,
|
||||
sport_id=activity.sport_id,
|
||||
activity=activity,
|
||||
record_type=record_type,
|
||||
)
|
||||
new_record.value = getattr(activity, column)
|
||||
records.append(new_record)
|
||||
return records
|
||||
|
||||
|
||||
def create_activity(
|
||||
auth_user_id, activity_data, gpx_data=None
|
||||
auth_user_id, activity_data, gpx_data=None
|
||||
):
|
||||
activity_date = gpx_data['start'] if gpx_data else datetime.strptime(
|
||||
activity_data.get('activity_date'), '%Y-%m-%d %H:%M')
|
||||
|
Loading…
Reference in New Issue
Block a user