Example #1
0
def create_booking(user, stock=None, venue=None, recommendation=None, quantity=1, date_created=datetime.utcnow(),
                   date_used=None, is_cancelled=False, is_used=False, token=None, idx=None, amount=None):
    booking = Booking()
    if venue is None:
        offerer = create_offerer('987654321', 'Test address', 'Test city', '93000', 'Test name')
        venue = create_venue(offerer, 'Test offerer', '*****@*****.**', '123 rue test', '93000', 'Test city',
                             '93')
    if stock is None:
        product_with_thing_type = create_offer_with_thing_product(venue)
        stock = create_stock_with_thing_offer(offerer, venue, product_with_thing_type, price=10)
    booking.stock = stock
    booking.user = user
    if token is None:
        booking.token = random_token()
    else:
        booking.token = token

    if amount is None:
        booking.amount = stock.price
    else:
        booking.amount = amount

    booking.quantity = quantity
    booking.dateCreated = date_created
    if recommendation:
        booking.recommendation = recommendation
    elif not stock.offer:
        offer = create_offer_with_thing_product(venue)
        booking.recommendation = create_recommendation(offer, user)
    else:
        booking.recommendation = create_recommendation(stock.offer, user)
    booking.isCancelled = is_cancelled
    booking.isUsed = is_used
    booking.dateUsed = date_used
    if idx:
        booking.id = idx
    return booking
Example #2
0
    def post(self, obj_id=None):
        """
       Create a new booking/ Update a booking
       ---
       tags:
         - bookings
       parameters:
         - in: path
           name: obj_id
         - in: body
           name: body
           schema:
             id: Booking
             required:
               - room_number
               - customerID
             optional:
               - is_active
             properties:
               customerID:
                 type: string
                 description: customer id
               room_number:
                 type: integer
                 description: room number
               is_active:
                 type: boolean
                 description: room booked
       responses:
         201:
           description: Booking created
       """
        data = self.prepare_data()
        if obj_id:
            form = UpdateBookingForm(data, csrf_enabled=False)
            if form.validate():
                booking = Booking.get_by_id(int(obj_id))
                if not booking:
                    abort(404, message="Booking with key ({}) not found".format(obj_id))

                booking.is_active = form.is_active.data
                booking.put()

                room = Room.query(Room.number == booking.room_number).get()
                room.is_booked = True if booking.is_active is True else False
                room.put()

                output = self.output_fields
                output.update(self.resource_fields)
                return marshal(booking, output), 200

            error_data = self.prepare_errors(form.errors)
            raise CustomException(code=400, name='Validation Failed', data=error_data)

        else:
            form = BookingForm(data, csrf_enabled=False)
            if form.validate():
                booking = Booking(customerID=form.customerID.data, room_number=int(form.room_number.data),
                                  is_active=True)
                booking.put()
                booking.id = str(booking.key.id())
                booking.put()

                room = Room.query(Room.number == booking.room_number).get()
                room.is_booked = True if booking.is_active is True else False
                room.put()

                output = self.output_fields
                output.update(self.resource_fields)
                return marshal(booking, output), 201

            error_data = self.prepare_errors(form.errors)
            raise CustomException(code=400, name='Validation Failed', data=error_data)