def create_journey_distances(user): for journey in Journey.all().filter('distance', 0.0).filter('user', user): points = Point.all().filter('journey', journey) journey.distance = sum( point.distance for point in points) if points.count() > 0 else 0.000001 journey.put()
def create_user_json(user): """ :param user: User :return: json """ create_journey_distances(user) user_travel_types = {} # Compute total distances for each travel type for journey in Journey.all().filter('user', user): # Get all points for each journey points = Point.all().filter('journey', journey) if points.count() > 0: # Saved distance made on a journey if journey.travel_type.name not in user_travel_types: user_travel_types[journey.travel_type.name] = { 'distance': 0.0, 'total': journey.travel_type.co2_exhausts, 'saved': TravelType.all().filter('name', 'Average').get().co2_exhausts - journey.travel_type.co2_exhausts } user_travel_types[journey.travel_type.name]['distance'] += round( journey.distance, 4) # Save all data total_co2 = total_saved = total_distance = 0.0 for travel_type in user_travel_types: # For each travel type travel_type = user_travel_types[travel_type] distance = travel_type['distance'] travel_type['total'] = round(travel_type['total'] * distance, 4) travel_type['saved'] = round(travel_type['saved'] * distance, 4) # Total total_distance += distance total_saved += travel_type['saved'] total_co2 += travel_type['total'] # Create json user_json = { 'facebook_id': user.facebook_id, 'name': '{0} {1}'.format(user.first_name, user.last_name), 'total': total_co2, 'saved': round(total_saved, 3), 'distance': round(total_distance, 3), 'travel_types': user_travel_types } return user_json
def create_user_json(user): """ :param user: User :return: json """ create_journey_distances(user) user_travel_types = {} # Compute total distances for each travel type for journey in Journey.all().filter('user', user): # Get all points for each journey points = Point.all().filter('journey', journey) if points.count() > 0: # Saved distance made on a journey if journey.travel_type.name not in user_travel_types: user_travel_types[journey.travel_type.name] = { 'distance': 0.0, 'total': journey.travel_type.co2_exhausts, 'saved': TravelType.all().filter('name', 'Average').get().co2_exhausts - journey.travel_type.co2_exhausts } user_travel_types[journey.travel_type.name]['distance'] += round(journey.distance, 4) # Save all data total_co2 = total_saved = total_distance = 0.0 for travel_type in user_travel_types: # For each travel type travel_type = user_travel_types[travel_type] distance = travel_type['distance'] travel_type['total'] = round(travel_type['total'] * distance, 4) travel_type['saved'] = round(travel_type['saved'] * distance, 4) # Total total_distance += distance total_saved += travel_type['saved'] total_co2 += travel_type['total'] # Create json user_json = { 'facebook_id': user.facebook_id, 'name': '{0} {1}'.format(user.first_name, user.last_name), 'total': total_co2, 'saved': round(total_saved, 3), 'distance': round(total_distance, 3), 'travel_types': user_travel_types } return user_json
def create_journey_distances(user): for journey in Journey.all().filter('distance', 0.0).filter('user', user): points = Point.all().filter('journey', journey) journey.distance = sum(point.distance for point in points) if points.count() > 0 else 0.000001 journey.put()