Skip to content

Instantly share code, notes, and snippets.

@hassanabidpk
Created April 16, 2016 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hassanabidpk/597bdb43d4e15f9828356cdb759c1bba to your computer and use it in GitHub Desktop.
Save hassanabidpk/597bdb43d4e15f9828356cdb759c1bba to your computer and use it in GitHub Desktop.
# required imports
def getRestaurantList(ilocation,query):
#....
# code implementation can be seen here https://github.com/hassanabidpk/searchrestaurant/blob/master/django/searchrestaurant/search/views.py
return result
class RestaurantList(APIView):
def get(self,request,format=None):
if "location" in request.GET and "rtype" in request.GET:
location = request.GET["location"]
location = location.replace(" ","+")
restaurantType = request.GET["rtype"]
else:
restaurants = Restaurant.objects.all()
serializer = RestaurantSerializer(restaurants, many=True)
return Response(serializer.data,status=status.HTTP_200_OK)
try:
loc = Location.objects.get(restaurant_location=location,restaurant_type=restaurantType)
restaurants = loc.restaurant.all()
serializer = RestaurantSerializer(restaurants, many=True)
return Response(serializer.data,status=status.HTTP_200_OK)
except MultipleObjectsReturned:
loc = loc[0]
restaurants = loc.restaurant.all()
serializer = RestaurantSerializer(restaurants, many=True)
return Response(serializer.data,status=status.HTTP_200_OK)
except ObjectDoesNotExist:
context = getRestaurantList(location,restaurantType)
try:
loc = Location.objects.get(restaurant_location=location,restaurant_type=restaurantType)
restaurants = loc.restaurant.all()
serializer = RestaurantSerializer(restaurants, many=True)
return Response(serializer.data,status=status.HTTP_200_OK)
except ObjectDoesNotExist:
print ("does not exist")
return Response(data={'error':"not-found",'status':"404"},status=status.HTTP_404_NOT_FOUND)
except MultipleObjectsReturned:
loc = loc[0]
restaurants = loc.restaurant.all()
serializer = RestaurantSerializer(restaurants, many=True)
return Response(serializer.data,status=status.HTTP_200_OK)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment