Beispiel #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
Beispiel #2
0
    def list_trips_by_type(self, offer_details, demand_details, user,
                           trip_type, maximum):
        """Return a list of trips, for the trip_type given in parameter
        
        If trip_type is not contained in TRIPOFFER or TRIPDEMAND, raise a 
        InvalidTripType exception
        
        """
        if trip_type == self.TRIPOFFER:
            # search for offers
            (radius, date, interval_min, interval_max, departure_point,
             arrival_point) = offer_details

        elif trip_type == self.TRIPDEMAND:
            #search for demands
            (radius, date, interval_min, interval_max, route, departure_point,
             arrival_point) = demand_details
            if route is None or route.geom_type != 'LineString':
                raise InvalidGeometry(
                    "the demand geometry type must be a LineString")
        else:
            raise InvalidTripType()

        if trip_type == self.TRIPOFFER:
            trips = Trip.objects.get_offers(departure_point, arrival_point,
                                            radius)
        else:
            trips = Trip.objects.get_demands(route, get_direction_route(route),
                                             radius)
        trips = trips.get_mark_details()

        # adding common date clause
        trips = trips.filter_date_interval(date, interval_min,
                                           interval_max).exclude_outdated()
        # exclude my trips ?
        if settings.EXCLUDE_MY_TRIPS and user.is_authenticated():
            trips = trips.exclude(user=user)
        # ordering and limit
        trips = trips.order_by('-pourcentage_rank')[:maximum]

        if trip_search_type == self.TRIPOFFER:
            trips = sort_offers(trips, date, interval_min, interval_max)
        else:
            trips = sort_demands(trips, date, interval_min, interval_max)

        return trips
Beispiel #3
0
    def list_trips_by_type(self, offer_details, demand_details, user, trip_type, maximum):
        """Return a list of trips, for the trip_type given in parameter
        
        If trip_type is not contained in TRIPOFFER or TRIPDEMAND, raise a 
        InvalidTripType exception
        
        """
        if trip_type == self.TRIPOFFER:
            # search for offers
            (radius, date, interval_min, interval_max, departure_point,
                arrival_point) = offer_details
                
        elif trip_type == self.TRIPDEMAND:
            #search for demands
            (radius, date, interval_min, interval_max, route, departure_point,
                arrival_point) = demand_details
            if route is None or route.geom_type != 'LineString':
                raise InvalidGeometry("the demand geometry type must be a LineString")
        else:
            raise InvalidTripType()
            
        if trip_type == self.TRIPOFFER:
            trips = Trip.objects.get_offers(departure_point, arrival_point, radius)
        else:
            trips = Trip.objects.get_demands(route, get_direction_route(route), radius)
        trips = trips.get_mark_details()

        # adding common date clause
        trips = trips.filter_date_interval(date, interval_min, interval_max).exclude_outdated()
        # exclude my trips ?
        if settings.EXCLUDE_MY_TRIPS and user.is_authenticated():
            trips = trips.exclude(user=user)
        # ordering and limit
        trips = trips.order_by('-pourcentage_rank')[:maximum]

        if trip_search_type == self.TRIPOFFER:
            trips = sort_offers(trips, date, interval_min, interval_max)
        else:
            trips = sort_demands(trips, date, interval_min, interval_max)
        
        return trips
Beispiel #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
Beispiel #5
0
    def get_trip_results(self,
                         is_offer=None,
                         is_demand=None,
                         offer_radius=None,
                         demand_radius=None,
                         date=None,
                         interval_min=None,
                         interval_max=None,
                         is_regular=None,
                         dows=None,
                         offer_route=None,
                         departure_point=None,
                         arrival_point=None,
                         max_trips=None,
                         trip_id=None,
                         user=None):
        """Return a list containing the trip object, trip_offers and 
        trip_demands matching given criterias.
        
        """
        # vars initialisation
        max_trips = max_trips or self.MAX_TRIPS
        trip = None
        trip_offers = None
        trip_demands = None

        if trip_id and user:
            # return matching trips with an already existing search
            try:
                trip = Trip.objects.get(pk=int(trip_id), user=user)
            except Trip.DoesNotExist:
                raise InvalidUser(user)

            if is_offer or trip.offer:
                is_offer = is_offer or True
                offer_radius = offer_radius or trip.offer.radius
                offer_route = offer_route or trip.offer.route

            if (is_demand or trip.demand):
                is_demand = is_demand or True
                demand_radius = demand_radius or trip.demand.radius
            is_regular = is_regular or trip.regular
            date = date or trip.date
            dows = dows or trip.dows
            interval_min = interval_min or trip.interval_min
            interval_max = interval_max or trip.interval_max
            departure_point = departure_point or trip.departure_point
            arrival_point = arrival_point or trip.arrival_point

        if is_offer:
            offer_radius = offer_radius or self.OFFER_RADIUS
            if trip_id:
                if offer_route is None or offer_route.geom_type != 'MultiLineString':
                    raise InvalidGeometry("The trip offer route geometry is " \
                    "required and need to be a 'MultiLineString'. You gave us " \
                    "a '%(type)s' instead." % {
                        'type': "None" if offer_route is None else offer_route.geom_type
                    })
            else:
                if offer_route is None or offer_route.geom_type != 'LineString':
                    raise InvalidGeometry("The trip offer route geometry is " \
                    "required and need to be a 'LineString'. You gave us " \
                    "a '%(type)s' instead." % {
                        'type': "None" if offer_route is None else offer_route.geom_type
                    })

        if is_demand:
            demand_radius = demand_radius or self.DEMAND_RADIUS

        interval_min = interval_min or self.INTERVAL_MIN
        interval_max = interval_max or self.INTERVAL_MAX
        today = datetime.date.today()
        date = date or today

        if is_demand:
            trip_offers = Trip.objects.get_offers(departure_point,
                                                  arrival_point, demand_radius)
            if trip_id:
                trip_offers = trip_offers.exclude(pk=trip_id)
            trip_offers = trip_offers.get_mark_details()

            if is_regular:
                trip_offers = trip_offers.filter_dows(dows)
            else:
                trip_offers = trip_offers.filter_date_interval(
                    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=user)
            # ordering and limit
            trip_offers = trip_offers.order_by('-pourcentage_rank')[:max_trips]
            trip_offers = sort_offers(trip_offers,
                                      date,
                                      interval_min,
                                      interval_max,
                                      trip=trip)

        if is_offer:
            trip_demands = Trip.objects.get_demands(
                offer_route, get_direction_route(offer_route), offer_radius)
            if trip_id:
                trip_demands = trip_demands.exclude(pk=trip_id)
            trip_demands = trip_demands.get_mark_details()
            if is_regular:
                trip_demands = trip_demands.filter_dows(dows)
            else:
                trip_demands = trip_demands.filter_date_interval(
                    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=user)
            # ordering and limit
            trip_demands = trip_demands.order_by(
                '-pourcentage_rank')[:max_trips]
            trip_demands = sort_demands(trip_demands,
                                        date,
                                        interval_min,
                                        interval_max,
                                        trip=trip)

        return {
            'trip': trip,
            'trip_demands': trip_demands,
            'trip_offers': trip_offers,
        }
Beispiel #6
0
    def get_trip_results(self, is_offer=None, is_demand=None, offer_radius=None, 
            demand_radius=None, date=None, interval_min=None, interval_max=None, 
            is_regular=None, dows=None, offer_route=None, departure_point=None, 
            arrival_point=None, max_trips=None, trip_id=None, user=None):
        """Return a list containing the trip object, trip_offers and 
        trip_demands matching given criterias.
        
        """
        # vars initialisation
        max_trips = max_trips or self.MAX_TRIPS
        trip = None
        trip_offers = None
        trip_demands = None
        
        if trip_id and user:
            # return matching trips with an already existing search
            try:
                trip = Trip.objects.get(pk=int(trip_id), user=user)
            except Trip.DoesNotExist:
                raise InvalidUser(user)
                
            if is_offer or trip.offer:
                is_offer = is_offer or True
                offer_radius = offer_radius or trip.offer.radius
                offer_route = offer_route or trip.offer.route
                    
            if (is_demand or trip.demand):
                is_demand = is_demand or True
                demand_radius = demand_radius or trip.demand.radius
            is_regular = is_regular or trip.regular
            date = date or trip.date
            dows = dows or trip.dows
            interval_min = interval_min or trip.interval_min
            interval_max = interval_max or trip.interval_max
            departure_point = departure_point or trip.departure_point
            arrival_point = arrival_point or trip.arrival_point
            
        if is_offer:
            offer_radius = offer_radius or self.OFFER_RADIUS
            if trip_id:
                if offer_route is None or offer_route.geom_type != 'MultiLineString':
                    raise InvalidGeometry("The trip offer route geometry is " \
                    "required and need to be a 'MultiLineString'. You gave us " \
                    "a '%(type)s' instead." % {
                        'type': "None" if offer_route is None else offer_route.geom_type
                    })
            else:
                if offer_route is None or offer_route.geom_type != 'LineString':
                    raise InvalidGeometry("The trip offer route geometry is " \
                    "required and need to be a 'LineString'. You gave us " \
                    "a '%(type)s' instead." % {
                        'type': "None" if offer_route is None else offer_route.geom_type
                    })
                    
        if is_demand:
            demand_radius = demand_radius or self.DEMAND_RADIUS
       
        interval_min = interval_min or self.INTERVAL_MIN
        interval_max = interval_max or self.INTERVAL_MAX
        today = datetime.date.today()
        date = date or today            
        
        if is_demand:
            trip_offers = Trip.objects.get_offers(departure_point, arrival_point, demand_radius)
            if trip_id:
                trip_offers = trip_offers.exclude(pk=trip_id)
            trip_offers = trip_offers.get_mark_details()
            
            if is_regular:
                trip_offers = trip_offers.filter_dows(dows)
            else:
                trip_offers = trip_offers.filter_date_interval(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=user)
            # ordering and limit
            trip_offers = trip_offers.order_by('-pourcentage_rank')[:max_trips]
            trip_offers = sort_offers(trip_offers, date, interval_min, interval_max, trip=trip)

        if is_offer:
            trip_demands = Trip.objects.get_demands(offer_route, get_direction_route(offer_route), offer_radius)
            if trip_id:
                trip_demands = trip_demands.exclude(pk=trip_id)
            trip_demands = trip_demands.get_mark_details()
            if is_regular:
                trip_demands = trip_demands.filter_dows(dows)
            else:
                trip_demands = trip_demands.filter_date_interval(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=user)
            # ordering and limit
            trip_demands = trip_demands.order_by('-pourcentage_rank')[:max_trips]
            trip_demands = sort_demands(trip_demands, date, interval_min, interval_max, trip=trip)

        return{
            'trip': trip,
            'trip_demands': trip_demands,
            'trip_offers': trip_offers,
        }