API: fetch one activity

This commit is contained in:
Sam 2018-05-01 21:26:17 +02:00
parent c553a5a8d3
commit be326ba294
2 changed files with 49 additions and 10 deletions

View File

@ -21,15 +21,7 @@ def get_activities(auth_user_id):
activities = Activity.query.all()
activities_list = []
for activity in activities:
activity_object = {
'id': activity.id,
'user_id': activity.user_id,
'sport_id': activity.sport_id,
'creation_date': activity.creation_date,
'activity_date': activity.activity_date,
'duration': activity.duration.seconds
}
activities_list.append(activity_object)
activities_list.append(activity.serialize())
response_object = {
'status': 'success',
'data': {
@ -39,6 +31,33 @@ def get_activities(auth_user_id):
return jsonify(response_object), 200
@activities_blueprint.route('/activities/<int:activity_id>', methods=['GET'])
@authenticate
def get_activity(auth_user_id, activity_id):
"""Get an activity"""
activity = Activity.query.filter_by(id=activity_id).first()
activities_list = []
if activity:
activities_list.append(activity.serialize())
response_object = {
'status': 'success',
'data': {
'activities': activities_list
}
}
code = 200
else:
response_object = {
'status': 'not found',
'data': {
'activities': activities_list
}
}
code = 404
return jsonify(response_object), code
@activities_blueprint.route('/activities', methods=['POST'])
@authenticate
def post_activity(auth_user_id):

View File

@ -50,7 +50,7 @@ class Activity(db.Model):
ave_speed = db.Column(db.Numeric(5, 2), nullable=True)
def __str__(self):
return self.sport.label + \
return str(self.sport_id) + \
" - " + self.activity_date.strftime('%Y-%m-%d')
def __init__(self, user_id, sport_id, activity_date, duration):
@ -58,3 +58,23 @@ class Activity(db.Model):
self.sport_id = sport_id
self.activity_date = activity_date
self.duration = duration
def serialize(self):
return {
"id": self.id,
"user_id": self.user_id,
"sport_id": self.sport_id,
"creation_date": self.creation_date,
"modification_date": self.modification_date,
"activity_date": self.activity_date,
"duration": self.duration.total_seconds(),
"pauses": self.pauses.total_seconds(),
"moving": self.moving.total_seconds(),
"distance": float(self.distance),
"min_alt": float(self.min_alt),
"max_alt": float(self.max_alt),
"descent": float(self.descent),
"ascent": float(self.ascent),
"max_speed": float(self.max_speed),
"ave_speed": float(self.ave_speed)
}