def post(self, request): data = json.loads(request.body.decode('utf-8')) if 'top' in data: filter_qs = Hotel.get_top(self, data['top']) else: filter_qs = Hotel.get_nearest_places(self, data) serializer = HotelPlaceSerializer(filter_qs, many=True) return Response(serializer.data)
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
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 post(self, request): keywords = request.POST.get('keywords') if ('adult' in request.POST): category = Hotel.get_object_by_keywords(self, keywords) coordinates = Address.get_hotels_coordinates(self, keywords) else: category = FoodPlace.get_object_by_keywords(self, keywords) coordinates = Address.get_foodplace_coordinates(self, keywords) return render(request, 'search/result_page.html', { 'category': category, 'coordinates': coordinates })
class SavedPlacesApi(generics.ListCreateAPIView): """API for saving places""" queryset_hotels = Hotel.get_all_hotels() queryset_cafes = Cafe.get_all_cafes() hotels_serializer_class = HotelSerializer cafe_serializer_class = CafeSerializer def post(self, request): username = request.data['user_name'] place_name = request.data['place_name'] if SavedPlaces.save_place(username, place_name): return HttpResponse('Place was saved') else: return HttpResponse(status=status.HTTP_400_BAD_REQUEST)
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)
class HotelApi(generics.ListCreateAPIView): """API to get hotels and save comments""" queryset = Hotel.get_all_hotels() serializer_class = HotelSerializer permission_classes = (permissions.AllowAny, ) def post(self, request): self.save_comment(request.data) return HttpResponse("OK") 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 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)