Example #1
0
    def post(self):
        data = json_decode(self.request.body)
        schema = {
            # TODO get customer_id from session
            # and set like an additional(not required) field
            # if auth user is booking a room for third-part person
            'customer_id': {
                'type': 'integer',
                'required': True},
            'room_id': {
                'type': 'integer',
                'required': True},
            'from_date': {
                'type': 'string',
                'required': True},
            'to_date': {
                'type': 'string',
                'required': True},
        }
        if not self.valid_data(schema, data):
            return

        customer_id = data['customer_id']
        room_id = data['room_id']
        from_date = parse_date(data['from_date'])
        to_date = parse_date(data['to_date'])

        room = Room.get_available_between_dates_by_id(room_id, from_date.isoformat(), to_date.isoformat())

        if not room:
            self.set_response(
                content={'message': ('The room with id {} is not available '
                                     'for booking from {} to {}').format(room_id, from_date, to_date)},
                status=404)
            return

        # TODO add payment system
        # change on next step with payment system
        status = Status.get(name='reserved')

        # TODO get customer_id from session
        try:
            customer = Customer.get(id=customer_id)
        except Customer.DoesNotExist:
            self.set_response(
                {'message': 'Customer with id {} not found'.format(customer_id)},
                status=400)
            return

        booking = Booking.create(
            room=room,
            customer=customer,
            from_date=from_date,
            to_date=to_date,
            status=status)
        self.set_response(
            content=dict(booking=model_to_dict(booking)),
            status=201,
            headers={'Location': self.reverse_url('booking', booking.id)}
        )
Example #2
0
 def get(self, room_id):
     try:
         room = Room.get(id=room_id)
         customers = Customer.select().join(Booking).join(Room).where(Room.id == room_id)
         customers = map(lambda customer: model_to_dict(
             customer,
             append_attrs={'url': self.reverse_url('customer', customer.id)}),
             customers)
         room = model_to_dict(
             room,
             append_attrs=dict(customers=customers)
             )
         self.set_response(dict(room=room))
     except Room.DoesNotExist:
         self.set_response(
             {'message': 'A room with id #{} not found'.format(room_id)},
             status=404)
Example #3
0
 def get(self, customer_id):
     fields = self.get_only_fields()
     try:
         rooms = Room.select().join(Booking).where(Booking.customer_id == customer_id)
         rooms = map(lambda item: model_to_dict(
             item,
             append_attrs={'url': self.reverse_url('room', item.id)},
             exclude=[Booking.customer]),
             rooms)
         customer = model_to_dict(
             Customer.get(id=customer_id),
             append_attrs=dict(rooms=rooms),
             only=fields)
         self.set_response(dict(customer=customer))
     except Customer.DoesNotExist:
         self.set_response(
             {'message': 'A customer with id #{} not found'.format(customer_id)},
             status=404)
Example #4
0
 def get(self):
     fields = self.get_only_fields()
     customers = Customer.select()
     customers = map(lambda customer: model_to_dict(
         customer,
         append_attrs={'url': self.reverse_url('customer', customer.id)},
         only=fields
     ), customers)
     self.set_response(dict(customers=customers))
Example #5
0
 def get(self, room_id):
     try:
         room = Room.get(id=room_id)
         booking = Booking.select().where(Booking.room_id == room_id)
         booking = map(lambda item: model_to_dict(
             item,
             append_attrs={'url': self.reverse_url('booking_order', item.id)},
             exclude=[Booking.room]),
             booking)
         room = model_to_dict(
             room,
             append_attrs=dict(booking=booking)
             )
         self.set_response(dict(room=room))
     except Room.DoesNotExist:
         self.set_response(
             {'message': 'A room with id #{} not found'.format(room_id)},
             status=404)
Example #6
0
 def get(self):
     fields = self.get_only_fields()
     roomsset = Room.select()
     rooms = map(lambda room: model_to_dict(
         room,
         append_attrs={'url': self.reverse_url('room', room.id)},
         only=fields
     ), roomsset)
     self.set_response(dict(rooms=rooms))
Example #7
0
 def get(self, customer_id):
     fields = self.get_only_fields()
     try:
         customer = model_to_dict(
             Customer.get(id=customer_id),
             only=fields)
         self.set_response(dict(customer=customer))
     except Customer.DoesNotExist:
         self.set_response(
             {'message': 'A customer with id #{} not found'.format(customer_id)},
             status=404)
Example #8
0
 def get(self, room_id):
     fields = self.get_only_fields()
     try:
         room = model_to_dict(
             Room.get(id=room_id),
             only=fields)
         self.set_response(dict(room=room))
     except Room.DoesNotExist:
         self.set_response(
             {'message': 'A room with id #{} not found'.format(room_id)},
             status=404)
Example #9
0
 def get(self, booking_id):
     try:
         booking = Booking.get(id=booking_id)
         booking = model_to_dict(
             booking,
         )
         self.set_response(dict(booking=booking))
     except Booking.DoesNotExist:
         self.set_response(
             {'message': 'Booking with id {} not found'.format(booking_id)},
             status=404)
Example #10
0
 def get(self):
     try:
         from_date, to_date = self._get_booking_date()
         count_booking_days = (to_date - from_date).days
     except ValueError as e:
         self.set_response(dict(message=str(e)), status=400)
         return
     queryset = Room.find_available_between_dates(from_date.isoformat(), to_date.isoformat())
     rooms = map(lambda room: model_to_dict(
         room,
         append_attrs=self._prepare_addition_attrs(room, count_booking_days)
     ), queryset)
     self.set_response(dict(rooms=rooms))
Example #11
0
 def post(self):
     data = json_decode(self.request.body)
     schema = {
         'type': {
             'type': 'integer',
             'required': True},
         'price': {
             'type': 'float',
             'required': True},
         'options': {
             'type': 'string'}
     }
     if not self.valid_data(schema, data):
         return
     room = Room.create(**data)
     self.set_response(
         content=dict(room=model_to_dict(room)),
         status=201,
         headers={'Location': self.reverse_url('room', room.id)})
Example #12
0
 def post(self):
     data = json_decode(self.request.body)
     scheme = {
         'name': {
             'type': 'string',
             'required': True},
         'passport': {
             'type': 'string',
             'required': True}}
     if not self.valid_data(scheme, data):
         return
     try:
         customer = Customer.create(**data)
         self.set_response(
             content=dict(customer=model_to_dict(customer)),
             status=201,
             headers={'Location': self.reverse_url('customer', customer.id)})
     except peewee.IntegrityError:
         self.set_response(
             content={'message': 'Customer with passport code {} already exists'.format(data['passport'])},
             status=409)
Example #13
0
 def put(self, customer_id):
     data = json_decode(self.request.body)
     scheme = {
         'name': {
             'type': 'string'},
         'passport': {
             'type': 'string'}}
     if not self.valid_data(scheme, data):
         return
     try:
         customer = Customer.get(id=customer_id)
         if 'name' in data:
             customer.name = data['name']
         if 'passport' in data:
             customer.passport = data['pasport']
         customer.save()
         self.set_response(
             content=dict(customer=model_to_dict(customer)),
             status=200,
             headers={'Location': self.reverse_url('customer', customer.id)})
     except Customer.DoesNotExist:
         self.set_response(
             {'message': 'A customer with id #{} not found'.format(customer_id)},
             status=404)