def test_request_geolocation(self): # use a consistent default ZIP settings.DEFAULT_ZIP_CODE = '54403' # make a "request" to pass to geolocate_request mock_request = _Mock() mock_request.GET = {} mock_request.session = {} mock_request.META = {'REMOTE_ADDR': '127.0.0.1'} # should fall back to DEFAULT_ZIP_CODE l1 = geolocate_request(mock_request, 100) self.assertTrue(l1.location) self.assertEqual(l1.nearby_distance, 80) # should find the new ZIP, with a different location than default mock_request.GET['geo'] = '54481' l2 = geolocate_request(mock_request, 50) self.assertTrue(l2.location) self.assertEqual(l2.nearby_distance, 50) self.assertNotEqual(l2.location, l1.location) # geolocation should be saved in the session now del mock_request.GET['geo'] l3 = geolocate_request(mock_request) self.assertEqual(l3.location, l2.location)
def home(request, **kwargs): template_name = kwargs.get("template_name", "cancellations/index.html") request_location = geolocate_request(request, 10) location = request_location.location distance = request_location.nearby_distance geo_form = GeolocationDistanceForm(initial={ 'geo': request_location.zip_code, 'dist': distance}) (places, current, upcoming) = get_cancellations(location, distance) paginator = Paginator(current, 50) try: page = int(request.GET.get('page', '1')) except ValueError: page = 1 try: ncan = paginator.page(page) except (EmptyPage, InvalidPage): ncan = paginator.page(paginator.num_pages) return render(request, template_name, { 'upcoming_cancellations': upcoming, 'ncan': ncan, 'geo_form': geo_form, 'distance':distance, 'geo':request_location.zip_code, })
def _get_request_location(request): location = geolocate_request(request) # need to make sure the location has a ZIP for intellicast if not location.zip_code: try: zip_code = get_current_site(request).profile.zip_code except AttributeError: zip_code = settings.DEFAULT_ZIP_CODE else: if not zip_code: zip_code = settings.DEFAULT_ZIP_CODE location = geocode(zip_code) location.zip_code = zip_code return location
def home(request): request_location = geolocate_request(request) geo_form = GeolocationForm(initial={'geo': request_location.zip_code}) if request.method == "POST": form = PlaceForm(request.POST) if form.is_valid(): place = form.save() return redirect("loci_home") else: form = PlaceForm() return render(request, "loci/home.html", { "request_location": request_location, "geo_form": geo_form, "form": form, "places": Place.objects.all() })