Пример #1
0
def get_trip_results(request, trip_id):
    """Display results for a trip.   

    """
    trip = get_object_or_404(Trip, pk=trip_id, user=request.user)

    #get the trip search details
    (offer_radius, demand_radius, interval_min, interval_max, route,
        departure_point, arrival_point) = get_trip_search_details(request)
        
    if trip.offer and (route is None or route.geom_type != 'MultiLineString'):
        raise Http404

    today = datetime.date.today()

    if trip.demand:
        trip_offers = Trip.objects.get_offers(departure_point, arrival_point, demand_radius).exclude(pk=trip.id)
        trip_offers = trip_offers.get_mark_details()
        
        if trip.regular:
            trip_offers = trip_offers.filter_dows(trip.dows)
        else:
            trip_offers = trip_offers.filter_date_interval(trip.date, interval_min, interval_max)
        trip_offers = trip_offers.exclude_outdated(today)
        # exclude my trips ?
        if settings.EXCLUDE_MY_TRIPS:
            trip_offers = trip_offers.exclude(user=request.user)
        # ordering and limit
        trip_offers = trip_offers.order_by('-pourcentage_rank')[:_MAX_TRIP]
        trip_offers = sort_offers(trip_offers, trip.date, interval_min, interval_max, trip=trip)

    if trip.offer:
        trip_demands = Trip.objects.get_demands(route, get_direction_route(route), offer_radius).exclude(pk=trip.id)
        trip_demands = trip_demands.get_mark_details()
        if trip.regular:
            trip_demands = trip_demands.filter_dows(trip.dows)
        else:
            trip_demands = trip_demands.filter_date_interval(trip.date, interval_min, interval_max)
        trip_demands = trip_demands.exclude_outdated(today)
        # exclude my trips ?
        if settings.EXCLUDE_MY_TRIPS:
            trip_demands = trip_demands.exclude(user=request.user)
        # ordering and limit
        trip_demands = trip_demands.order_by('-pourcentage_rank')[:_MAX_TRIP]
        trip_demands = sort_demands(trip_demands, trip.date, interval_min, interval_max, trip=trip)

    response_dict = {
        'authenticated': request.user.is_authenticated(),
        'trip_offers': [get_trip_dict(t) for t in trip_offers] if trip.demand else None,
        'trip_demands': [get_trip_dict(t) for t in trip_demands] if trip.offer else None,
    }

    resp = HttpResponse()
    simplejson.dump(response_dict , resp, ensure_ascii=False, separators=(',',':'))
    return resp
Пример #2
0
def get_trips(request):
    """Get (JSON formatted) informations about all trips (offer or demand)

    """
    trip_search_type = get_trip_search_type(request.REQUEST)

    params = {"is_offer": trip_search_type == Trip.DEMAND, "is_demand": trip_search_type == Trip.OFFER}

    if trip_search_type == Trip.OFFER:
        # search for offers
        (
            offer_radius,
            date,
            interval_min,
            interval_max,
            departure_point,
            arrival_point,
        ) = get_trip_search_offer_details(request)
        params["offer_radius"] = offer_radius

    elif trip_search_type == Trip.DEMAND:
        # search for demands
        (
            demand_radius,
            date,
            interval_min,
            interval_max,
            route,
            departure_point,
            arrival_point,
        ) = get_trip_search_demand_details(request)
        params["demand_radius"] = demand_radius
        params["route"] = route

    params["date"] = date
    params["interval_min"] = interval_min
    params["interval_max"] = interval_max
    params["departure_point"] = departure_point
    params["arrival_point"] = arrival_point

    lib = LibCarpool()
    results = lib.get_trip_results(**params)
    trip_demands = results["trip_demands"]
    trip_offers = results["trip_offers"]
    trip = results["trip"]

    if trip_search_type == Trip.OFFER:
        trips = trip_offers
    else:
        trips = trip_demands

    response_dict = {"authenticated": request.user.is_authenticated(), "trips": [get_trip_dict(t) for t in trips]}

    resp = HttpResponse()
    simplejson.dump(response_dict, resp, ensure_ascii=False, separators=(",", ":"))
    return resp
Пример #3
0
def get_trips(request):
    """Get (JSON formatted) informations about all trips (offer or demand)

    """
    trip_search_type = get_trip_search_type(request.REQUEST)
    
    params = {
        'is_offer': trip_search_type == Trip.DEMAND,
        'is_demand': trip_search_type == Trip.OFFER,
    }
    
    if trip_search_type == Trip.OFFER:
        # search for offers
        (offer_radius, date, interval_min, interval_max, departure_point,
            arrival_point) = get_trip_search_offer_details(request)
        params['offer_radius'] = offer_radius
            
    elif trip_search_type == Trip.DEMAND:
        #search for demands
        (demand_radius, date, interval_min, interval_max, route, departure_point,
            arrival_point) = get_trip_search_demand_details(request)
        params['demand_radius'] = demand_radius
        params['route'] = route
        
    params['date'] = date
    params['interval_min'] = interval_min
    params['interval_max'] = interval_max
    params['departure_point'] = departure_point
    params['arrival_point'] = arrival_point

    lib = LibCarpool()
    results = lib.get_trip_results(**params)
    trip_demands = results['trip_demands']
    trip_offers = results['trip_offers']
    trip = results['trip']
    

    if trip_search_type == Trip.OFFER:
        trips = trip_offers
    else:
        trips = trip_demands

    response_dict = {
        'authenticated': request.user.is_authenticated(),
        'trips': [get_trip_dict(t) for t in trips],
    }

    resp = HttpResponse()
    simplejson.dump(response_dict , resp, ensure_ascii=False, separators=(',',':'))
    return resp
Пример #4
0
def get_trip_results(request, trip_id):
    """Display results for a trip.   

    """
    trip = get_object_or_404(Trip, pk=trip_id, user=request.user)

    # get the trip search details
    (
        offer_radius,
        demand_radius,
        interval_min,
        interval_max,
        route,
        departure_point,
        arrival_point,
    ) = get_trip_search_details(request)

    if trip.offer and (route is None or route.geom_type != "MultiLineString"):
        raise Http404

    today = datetime.date.today()

    if trip.demand:
        trip_offers = Trip.objects.get_offers(departure_point, arrival_point, demand_radius).exclude(pk=trip.id)
        trip_offers = trip_offers.get_mark_details()

        if trip.regular:
            trip_offers = trip_offers.filter_dows(trip.dows)
        else:
            trip_offers = trip_offers.filter_date_interval(trip.date, interval_min, interval_max)
        trip_offers = trip_offers.exclude_outdated(today)
        # exclude my trips ?
        if settings.EXCLUDE_MY_TRIPS:
            trip_offers = trip_offers.exclude(user=request.user)
        # ordering and limit
        trip_offers = trip_offers.order_by("-pourcentage_rank")[:_MAX_TRIP]
        trip_offers = sort_offers(trip_offers, trip.date, interval_min, interval_max, trip=trip)

    if trip.offer:
        trip_demands = Trip.objects.get_demands(route, get_direction_route(route), offer_radius).exclude(pk=trip.id)
        trip_demands = trip_demands.get_mark_details()
        if trip.regular:
            trip_demands = trip_demands.filter_dows(trip.dows)
        else:
            trip_demands = trip_demands.filter_date_interval(trip.date, interval_min, interval_max)
        trip_demands = trip_demands.exclude_outdated(today)
        # exclude my trips ?
        if settings.EXCLUDE_MY_TRIPS:
            trip_demands = trip_demands.exclude(user=request.user)
        # ordering and limit
        trip_demands = trip_demands.order_by("-pourcentage_rank")[:_MAX_TRIP]
        trip_demands = sort_demands(trip_demands, trip.date, interval_min, interval_max, trip=trip)

    response_dict = {
        "authenticated": request.user.is_authenticated(),
        "trip_offers": [get_trip_dict(t) for t in trip_offers] if trip.demand else None,
        "trip_demands": [get_trip_dict(t) for t in trip_demands] if trip.offer else None,
    }

    resp = HttpResponse()
    simplejson.dump(response_dict, resp, ensure_ascii=False, separators=(",", ":"))
    return resp