def get(self, request, pk): selected_hotel = Hotel().get_object_by_pk(pk=pk) comments = Comment().get_objects_by_hotel( selected_hotel=selected_hotel) comments_serializer = CommentSerializer(comments, many=True) return JsonResponse(comments_serializer.data, safe=False)
def save_comment(self, data): try: hotel = Hotel().get_object_by_name(data['hotel']) comment = Comment(hotel=hotel, author=data['author'], text=data['text']) comment.save() except Exception as exc: print("save_comment EXCEPTION: ", repr(exc))
def save_hotel(self, hotel_info): """Create hotel object and save it. Getting dictionary with hotel information. Return 1 if hotel saved and 0 when not. """ try: address_info = self.extract_hotel_address(hotel_info) del hotel_info["address_raw_line"] hotel = Hotel(**hotel_info) hotel.save() address_info.update({"hotel": hotel}) hotel_address = Address(**address_info) hotel_address.save() return 1 except Exception as exc: print("save_hotel EXCEPTION: ", repr(exc)) return 0
class HotelAdditionAPI(generics.ListCreateAPIView): """API endpoint for listing and creating Hotel objects.""" queryset = Hotel().get_all_hotels() serializer_class = HotelAdditionSerializer permission_classes = (permissions.AllowAny, ) def post(self, request, *args, **kwargs): serializer = HotelAdditionSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.error_messages, status=status.HTTP_400_BAD_REQUEST)
def is_hotel_saved(self, name): """Check hotel for saving. Getting hotel name. Return: Boolean. True if hotel is already saved """ return name in Hotel().get_all_names()
def test_string_representation(self): hotel = Hotel(name="My MEGA hotel name") self.assertEqual(str(hotel), hotel.name)