@@ -1,4 +1,4 @@ |
| 1 | | -from django.http import JsonResponse |
| 1 | +from django.http import JsonResponse, HttpResponse |
| 2 | 2 | from django.utils import timezone |
| 3 | 3 | from django.conf import settings |
| 4 | 4 | from django.db.models import Q |
@@ -39,6 +39,15 @@ def calculate_distance(lat1, lon1, lat2, lon2): |
| 39 | 39 | class NearbyRestaurantsView(APIView): |
| 40 | 40 | """Get restaurants near a location""" |
| 41 | 41 | |
| 42 | + def options(self, request, *args, **kwargs): |
| 43 | + """Handle preflight requests""" |
| 44 | + response = HttpResponse() |
| 45 | + response['Access-Control-Allow-Origin'] = request.headers.get('Origin', '*') |
| 46 | + response['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS' |
| 47 | + response['Access-Control-Allow-Headers'] = 'Content-Type, Accept, X-Requested-With' |
| 48 | + response['Access-Control-Max-Age'] = '3600' |
| 49 | + return response |
| 50 | + |
| 42 | 51 | def get(self, request): |
| 43 | 52 | lat = request.query_params.get('lat') |
| 44 | 53 | lng = request.query_params.get('lng') |
@@ -93,6 +102,15 @@ class RestaurantListCreateView(generics.ListCreateAPIView): |
| 93 | 102 | queryset = Restaurant.objects.all() |
| 94 | 103 | serializer_class = RestaurantSerializer |
| 95 | 104 | |
| 105 | + def options(self, request, *args, **kwargs): |
| 106 | + """Handle preflight requests""" |
| 107 | + response = HttpResponse() |
| 108 | + response['Access-Control-Allow-Origin'] = request.headers.get('Origin', '*') |
| 109 | + response['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS' |
| 110 | + response['Access-Control-Allow-Headers'] = 'Content-Type, Accept, X-Requested-With' |
| 111 | + response['Access-Control-Max-Age'] = '3600' |
| 112 | + return response |
| 113 | + |
| 96 | 114 | def create(self, request, *args, **kwargs): |
| 97 | 115 | # Check if restaurant already exists |
| 98 | 116 | place_id = request.data.get('place_id') |
@@ -109,11 +127,29 @@ class RestaurantDetailView(generics.RetrieveAPIView): |
| 109 | 127 | """Get detailed info about a restaurant""" |
| 110 | 128 | queryset = Restaurant.objects.all() |
| 111 | 129 | serializer_class = RestaurantDetailSerializer |
| 130 | + |
| 131 | + def options(self, request, *args, **kwargs): |
| 132 | + """Handle preflight requests""" |
| 133 | + response = HttpResponse() |
| 134 | + response['Access-Control-Allow-Origin'] = request.headers.get('Origin', '*') |
| 135 | + response['Access-Control-Allow-Methods'] = 'GET, OPTIONS' |
| 136 | + response['Access-Control-Allow-Headers'] = 'Content-Type, Accept, X-Requested-With' |
| 137 | + response['Access-Control-Max-Age'] = '3600' |
| 138 | + return response |
| 112 | 139 | |
| 113 | 140 | |
| 114 | 141 | class RestaurantRatingView(APIView): |
| 115 | 142 | """Add a rating to a restaurant or get all ratings""" |
| 116 | 143 | |
| 144 | + def options(self, request, *args, **kwargs): |
| 145 | + """Handle preflight requests""" |
| 146 | + response = HttpResponse() |
| 147 | + response['Access-Control-Allow-Origin'] = request.headers.get('Origin', '*') |
| 148 | + response['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS' |
| 149 | + response['Access-Control-Allow-Headers'] = 'Content-Type, Accept, X-Requested-With' |
| 150 | + response['Access-Control-Max-Age'] = '3600' |
| 151 | + return response |
| 152 | + |
| 117 | 153 | def get(self, request, pk): |
| 118 | 154 | try: |
| 119 | 155 | restaurant = Restaurant.objects.get(pk=pk) |
@@ -152,6 +188,15 @@ class RestaurantRatingView(APIView): |
| 152 | 188 | class RestaurantToastStatusView(APIView): |
| 153 | 189 | """Update restaurant's toast status""" |
| 154 | 190 | |
| 191 | + def options(self, request, *args, **kwargs): |
| 192 | + """Handle preflight requests""" |
| 193 | + response = HttpResponse() |
| 194 | + response['Access-Control-Allow-Origin'] = request.headers.get('Origin', '*') |
| 195 | + response['Access-Control-Allow-Methods'] = 'PATCH, OPTIONS' |
| 196 | + response['Access-Control-Allow-Headers'] = 'Content-Type, Accept, X-Requested-With' |
| 197 | + response['Access-Control-Max-Age'] = '3600' |
| 198 | + return response |
| 199 | + |
| 155 | 200 | def patch(self, request, pk): |
| 156 | 201 | try: |
| 157 | 202 | restaurant = Restaurant.objects.get(pk=pk) |
@@ -181,6 +226,15 @@ class RestaurantToastStatusView(APIView): |
| 181 | 226 | class SearchPlacesView(APIView): |
| 182 | 227 | """Search for places that might serve toast""" |
| 183 | 228 | |
| 229 | + def options(self, request, *args, **kwargs): |
| 230 | + """Handle preflight requests""" |
| 231 | + response = HttpResponse() |
| 232 | + response['Access-Control-Allow-Origin'] = request.headers.get('Origin', '*') |
| 233 | + response['Access-Control-Allow-Methods'] = 'GET, OPTIONS' |
| 234 | + response['Access-Control-Allow-Headers'] = 'Content-Type, Accept, X-Requested-With' |
| 235 | + response['Access-Control-Max-Age'] = '3600' |
| 236 | + return response |
| 237 | + |
| 184 | 238 | def get(self, request): |
| 185 | 239 | lat = request.query_params.get('lat') |
| 186 | 240 | lng = request.query_params.get('lng') |
@@ -353,6 +407,23 @@ class SearchPlacesView(APIView): |
| 353 | 407 | ] |
| 354 | 408 | |
| 355 | 409 | |
| 410 | +@api_view(['GET', 'OPTIONS']) |
| 411 | +def cors_test(request): |
| 412 | + """Test endpoint for CORS""" |
| 413 | + if request.method == 'OPTIONS': |
| 414 | + response = HttpResponse() |
| 415 | + response['Access-Control-Allow-Origin'] = '*' |
| 416 | + response['Access-Control-Allow-Methods'] = 'GET, OPTIONS' |
| 417 | + response['Access-Control-Allow-Headers'] = '*' |
| 418 | + return response |
| 419 | + |
| 420 | + return Response({ |
| 421 | + 'status': 'CORS test successful', |
| 422 | + 'origin': request.headers.get('Origin', 'No origin header'), |
| 423 | + 'method': request.method |
| 424 | + }) |
| 425 | + |
| 426 | + |
| 356 | 427 | @api_view(['POST']) |
| 357 | 428 | def seed_data(request): |
| 358 | 429 | """Seed the database with sample restaurants""" |