Beispiel #1
0
    def put(self, username, goal_id, start_at, duration, start_value,
            target_value, category_id, **kwargs):
        profile = get_auth_profile_or_abort(username, "goal")
        goal = profile.get_goal_by_id(goal_id)

        if goal is None:
            report_error_and_abort(422, "goal", "Goal not found")

        if goal.end_at < dt.datetime.utcnow():
            report_error_and_abort(422, "goal", "Goal already expired")

        category = GoalCategory.get_by_id(category_id)

        if category is None:
            report_error_and_abort(422, "goal", "Goal category not found")

        if start_value == target_value:
            report_error_and_abort(422, "goal",
                                   "Goal target value equals start value")

        utc_start_at = start_at - start_at.utcoffset()

        goal.category = category
        goal.start_at = utc_start_at
        goal.end_at = utc_start_at + dt.timedelta(days=duration)
        goal.start_value = start_value
        goal.target_value = target_value

        try:
            goal.save()
        except:
            db.session.rollback()
            report_error_and_abort(500, "goal", "Unable to update goal")

        return goal, 200
Beispiel #2
0
    def put(self,
            username,
            workout_id,
            name,
            start_at,
            distance,
            duration,
            category_id,
            climb=None,
            edited=None):
        profile = get_auth_profile_or_abort(username, "workout")
        workout = profile.get_workout_by_id(workout_id)

        if workout is None:
            report_error_and_abort(422, "workout", "Workout not found")

        category = WorkoutCategoryModel.get_by_id(category_id)

        if category is None:
            report_error_and_abort(422, "workout",
                                   "Workout category not found")

        if name is None or name == "":
            name = category.name

        utc_start_at = start_at - start_at.utcoffset()
        now = dt.datetime.utcnow().replace(tzinfo=pytz.UTC)

        if utc_start_at > now:
            report_error_and_abort(422, "workout",
                                   "Workout start time is in the future")

        # remove data from goal before registering updated
        try:
            remove_workout_data_from_goals(profile, workout)
        except:
            db.session.rollback()
            report_error_and_abort(500, "workout", "Unable to update workout")

        # update category
        workout.category = category
        workout.name = name
        workout.start_at = utc_start_at
        workout.distance = distance
        workout.duration = duration

        if climb is not None:
            workout.climb = climb

        if edited is not None:
            workout.edited = edited

        try:
            workout.save()
            add_workout_data_to_goals(profile, workout)
        except:
            db.session.rollback()
            report_error_and_abort(500, "workout", "Unable to update workout")

        return workout, 200
Beispiel #3
0
    def post(self, username, start_at, duration, start_value, target_value,
             category_id):
        profile = get_auth_profile_or_abort(username, "goal")
        category = GoalCategory.get_by_id(category_id)
        current_value = 0

        if category is None:
            report_error_and_abort(422, "goal", "Goal category not found")

        utc_start_at = start_at - start_at.utcoffset()
        utc_end_at = utc_start_at + dt.timedelta(days=duration)
        now = dt.datetime.utcnow().replace(tzinfo=pytz.UTC)

        if utc_end_at < now:
            report_error_and_abort(422, "goal", "Goal already expired")

        if start_value == target_value:
            report_error_and_abort(422, "goal",
                                   "Goal target value equals start value")

        if category.name == "Weight loss":
            current_value = start_value

        try:
            new_goal = Goal(profile.id, category, utc_start_at, utc_end_at,
                            start_value, target_value, current_value)
            new_goal.save()
        except:
            db.session.rollback()
            report_error_and_abort(500, "goal", "Unable to create goal.")

        return new_goal, 200, {
            'Location': '{}/{}'.format(request.path, new_goal.id)
        }
Beispiel #4
0
    def get(self, username, goal_id):
        profile = get_auth_profile_or_abort(username, "goal")
        goal = profile.get_goal_by_id(goal_id)

        if goal is None:
            report_error_and_abort(404, "goal", "Goal not found.")

        return goal
Beispiel #5
0
    def get(self, username, workout_id):
        profile = get_auth_profile_or_abort(username, "workout")
        workout = profile.get_workout_by_id(workout_id)

        if workout is None:
            report_error_and_abort(404, "workout", "Workout not found.")

        if workout.category.supports_gps_data:
            workout.register_extended_data()
        return workout
Beispiel #6
0
    def get(self, username, goal_id=None, limit=10, offset=0):
        profile = get_auth_profile_or_abort(username, "workout")

        if goal_id is None:
            return profile.get_workouts(limit, offset)

        goal = profile.get_goal_by_id(goal_id)

        if goal is None:
            report_error_and_abort(422, "workout", "Goal not found.")

        return Workout.get_workouts_for_goal(goal)
Beispiel #7
0
    def get(self, username, filter='', **kwargs):
        profile = get_auth_profile_or_abort(username, "goal")
        goals = None

        if filter == 'expired':
            goals = profile.get_expired_goals()
        elif filter == 'future':
            goals = profile.get_future_goals()
        elif filter == 'completed':
            goals = profile.get_completed_goals()
        elif filter == 'incompleted':
            goals = profile.get_incompleted_goals()
        else:
            goals = profile.get_active_goals()
        return goals
Beispiel #8
0
    def post(self, username):
        profile = get_auth_profile_or_abort(username, "polar")
        polar_user = profile.get_polar_data()

        if polar_user is None:
            polar_user = PolarUser(profile.id, profile.username)

        if not polar_user.has_valid_access_token():
            polar_user.generate_state_code()
        else:
            polar_user.state = None

        try:
            polar_user.updated_at = dt.datetime.utcnow()
            polar_user.save()
        except:
            db.session.rollback()
            report_error_and_abort(500, "polar", "Failed to retrieve data.")

        return polar_user
Beispiel #9
0
    def get(self, username):
        profile = get_auth_profile_or_abort(username, "polar")
        polar_user = profile.get_polar_data()

        if polar_user is None:
            return {}, 204  # no data

        if not polar_user.has_valid_access_token():
            if polar_user.state is None:
                polar_user.generate_state_code()
        else:
            polar_user.state = None

        try:
            polar_user.save()
        except:
            db.session.rollback()
            report_error_and_abort(500, "polar", "Failed to retrieve data.")

        return polar_user
Beispiel #10
0
    def post(self,
             username,
             name,
             start_at,
             distance,
             duration,
             category_id,
             climb=0,
             edited=False):
        profile = get_auth_profile_or_abort(username, "workout")
        category = WorkoutCategoryModel.get_by_id(category_id)

        if category is None:
            report_error_and_abort(422, "workout",
                                   "Workout category not found")

        if name is None or name == "":
            name = category.name

        utc_start_at = start_at - start_at.utcoffset()
        now = dt.datetime.utcnow().replace(tzinfo=pytz.UTC)

        if utc_start_at > now:
            report_error_and_abort(422, "workout",
                                   "Workout start time is in the future")

        try:
            new_workout = Workout(profile.id, category, name, utc_start_at,
                                  distance, duration, climb, None, edited)
            new_workout.save()
            add_workout_data_to_goals(profile, new_workout)
        except:
            db.session.rollback()
            report_error_and_abort(500, "workout", "Unable to create workout.")

        return new_workout, 200, {
            'Location': '{}/{}'.format(request.path, new_workout.id)
        }
Beispiel #11
0
    def post(self, username, category_id):
        profile = get_auth_profile_or_abort(username, "workout")
        category = WorkoutCategoryModel.get_by_id(category_id)

        if category is None:
            report_error_and_abort(422, "workout",
                                   "Workout category not found")

        if request.files is None or len(
                request.files) != 1 or request.files["gpxfile"] is None:
            report_error_and_abort(422, "workout",
                                   "Workout file not provided.")

        uploaded_file = request.files["gpxfile"]

        if not is_valid_workout_filename(uploaded_file.filename):
            report_error_and_abort(422, "workout", "Workout filename invalid.")

        tmp_filepath = save_uploaded_file_or_abort(uploaded_file,
                                                   profile.username)

        # create object with temporary data and use it to parse workout file
        new_workout = Workout(profile.id, category, category.name,
                              dt.datetime.utcnow(), 0, 1, 0, tmp_filepath,
                              False)
        new_workout.register_extended_data()
        parsed_summary = new_workout.extended_summary

        if parsed_summary is None:
            remove_uploaded_file(tmp_filepath)
            db.session.rollback()
            report_error_and_abort(422, "workout",
                                   "Failed to parse uploaded file")

        new_workout.name = get_autogenerated_workout_name(
            parsed_summary.latitude, parsed_summary.longitude,
            new_workout.category_name)
        new_workout.start_at = parsed_summary.time
        new_workout.duration = parsed_summary.duration

        if category.supports_gps_data:
            new_workout.distance = parsed_summary.distance
            new_workout.climb = parsed_summary.elevation

        workout_filepath = None
        try:
            new_workout.save()
            workout_filepath = rename_uploaded_file(tmp_filepath,
                                                    profile.username,
                                                    new_workout.id)
            new_workout.resource_path = workout_filepath
            new_workout.save()
            add_workout_data_to_goals(profile, new_workout)
        except:
            remove_uploaded_file(tmp_filepath)
            remove_uploaded_file(workout_filepath)
            db.session.rollback()
            report_error_and_abort(500, "workout",
                                   "Unable to create workout from file.")

        return new_workout, 200, {
            'Location': '{}/{}'.format(request.path, new_workout.id)
        }