mast/integrations/utils.py
def create_activity_from_strava_json(activity: dict, user: User, strava_activity_id: int) -> Activity:
    """
    Processes json from strava *DetailedActivity* and creates Activity instance
    :param activity: json from strava *strava.com/activity/id*
    :param user: User that uploaded the activity
    :param strava_activity_id: strava activity id
    :return: new Activity ready to save into database
    """

    distance = activity['distance']     # meters
    time_in_secs = activity['moving_time']  # seconds
    total_time = _get_time(time_in_secs)     # time(H:M:S)
    elevation = activity['total_elevation_gain'] if not None else 0     # meters
    activity_type = _get_activity_type(activity['type'])  # https://developers.strava.com/docs/reference/#api-models-ActivityType
    name = activity['name'][:30] if not None else ''    # string
    pace = _get_time(round(time_in_secs / distance * 1000))     # minutes per km
    activity_date = datetime.strptime(activity['start_date'],'%Y-%m-%dT%H:%M:%SZ')  # datetime
    score = _get_score(distance, elevation, pace, user, activity_type)     # int

    # check activity constrains
    if activity_type is None:    # unsupported ActivityType
        strava_logger.info(
            f'Activity :{activity_type} is not supported.')
        return None

    if _satisfy_distance_constrains(distance/1000, activity_type):     # Activity is not long enough to be counted
        strava_logger.info(
            f'Activity :{activity_type} of length {distance} meters does not satisfy distance constrains.')
        return None

    # creation of the activity
    new_activity = Activity()
    new_activity.datetime = activity_date
    new_activity.distance = distance / 1000
    new_activity.duration = total_time
    new_activity.average_duration_per_km = pace
    new_activity.type = activity_type
    new_activity.user_id = user.id
    new_activity.name = name
    new_activity.elevation = elevation
    new_activity.strava_id = strava_activity_id
    new_activity.score = score

    return new_activity

    Processes json from strava *DetailedActivity* and creates Activity instance
    :param activity: json from strava *strava.com/activity/id*
    :param user: User that uploaded the activity
    :param strava_activity_id: strava activity id
    :return: new Activity ready to save into database
    
Processes json from strava *DetailedActivity* and creates Activity instance
Processes json from strava * DetailedActivity * and creates Activity instance
def create_activity_from_strava_json(activity: dict, user: User, strava_activity_id: int) -> Activity:
    distance = activity['distance']     
    time_in_secs = activity['moving_time']  
    total_time = _get_time(time_in_secs)     
    elevation = activity['total_elevation_gain'] if not None else 0     
    activity_type = _get_activity_type(activity['type'])  
    name = activity['name'][:30] if not None else ''    
    pace = _get_time(round(time_in_secs / distance * 1000))     
    activity_date = datetime.strptime(activity['start_date'],'%Y-%m-%dT%H:%M:%SZ')  
    score = _get_score(distance, elevation, pace, user, activity_type)     
    if activity_type is None:    
        strava_logger.info(
            f'Activity :{activity_type} is not supported.')
        return None
    if _satisfy_distance_constrains(distance/1000, activity_type):     
        strava_logger.info(
            f'Activity :{activity_type} of length {distance} meters does not satisfy distance constrains.')
        return None
    new_activity = Activity()
    new_activity.datetime = activity_date
    new_activity.distance = distance / 1000
    new_activity.duration = total_time
    new_activity.average_duration_per_km = pace
    new_activity.type = activity_type
    new_activity.user_id = user.id
    new_activity.name = name
    new_activity.elevation = elevation
    new_activity.strava_id = strava_activity_id
    new_activity.score = score
    return new_activity
def create_activity_from_strava_json ( activity : dict , user : User , strava_activity_id : int ) -> Activity : distance = activity [ 'distance' ] time_in_secs = activity [ 'moving_time' ] total_time = _get_time ( time_in_secs ) elevation = activity [ 'total_elevation_gain' ] if not None else 0 activity_type = _get_activity_type ( activity [ 'type' ] ) name = activity [ 'name' ] [ : 30 ] if not None else '' pace = _get_time ( round ( time_in_secs / distance * 1000 ) ) activity_date = datetime . strptime ( activity [ 'start_date' ] , '%Y-%m-%dT%H:%M:%SZ' ) score = _get_score ( distance , elevation , pace , user , activity_type ) if activity_type is None : strava_logger . info ( f'Activity :{activity_type} is not supported.' ) return None if _satisfy_distance_constrains ( distance / 1000 , activity_type ) : strava_logger . info ( f'Activity :{activity_type} of length {distance} meters does not satisfy distance constrains.' ) return None new_activity = Activity ( ) new_activity . datetime = activity_date new_activity . distance = distance / 1000 new_activity . duration = total_time new_activity . average_duration_per_km = pace new_activity . type = activity_type new_activity . user_id = user . id new_activity . name = name new_activity . elevation = elevation new_activity . strava_id = strava_activity_id new_activity . score = score return new_activity
Processes json from strava *DetailedActivity* and creates Activity instance
Processes json from strava * DetailedActivity * and creates Activity instance
"""
    Processes json from strava *DetailedActivity* and creates Activity instance
    :param activity: json from strava *strava.com/activity/id*
    :param user: User that uploaded the activity
    :param strava_activity_id: strava activity id
    :return: new Activity ready to save into database
    """ # meters # seconds # time(H:M:S) # meters # https://developers.strava.com/docs/reference/#api-models-ActivityType # string # minutes per km # datetime # int # check activity constrains # unsupported ActivityType # Activity is not long enough to be counted # creation of the activity
new Activity ready to save into database
new Activity ready to save into database
activity
dict
json from strava *strava.com/activity/id
json from strava * strava . com / activity / id
user
User
User that uploaded the activity
User that uploaded the activity
strava_activity_id
int
strava activity id
strava activity id