예제 #1
0
def create_order():
    form = OrderCreateForm()
    if form.validate():
        user_id = form.data.get('user_id')
        o = Order.query.filter(
            Order.is_canceled == False,
            Order.customer_id == user_id,
            Order.driver_id == None
        ).first()
        if o:
            return ujsonify({'errors': 'already took taxi'})

        lat = form.data['lat']
        lon = form.data['lon']
        time = form.data['time'] or datetime.utcnow()
        time = time if time > datetime.utcnow() else datetime.utcnow()
        o = Order()
        o.customer_id = user_id
        o.order_time = time
        db.session.add(o)
        db.session.commit()
        scheduler.enqueue_at(time, taxi_order_submit, o.id, user_id, lat, lon)
        logging.debug('{order_id} JOB AT CREATED order at: {time}'.format(
            order_id=o.id,
            time=time
        ))
        return ujsonify(o.get_public())
    return ujsonify({'errors': form.errors}), 400
예제 #2
0
def create_taxi():
    form = TaxiCreateForm()
    if form.validate():
        data = form.data.copy()
        data['driver_id'] = form.data.get('user_id')
        del data['user_id']
        t = Taxi(**data)
        db.session.add(t)
        db.session.commit()
        return ujsonify(t.get_public())
    return ujsonify({'errors': form.errors}), 400
예제 #3
0
def cancel_order(order_id):
    o = Order.query.filter(
        Order.id == order_id,
        Order.driver_id == None
    ).first()
    if o:
        o.is_canceled = True
        db.session.add(o)
        db.session.commit()
    else:
        return ujsonify({'errors': 'driver already on the way, we are sorry!'})
    return ujsonify({'results': o.get_public()})
예제 #4
0
def get_orders():
    q = Order.query.order_by(
        Order.created_at.desc()
    ).limit(
        100
    )
    return ujsonify({'results': [o.get_public() for o in q]})
def handle_parse_error(error, request):
    response = ujsonify(**error.messages)
    response.status_code = 400
    abort(response)
예제 #6
0
def list_view():
    c = Chart.query
    total = c.count()
    return ujsonify(result=[b.to_dict() for b in c], total=total)