示例#1
0
def JourneyBatch(request):
    user = User.all().filter('facebook_id',
                             request.session['facebook_id']).get()

    for point in json.loads(request.body):
        # Date sent
        date = datetime.fromtimestamp(int(
            point['date'])).replace(tzinfo=pytz.UTC)
        if 'journey' in point:
            if point['journey'] != 'Stop':
                journey = Journey(user=user,
                                  travel_type=TravelType.all().filter(
                                      'name', point['journey']).get(),
                                  start_date=date)
                journey.put()
                logging.info('Journey started for ' + point['journey'])
                user.active_trip_id = journey.key().id()
            else:
                logging.info('Journey ended')
                journey = Journey.get_by_id(user.active_trip_id)
                journey.end_date = date
                journey.put()
                # Remove active trip
                user.active_trip_id = -1
            user.put()

        elif 'distance' in point and user.active_trip_id != -1:
            # Get the current journey and save point
            Point(journey=Journey.get_by_id(user.active_trip_id),
                  latitude=float(point['x']),
                  longitude=float(point['y']),
                  speed=float(point['speed']),
                  time=date,
                  distance=float(point['distance'])).put()
    return HttpResponse()
示例#2
0
    def get(self, request, *args, **kwargs):
        """
        :return: json
        """
        save_to_database()
        response = {}
        if 'travel_type' in kwargs:
            # If travel type exists return only average co2 produce
            travel_type = TravelType.all().filter('name', kwargs['travel_type'].capitalize())
            if travel_type.count() > 0:
                response = to_dict(travel_type.get())
        else:
            for travel_type in TravelType.all():
                response[travel_type.name] = travel_type.co2_exhausts

        logging.info('Getting travel types')
        return HttpResponse(json.dumps(response), content_type="application/json")
示例#3
0
    def get(self, request, *args, **kwargs):
        """
        :return: json
        """
        save_to_database()
        response = {}
        if 'travel_type' in kwargs:
            # If travel type exists return only average co2 produce
            travel_type = TravelType.all().filter(
                'name', kwargs['travel_type'].capitalize())
            if travel_type.count() > 0:
                response = to_dict(travel_type.get())
        else:
            for travel_type in TravelType.all():
                response[travel_type.name] = travel_type.co2_exhausts

        logging.info('Getting travel types')
        return HttpResponse(json.dumps(response),
                            content_type="application/json")
示例#4
0
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
示例#5
0
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
示例#6
0
def JourneyBatch(request):
    user = User.all().filter('facebook_id', request.session['facebook_id']).get()

    for point in json.loads(request.body):
        # Date sent
        date = datetime.fromtimestamp(int(point['date'])).replace(tzinfo=pytz.UTC)
        if 'journey' in point:
            if point['journey'] != 'Stop':
                journey = Journey(
                    user=user,
                    travel_type=TravelType.all().filter('name', point['journey']).get(),
                    start_date=date
                )
                journey.put()
                logging.info('Journey started for ' + point['journey'])
                user.active_trip_id = journey.key().id()
            else:
                logging.info('Journey ended')
                journey = Journey.get_by_id(user.active_trip_id)
                journey.end_date = date
                journey.put()
                # Remove active trip
                user.active_trip_id = -1
            user.put()

        elif 'distance' in point and user.active_trip_id != -1:
            # Get the current journey and save point
            Point(
                journey=Journey.get_by_id(user.active_trip_id),
                latitude=float(point['x']),
                longitude=float(point['y']),
                speed=float(point['speed']),
                time=date,
                distance=float(point['distance'])
            ).put()
    return HttpResponse()
示例#7
0
def save_to_database():
    # Save all travel types
    for travel_type in travel_types:
        if TravelType.all().filter('name', travel_type).count() == 0:
            TravelType(name=travel_type, co2_exhausts=travel_types.get(travel_type)).put()
示例#8
0
def save_to_database():
    # Save all travel types
    for travel_type in travel_types:
        if not TravelType.objects.filter(name=travel_type).exists():
            TravelType(name=travel_type,
                       co2_exhausts=travel_types.get(travel_type)).save()