def update(self, request): try: if request.user_type != 'Business': return Response('Acess denied') room_id = intValidator(request.data['id']) room = Room.objects.get(id=room_id) if room.hotel.business != request.authorized: return Response('Acess denied.') if request.data['nameEdit']: name = textValidator(request.data['name'], 40, 5) room.name = name if request.data['descriptionEdit']: description = textValidator(request.data['description'], 200, 10) room.description = description if request.data['priceEdit']: price = numValidator(request.data['price'], 5, 1000) room.price = price room.updated = timezone.now() room.save() serializer = RoomSerializer(room) return Response(serializer.data) except Exception: return Response('Send a valid data.')
def post(self, request): try: if request.user_type != 'Business': return Response('Acess denied.') hotel = Hotel.objects.get(id=request.data['hotelId']) if request.authorized.id != hotel.business.id: return Response('Acess denied.') name = textValidator(request.data['name'], 40, 5) description = textValidator(request.data['description'], 450, 10) price = numValidator(request.data['price'], 1000, 5) room = Room(name=name, description=description, price=price, created=timezone.now(), hotel=hotel, updated=timezone.now()) room.save() serializer = RoomSerializer(room) return Response(serializer.data) except CustomException as err: return Response({"error": str(err)}) except Exception: return Response({"error": 'An error has been occurred.'})
def post(self, request): try: room = Room.objects if request.data['priceRange'] == 'True': if request.data['minPrice'] != '-1': min_price = request.data['minPrice'] room = room.filter(price__gte=min_price) if request.data['maxPrice'] != '-1': max_price = request.data['maxPrice'] room = room.filter(price__lte=max_price) if request.data['nameSearch'] == 'True': name = textValidator(request.data['name'], 40, 2) room = room.filter(name__contains=name) if request.data['hotelIdSearch'] == 'True': hotel_id = request.data['hotelId'] room = room.filter(hotel_id=hotel_id) if request.data['hotelNameSearch'] == 'True': hotel_name = request.data['hotelName'] room = room.filter(hotel__name__contains=hotel_name) if request.data['citySearch'] == 'True': city = textValidator(request.data['city'], 40, 2) room = room.filter(hotel__city__contains=city) if request.data['stateSearch'] == 'True': state = textValidator(request.data['state'], 30, 2) room = room.filter(hotel__state__contains=state) # if request.data['reviewsSearch'] == 'True': # review_min = request.data['minReview'] # review_max = request.data['maxReview'] # room = room.filter(Q(reviews_average__gte=review_min) & Q(reviews_average__lte=review_max)) serializer = RoomSerializer(room, many=True) return Response(serializer.data) except CustomException as err: return Response({"error": str(err)}) except Exception: return Response({"error": 'An error has been occurred.'})
def post(self, request): try: if request.user_type != 'Business': raise Exception name = textValidator(request.data['name'], 30, 4) price = numValidator(request.data['price'], 1000, 0.1) description = textValidator(request.data['description'], 50) product = Product(name=name, price=price, description=description, created=timezone.now(), updated=timezone.now(), business=request.authorized) product.save() serializer = ProductSerializer(product) return Response(serializer.data) except Exception: return Response('Send a valid product.')
def post(self, request): try: if request.user_type != 'Client': raise CustomException("You don't have permission.") room_id = request.data['roomId'] booking_id = request.data['bookingId'] room = Room.objects.get(id=room_id) hotel = Hotel.objects.get(id=room.hotel.id) review_description = textValidator(request.data['description'], 250, 0) review_value = reviewValueValidator(int(request.data['value'])) booking = Booking.objects.get(id=booking_id) reviews = Review.objects.filter(booking__id=booking_id) if len(reviews) != 0: raise CustomException( "You already have a review created for this booking.") #problem here if booking.start > timezone.now(): raise CustomException("Your booking didn't started yet.") #saving review information at the hotel that the room is from hotel.reviews_count += 1 hotel.reviews_total += review_value hotel.reviews_average = hotel.reviews_total / hotel.reviews_count hotel.save() review = Review(value=review_value, booking=booking, description=review_description, room=room, client=request.authorized, created=timezone.now()) review.save() #saving review information at the room room.reviews_count += 1 room.reviews_total += review_value room.reviews_average = room.reviews_total / room.reviews_count room.reviews.add(review) room.save() booking.review = review booking.save() serializer = ReviewSerializer(review) return Response(serializer.data) except CustomException as err: return Response({"error": str(err)}) except Exception: return Response({"error": 'Send a valid data.'})
def update(self, request): try: if request.user_type != 'Business': raise Exception product_id = request.data['productId'] #validate product = Product.objects.get(id=product_id) if request.authorized != product.business: raise Exception if request.data['name'] != " ": product.name = textValidator(request.data['name'], 30, 4) if request.data['price'] != " ": product.price = numValidator(request.data['price'], 1000, 0.1) if request.data['description'] != " ": product.description = textValidator( request.data['description'], 50) product.updated = timezone.now() product.save() serializer = ProductSerializer(product) return Response(serializer.data) except Exception: return Response('Send a valid text.')
def post(self, request): try: if request.user_type != 'Business': raise Exception name = textValidator(request.data['name'], 30, 4) product_list = ProductList(name=name, created=timezone.now(), updated=timezone.now(), business=request.authorized) product_list.save() serializer = ProductListSerializer(productlist) return Response(serializer.data) except Exception: return Response('Send a valid product list.')