Пример #1
0
    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)
Пример #2
0
    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))
Пример #3
0
 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
Пример #4
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)
Пример #5
0
 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()
Пример #6
0
 def test_string_representation(self):
     hotel = Hotel(name="My MEGA hotel name")
     self.assertEqual(str(hotel), hotel.name)