コード例 #1
0
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()
コード例 #2
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()
コード例 #3
0
ファイル: views.py プロジェクト: premik91/ecoTravel
 def get_context_data(self, **kwargs):
     context = super(JourneyMap, self).get_context_data(**kwargs)
     journey = Journey.get_by_id(long(kwargs['journey_id']))
     points = Point.all().filter('journey', journey)
     if points.count() > 0:
         context.update({
             'journey_points': points.order('-time'),
         })
     return context
コード例 #4
0
 def get_context_data(self, **kwargs):
     context = super(JourneyMap, self).get_context_data(**kwargs)
     journey = Journey.get_by_id(long(kwargs['journey_id']))
     points = Point.all().filter('journey', journey)
     if points.count() > 0:
         context.update({
             'journey_points': points.order('-time'),
         })
     return context
コード例 #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
ファイル: views.py プロジェクト: premik91/ecoTravel
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
コード例 #7
0
ファイル: views.py プロジェクト: premik91/ecoTravel
def TripBatch(request):
    user_profile = UserProfile.objects.get(user=request.user)
    for point in json.loads(request.body):
        # Start new journey
        if 'journey' in point:
            if point['journey'] == 'Stop':
                print 'Journey ended'
                journey = Journey.objects.get(id=user_profile.active_trip_id)
                # End date
                date = datetime.fromtimestamp(int(point['date']))
                date = date.replace(tzinfo=pytz.UTC)
                journey.end_date = date
                journey.save()
                # Remove active trip
                user_profile.active_trip_id = -1
            else:
                # Start date
                date = datetime.fromtimestamp(int(point['date']))
                date = date.replace(tzinfo=pytz.UTC)
                journey = Journey(
                    user_profile=user_profile,
                    travel_type=TravelType.objects.get(name=point['journey']),
                    start_date=date)
                journey.save()
                print 'Journey started for ' + point['journey']
                user_profile.active_trip_id = journey.id
            user_profile.save()
        elif 'distance' in point and user_profile.active_trip_id > -1:
            # Get the current journey and save point
            date = datetime.fromtimestamp(int(point['date']))
            date = date.replace(tzinfo=pytz.UTC)
            Point(journey=Journey.objects.get(id=user_profile.active_trip_id),
                  latitude=float(point['x']),
                  longitude=float(point['y']),
                  speed=float(point['speed']),
                  time=date,
                  distance=float(point['distance'])).save()
    return HttpResponse()
コード例 #8
0
ファイル: views.py プロジェクト: premik91/ecoTravel
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()