Exemplo n.º 1
0
def machine_post_order_completed(event_key):
    """
    header Authorization: Token Authentication_machine_token
    """
    if g.get('current_machine', None) is None:
        return json_error('No machine found might have been a user token',
                          status=401)
    if g.current_machine.selected_order is None:
        return json_error('Machine has no order to finsih',
                          'Machine has no order to finish', 403)
    order = Order.find_by_id(g.current_machine.selected_order)
    if order is None:
        return json_error('Order not found', 'Order not found', 404)
    if order.machine_id is None:
        return json_error('Order does not have machine id, it should have one',
                          'Order does not have machine_id', 403)
    event = Event.find(event_key)
    if event is None:
        return json_error('Event not found', 'Event not found', 404)
    try:
        event.machine_finished_order(order)
    except Order_Process_Exception:
        return json_error(
            'Order: ' + str(order._id) +
            ' was not found in the new_orders of event: ' + str(event._id),
            'Order was not found in event new order', 404)
    g.current_machine.set_selected_order_done()
    order.set_done()
    return json_response(json.dumps(order.to_dict()), status=200)
Exemplo n.º 2
0
def machine_get_order(event_key, order_key):
    """
    header Authorization: Token Authentication_machine_token
    """
    if g.get('current_machine', None) is None:
        return json_error('No machine found might have been a user token',
                          status=401)
    if g.current_machine.selected_order is not None:
        return json_error('Machine has not finished its selected order',
                          'Machine not finished selected order', 403)
    order = Order.find(order_key)
    if order is None:
        return json_error('Order not found', 'Order not found', 404)
    if order.machine_id is not None:
        return json_error('Order already has an associated machine',
                          'Order already has an associated machine', 403)
    event = Event.find(event_key)
    if event is None:
        return json_error('Event not found', 'Event not found', 404)
    try:
        event.machine_get_order(order)
    except Order_Process_Exception:
        return json_error(
            'Order: ' + str(order._id) +
            ' was not found in the new_orders of event: ' + str(event._id),
            'Order was not found in event new order', 404)
    g.current_machine.set_selected_order(order)
    order.assigne_machine(g.current_machine._id)
    return json_response(json.dumps(order.to_dict()), status=200)
Exemplo n.º 3
0
def post_order():
    """
    header Authorization: Token Authentication_user_token
    {
      "event_key": "y37jsnks",
      "drinks": [{
          "mixer_type": "coke",
          "alcohol_type": "rhum",
          "double": True
      }],
    }
    """
    if request.content_type != JSON_MIME_TYPE:
        return json_error('Invalid Content Type', 'Invalid Content Type', 400)
    if g.get('current_user', None) is None:
        return json_error('No user found might have been a machine token',
                          status=401)
    data = request.json
    event = Event.find(data.get('event_key'))
    if event is None:
        return json_error('Event not found', 'Event not found', 404)
    drinks = [
        Drink(drink.get('mixer_type'), drink.get('alcohol_type'),
              drink.get('double')) for drink in data.get('drinks')
    ]
    # total_price = Order.get_price_from_drinks(drinks)
    # TODO process transation, for now assume the trasaction always passes
    new_order = Order(g.current_user._id, drinks, payed=True)
    new_order.create()
    event.add_new_order(new_order)
    g.current_user.add_order(new_order)
    return json_response(json.dumps(new_order.to_dict()), status=200)
Exemplo n.º 4
0
def post_machine_to_event(event_key):
    """
    header Authorization: Token Authentication_machine_token
    /api/v1/user/event/<string:event_key>
    """
    if g.get('current_machine', None) is None:
        return json_error('No machine found might have been a user token',
                          status=401)
    event = Event.find(event_key)
    if event is None:
        return json_error('Event not found', 'Event not found', 404)
    event.add_machine(g.current_machine)
    return json_response(json.dumps(event.to_dict()), status=200)