Exemple #1
0
def fetchStrava(code, request):
    if request.user == '' or request.user == None:
        request.user = '******'
    access_token = getAccessToken(code)
    activites_url = "https://www.strava.com/api/v3/athlete/activities"
    header = {'Authorization': 'Bearer ' + access_token}
    print("Fill database")
    i = 1
    while True:
        param = {'per_page': 200, 'page': i}
        current_activities = Activity.objects.filter(user=request.user)
        current_ids = set([i.strava_id for i in current_activities])
        print(len(current_activities))
        print(len(current_ids))
        strava_activities = requests.get(activites_url,
                                         headers=header,
                                         params=param).json()
        print("current batch -  " + str(len(strava_activities)))
        if len(strava_activities) == 0:
            break
        i += 1
        for strava_activity in strava_activities:
            if strava_activity["id"] not in current_ids:
                activity = Activity()
                activity.strava_id = strava_activity["id"]
                activity.user = request.user
                activity.title = strava_activity["name"]
                activity.activity_type = strava_activity["type"]
                activity.date = datetime.strptime(
                    strava_activity['start_date'][2:10], '%y-%m-%d')
                activity.timestamp = datetime.timestamp(activity.date)
                if strava_activity['has_heartrate']:
                    activity.heartrate = strava_activity['average_heartrate']
                    if 'suffer_score' in strava_activity:
                        activity.suffer = strava_activity['suffer_score']
                    else:
                        activity.suffer = 0
                else:
                    activity.heartrate = 0
                    activity.suffer = 0
                activity.distance = strava_activity['distance'] / 1000
                activity.moving_time = strava_activity['elapsed_time']
                activity.elevation = strava_activity['total_elevation_gain']
                activity.speed = strava_activity['average_speed']
                if activity.activity_type == "Run" or activity.activity_type == "Ride":
                    if strava_activity['workout_type'] == None:
                        activity.workout_type = 'niks'
                    else:
                        activity.workout_type = strava_activity['workout_type']
                activity.save()
def bulk_upload_activities(request):
    path = 'bulk_upload/activities.csv'
    with open(path, encoding="latin-1") as f:
        reader = csv.reader(f)
        for row in reader:
            activity = Activity()
            activity.name = row[0]
            activity.activity_type = row[1]
            activity.term = row[2]
            activity.location = row[3]
            activity.suburb = row[4]
            activity.postcode = row[5]
            activity.organiser = row[6]
            activity.contact_number = row[7]
            activity.description = row[8]
            activity.activity_date = row[9]
            activity.start_date = row[10]
            activity.end_date = row[11]
            activity.start_time = row[13]
            activity.end_time = row[14]
            activity.created_by = User.objects.get(username=row[15])

            # Image Upload
            if row[16] != '':
                file_dir = 'bulk_upload/images/' + row[16]
                image = open(file_dir, 'rb')
                image_file = File(image)
                activity.activity_img.save(row[16], image_file)
            # Flyer Upload
            if row[17] != '':
                file_dir = 'bulk_upload/flyers/' + row[17]
                flyer = open(file_dir, 'rb')
                flyer_file = File(flyer)
                activity.flyer.save(row[17], flyer_file)

            activity.min_age = row[18]
            activity.max_age = row[19]
            activity.background = row[20]
            activity.living_duration = row[21]
            activity.gender = row[22]
            if row[23] != '':
                activity.cost = row[23]
            if row[24] != '':
                activity.space = row[24]
            activity.cost_choice = row[25]
            activity.space_choice = row[26]
            activity.save()
        f.close()
    return HttpResponse("Success uploading activities.")