예제 #1
0
    def get_taxi_assignment(cls, taxi_number, station_id):
        """
        returns the uuid for the ride the taxi is assigned to
        """
        logging.info("isr_proxy get assignment for taxi #%s (station %s)" % (taxi_number, station_id))

        if not taxi_number:
            return None

        key = cls._get_key_for_taxi_and_station(taxi_number, station_id)
        ride_uuid = memcache.get(key, namespace=cls.NAMESPACE)
        if not ride_uuid:  # try from db (maybe memcache was flushed)
            try:
                station = Station.objects.get(fleet_station_id=station_id)

                isrproxy_fm = first(lambda fm: fm.backend == ISRProxy, FleetManager.objects.all())
                isrproxy_ongoing_rides = fleet_manager.get_ongoing_rides(backend=isrproxy_fm)

                ride = first(lambda r: r.taxi_number == taxi_number and r.station == station, isrproxy_ongoing_rides)
                if ride:
                    ride_uuid = ride.uuid
                else:
                    logging.warning(u"no ongoing ride found for station %s and taxi %s" % (station, taxi_number))

            except Station.DoesNotExist, e:
                logging.error("station with fleet id %s not found" % station_id)

            except IndexError, e:
                logging.error("isr_proxy fleet manager does not exist in db")
예제 #2
0
def update_ride_add_order(ride, ride_data, new_order):
    # important:
    # connect new_order to ride ONLY AFTER update_ride is done.
    # If not, new_order will turn up in ride.orders.all() queries which doesn't reflect the state of the ride prior to joining

    update_ride(ride, ride_data, new_order=new_order)

    # create or update points for the new order
    for point_data in [ride_data.order_pickup_point(NEW_ORDER_ID), ride_data.order_dropoff_point(NEW_ORDER_ID)]:
        if len(point_data.order_ids) == 1:  # new point
            point = create_ride_point(ride, point_data)

        else:  # find existing point
            existing_order_id = first(lambda id: id != NEW_ORDER_ID, point_data.order_ids)
            existing_order = Order.by_id(existing_order_id)
            point = existing_order.pickup_point if point_data.stop_type == StopType.PICKUP else existing_order.dropoff_point
            logging.info("joining existing point %s" % point.id)

        if point_data.stop_type == StopType.PICKUP:
            new_order.pickup_point = point
        else:
            new_order.dropoff_point = point

    new_order.price_data = ride_data.order_price_data(NEW_ORDER_ID)
    new_order.ride = ride
    new_order.save()
예제 #3
0
 def get_order_point(self, point_type, order_id):
     """
     @param point_type: AlgoField.PICKUP or AlgoField.DROPOFF
     @param order_id: order id to look up
     @return: a PointData object for the point data of the given order id. If order id is not found returns None
     """
     raw_point_data = first(lambda p: order_id in p[AlgoField.ORDER_IDS] and p[AlgoField.TYPE] == point_type, self.raw_ride_data[AlgoField.RIDE_POINTS])
     return PointData(raw_point_data) if raw_point_data else None
예제 #4
0
 def get_order_point(self, point_type, order_id):
     """
     @param point_type: AlgoField.PICKUP or AlgoField.DROPOFF
     @param order_id: order id to look up
     @return: a PointData object for the point data of the given order id. If order id is not found returns None
     """
     raw_point_data = first(
         lambda p: order_id in p[AlgoField.ORDER_IDS] and p[AlgoField.TYPE]
         == point_type, self.raw_ride_data[AlgoField.RIDE_POINTS])
     return PointData(raw_point_data) if raw_point_data else None
예제 #5
0
def create_order(order_settings, passenger, ride=None, discount_data=None):
    """
    Returns a created Order or None

    @param order_settings:
    @param passenger:
    @param ride:
    @param discount_data: a DiscountData instance
    @return:
    """
    ride_id = ride.id if ride else NEW_ORDER_ID

    # get ride data from algo: don't trust the client
    candidates = [ride] if ride else []
    matching_rides = get_matching_rides(candidates, order_settings)
    ride_data = first(lambda match: match.ride_id == ride_id, matching_rides)

    if not ride_data:
        return None

    order = Order.fromOrderSettings(order_settings, passenger, commit=False)

    if ride:  # if joining a ride, order departure is as shown in offer, not what was submitted in order_settings
        ride_departure = compute_new_departure(ride, ride_data)
        new_order_pickup_point = ride_data.order_pickup_point(NEW_ORDER_ID)
        order.depart_time = ride_departure + datetime.timedelta(seconds=new_order_pickup_point.offset)

    if order_settings.private:
        order.type = OrderType.PRIVATE
    else:
        order.type = OrderType.SHARED

    order.price_data = ride_data.order_price_data(NEW_ORDER_ID)

    if discount_data:
        order = apply_discount_data(order, order_settings, discount_data)
        if not order:
            return None

    order.save()
    logging.info("created new %s order [%s]" % (OrderType.get_name(order.type), order.id))

    billing_trx = BillingTransaction(order=order, amount=order.get_billing_amount(), debug=order.debug)
    billing_trx.save()
    billing_trx.commit(callback_args={
        "ride_id": ride_id,
        "ride_data": ride_data,
        "discount_data": DiscountData.dump(discount_data)
    })

    return order
예제 #6
0
    def get_taxi_assignment(cls, taxi_number, station_id):
        """
        returns the uuid for the ride the taxi is assigned to
        """
        logging.info("isr_proxy get assignment for taxi #%s (station %s)" %
                     (taxi_number, station_id))

        if not taxi_number:
            return None

        key = cls._get_key_for_taxi_and_station(taxi_number, station_id)
        ride_uuid = memcache.get(key, namespace=cls.NAMESPACE)
        if not ride_uuid:  # try from db (maybe memcache was flushed)
            try:
                station = Station.objects.get(fleet_station_id=station_id)

                isrproxy_fm = first(lambda fm: fm.backend == ISRProxy,
                                    FleetManager.objects.all())
                isrproxy_ongoing_rides = fleet_manager.get_ongoing_rides(
                    backend=isrproxy_fm)

                ride = first(
                    lambda r: r.taxi_number == taxi_number and r.station ==
                    station, isrproxy_ongoing_rides)
                if ride:
                    ride_uuid = ride.uuid
                else:
                    logging.warning(
                        u"no ongoing ride found for station %s and taxi %s" %
                        (station, taxi_number))

            except Station.DoesNotExist, e:
                logging.error("station with fleet id %s not found" %
                              station_id)

            except IndexError, e:
                logging.error("isr_proxy fleet manager does not exist in db")
예제 #7
0
def get_previous_rides(request, passenger):
    data = []

    orders = passenger.orders.filter(depart_time__lt=utc_now(), status__in=ORDER_SUCCESS_STATUS).order_by('-depart_time')
    orders = orders.filter(type__in=[OrderType.PRIVATE, OrderType.SHARED])[:PREVIOUS_RIDES_TO_DISPLAY]
    seen_rides = []

    for order in orders:
        ride = order.ride
        if not ride:
            logging.error("order [%s] not valid for previous rides (order.ride is None" % order.id)
            continue
        if ride in seen_rides:
            continue  # skip duplicates (in case ride has multiple orders by same passenger)

        seen_rides.append(ride)

        ride_orders = ride.orders.all()
        ride_mates_orders = filter(lambda o: o != order, ride_orders)
        ride_mates = [{'name': mate_order.passenger.name, 'picture_url': mate_order.passenger.picture_url}
                                    for mate_order in ride_mates_orders for seat in range(order.num_seats)]

        dispatching_event = first(lambda e: e.taxi_id, ride.events.all())

        ride_data = {
            "order_id": order.id,
            "pickup_time": to_js_date(order.pickup_point.stop_time),
            "passengers": ride_mates,
            "seats_left": MAX_SEATS - sum([o.num_seats for o in ride_orders]),
            "your_seats": order.num_seats,
            "taxi_number": dispatching_event.taxi_id if dispatching_event else None,
            "station_name": ride.station.name if ride.station else WAYBETTER_STATION_NAME,
            "price": order.get_billing_amount(),
            "price_alone": order.price_alone,
            "billing_status": ugettext_lazy(order.get_status_display().title()),
            "pickup": order.from_raw,
            "dropoff": order.to_raw,
            "is_private": order.type == OrderType.PRIVATE,
            "comment": ""
            }

        data.append(ride_data)

    return JSONResponse(data)