def customer_get_restaurants(request): restaurants = RestaurantSerializer( Restaurant.objects.all().order_by("-id"), many = True, context = {"request": request} ).data return JsonResponse({"restaurants": restaurants})
def customer_get_restaurants(request): try: restaurants = RestaurantSerializer( Restaurant.objects.all().order_by("-id"), many=True, context={ "request": request }).data return JsonResponse({"restaurants": restaurants}) except AccessToken.DoesNotExist: return JsonResponse({ "status": "falied", "error": "access_token is wrong" })
def get_open_restaurants_near_customer(request): ''' Returns a list of 10 restaurants which are open and near the user Latitude and longitude of the customer are required. If the starting id of the restaurant is provided, results are filtered on restaurants having id more than that of the provided id :param request: http request :return: list of restaurants, each entry a dict of the restaurant's attributes ''' from haversine import haversine latitude = float(request.GET.get('latitude')) longitude = float(request.GET.get('longitude')) starting_id = request.GET.get('starting_id') batch_size = int(request.GET.get('batch_size', 10)) operating_distance = int(request.GET.get('distance', 5)) if starting_id: restaurants = Restaurant.objects.filter( id__gt=starting_id).order_by('id') else: restaurants = Restaurant.objects.all().order_by('id') restaurants_within_distance = [] for restaurant in restaurants: distance = None if restaurant.latitude and restaurant.longitude: distance = haversine((latitude, longitude), (restaurant.latitude, restaurant.longitude)) if distance and distance <= operating_distance and restaurant.is_open( ): restaurants_within_distance.append(restaurant) if len(restaurants_within_distance) >= batch_size: break serialized_restaurants = RestaurantSerializer(restaurants_within_distance, many=True, context={ "request": request }).data return JsonResponse({"restaurants": serialized_restaurants})