def get_ride(ride): """ Get ride from database or fetch from strava. """ try: return Ride.get(strava_id=ride) except Ride.DoesNotExist: return fetch_ride(ride)
def fetch_ride(ride): """ Fetch and store a single ride from strava. Argument is either Ride instance or strava id. """ data = load_json("http://www.strava.com/api/v1/rides/%s" % ride)['ride'] instance = Ride.get_or_create(strava_id=ride).populate_from_dict(data) instance.save() return instance
def get_club_rides_for_week(club, week_id): club = get_club(club) return Ride.select().join(Athlete).join(ClubAthlete).where(Ride.week==week_id, ClubAthlete.club==club)
def construct_ride(data): instance = Ride.get_or_create(strava_id=data['id']).populate_from_dict(data) instance.save() return instance
def get_orphan_rides(club): for athlete in get_athletes(club): db_ids = set(r.strava_id for r in Ride.select(Ride.strava_id).where(Ride.athlete == athlete)) strava_ids = set(fetch_athlete_ride_ids(athlete)) for id in db_ids.difference(strava_ids): yield Ride.get(Ride.strava_id == id)