def process_update_school_form(request, school, old_anchor): update_school_form = OrganisationForm( request.POST, user=request.user, current_school=school ) if update_school_form.is_valid(): data = update_school_form.cleaned_data name = data.get("name", "") postcode = data.get("postcode", "") country = data.get("country", "") school.name = name school.postcode = postcode school.country = country error, country, town, lat, lng = lookup_coord(postcode, country) school.town = town school.latitude = lat school.longitude = lng school.save() anchor = "#" messages.success( request, "You have updated the details for your school or club successfully.", ) else: anchor = old_anchor return anchor
def process_update_school_form(request, school, old_anchor): update_school_form = OrganisationForm(request.POST, user=request.user, current_school=school) if update_school_form.is_valid(): data = update_school_form.cleaned_data name = data.get("name", "") postcode = data.get("postcode", "") country = data.get("country", "") school.name = name school.postcode = postcode school.country = country error, country, town, lat, lng = lookup_coord(postcode, country) school.town = town school.latitude = lat school.longitude = lng school.save() anchor = "#" messages.success( request, "You have updated the details for your school or club successfully.", ) else: anchor = old_anchor return anchor
def fill_in_missing_school_locations(request): schools = School.objects.filter(latitude='0', longitude='0') requests = 0 failures = [] town0 = 0 for school in schools: requests += 1 sleep(0.2) # so we execute a bit less than 5/sec error, \ school.country, \ school.town, \ school.latitude, \ school.longitude = lookup_coord(school.postcode, school.country.code) if error is None: school.save() if error is not None: failures += [(school.id, school.postcode, error)] if school.town == '0': town0 += 1 messages.info(request, 'Made %d requests' % requests) messages.info(request, 'There were %d errors: %s' % (len(failures), str(failures))) messages.info(request, '%d school have no town' % town0)
def fill_in_missing_school_locations(request): schools = School.objects.filter(latitude="0", longitude="0") requests = 0 failures = [] town0 = 0 for school in schools: requests += 1 sleep(0.2) # so we execute a bit less than 5/sec error, school.country, school.town, school.latitude, school.longitude = lookup_coord( school.postcode, school.country.code ) if error is None: school.save() if error is not None: failures += [(school.id, school.postcode, error)] if school.town == "0": town0 += 1 messages.info(request, "Made %d requests" % requests) messages.info(request, "There were %d errors: %s" % (len(failures), str(failures))) messages.info(request, "%d school have no town" % town0)
def test_lookup_coord_invalid_postcode_country_gb(self): responses.add(responses.GET, MAPS_API_GEOCODE_JSON + 'address=10000&components=country:GB', body=read_json_from_file(datafile('10000_gb.json')), match_querystring=True, content_type='application/json') result = lookup_coord('10000', 'GB') self.assertEqual(result, (None, 'GB', 0, 55.378051, -3.435973))
def test_lookup_coord_valid_postcode_country_gb2(self): responses.add(responses.GET, MAPS_API_GEOCODE_JSON + 'address=AL10 9NE&components=country:GB', body=read_json_from_file(datafile('al109ne_gb.json')), match_querystring=True, content_type='application/json') result = lookup_coord('AL10 9NE', 'GB') self.assertEqual(result, (None, 'GB', 'Hatfield', 51.7623259, -0.2438929))
def test_lookup_coord_valid_postcode_country_gb1(self): responses.add(responses.GET, MAPS_API_GEOCODE_JSON + 'address=SW72AZ&components=country:GB', body=read_json_from_file(datafile('sw72az_gb.json')), match_querystring=True, content_type='application/json') result = lookup_coord('SW72AZ', 'GB') self.assertEqual(result, (None, 'GB', 'London', 51.5005046, -0.1782187))
def test_lookup_coord_json_unchanged(self): responses.add(responses.GET, MAPS_API_GEOCODE_JSON + 'address=SW72AZ&components=country:GB', body=read_json_from_file(datafile('sw72az_gb.json')), match_querystring=True, content_type='application/json') result = lookup_coord('SW72AZ', 'GB') self.assertEqual(responses.calls[0].response.json(), json.loads(read_json_from_file(datafile('sw72az_gb.json'))))
def test_lookup_coord_call_api_once(self): responses.add(responses.GET, MAPS_API_GEOCODE_JSON + 'address=SW72AZ&components=country:GB', body=read_json_from_file(datafile('sw72az_gb.json')), match_querystring=True, content_type='application/json') result = lookup_coord('SW72AZ', 'GB') self.assertEqual(len(responses.calls), 1, 'API was called more than once')
def test_lookup_coord_invalid_postcode_country_kr(self): responses.add(responses.GET, MAPS_API_GEOCODE_JSON + 'address=AL109NE&components=country:KR', body=read_json_from_file(datafile('al109ne_kr.json')), match_querystring=True, content_type='application/json') result = lookup_coord('AL109NE', 'KR') self.assertEqual(result, (None, 'KR', 0, 35.907757, 127.766922))
def test_lookup_coord_zero_results(self): responses.add(responses.GET, MAPS_API_GEOCODE_JSON + 'address=AL109NE&components=country:AB', body=read_json_from_file(datafile('al109ne_ab.json')), match_querystring=True, content_type='application/json') error, country, town, lat, lng = lookup_coord('AL109NE', 'AB') assert 'API error' in error self.assert_default_coord(town, lat, lng)
def test_lookup_coord_cannot_decode_json(self): responses.add(responses.GET, MAPS_API_GEOCODE_JSON + 'address=AL109NE&components=country:GB', body='', match_querystring=True, content_type='application/json') error, country, town, lat, lng = lookup_coord('AL109NE', 'GB') assert 'Value error' in error self.assert_default_coord(town, lat, lng)
def test_lookup_coord_request_error_404(self): responses.add(responses.GET, MAPS_API_GEOCODE_JSON + 'address=AL109NE&components=country:GB', body=read_json_from_file(datafile('al109ne_gb.json')), status=requests.codes.not_found, match_querystring=True, content_type='application/json') error, country, town, lat, lng = lookup_coord('AL109NE', 'GB') assert 'Request error' in error self.assert_default_coord(town, lat, lng)
def test_lookup_coord_call_api_once(self): responses.add( responses.GET, MAPS_API_GEOCODE_JSON + "address=SW72AZ&components=country:GB", body=read_json_from_file(datafile("sw72az_gb.json")), match_querystring=True, content_type="application/json", ) result = lookup_coord("SW72AZ", "GB") self.assertEqual(len(responses.calls), 1, "API was called more than once")
def test_lookup_coord_zero_results(self): responses.add( responses.GET, MAPS_API_GEOCODE_JSON + "address=AL109NE&components=country:AB", body=read_json_from_file(datafile("al109ne_ab.json")), match_querystring=True, content_type="application/json", ) error, country, town, lat, lng = lookup_coord("AL109NE", "AB") assert "API error" in error self.assert_default_coord(town, lat, lng)
def test_lookup_coord_cannot_decode_json(self): responses.add( responses.GET, MAPS_API_GEOCODE_JSON + "address=AL109NE&components=country:GB", body="", match_querystring=True, content_type="application/json", ) error, country, town, lat, lng = lookup_coord("AL109NE", "GB") assert "Value error" in error self.assert_default_coord(town, lat, lng)
def organisation_teacher_view(request, is_admin): teacher = request.user.userprofile.teacher school = teacher.school coworkers = Teacher.objects.filter(school=school).order_by( 'user__user__last_name', 'user__user__first_name') join_requests = Teacher.objects.filter( pending_join_request=school).order_by('user__user__last_name', 'user__user__first_name') form = OrganisationForm(user=request.user, current_school=school) form.fields['name'].initial = school.name form.fields['postcode'].initial = school.postcode form.fields['country'].initial = school.country if request.method == 'POST' and is_admin: form = OrganisationForm(request.POST, user=request.user, current_school=school) if form.is_valid(): data = form.cleaned_data name = data.get('name', '') postcode = data.get('postcode', '') country = data.get('country', '') school.name = name school.postcode = postcode school.country = country error, country, town, lat, lng = lookup_coord(postcode, country) school.town = town school.latitude = lat school.longitude = lng school.save() messages.success( request, 'You have updated the details for your school or club successfully.' ) return render( request, 'portal/teach/organisation_manage.html', { 'teacher': teacher, 'is_admin': is_admin, 'coworkers': coworkers, 'join_requests': join_requests, 'form': form, })
def organisation_teacher_view(request, is_admin): teacher = request.user.userprofile.teacher school = teacher.school coworkers = Teacher.objects.filter(school=school).order_by('user__user__last_name', 'user__user__first_name') join_requests = Teacher.objects.filter(pending_join_request=school).order_by('user__user__last_name', 'user__user__first_name') form = OrganisationForm(user=request.user, current_school=school) form.fields['name'].initial = school.name form.fields['postcode'].initial = school.postcode form.fields['country'].initial = school.country if request.method == 'POST' and is_admin: form = OrganisationForm(request.POST, user=request.user, current_school=school) if form.is_valid(): data = form.cleaned_data name = data.get('name', '') postcode = data.get('postcode', '') country = data.get('country', '') school.name = name school.postcode = postcode school.country = country error, country, town, lat, lng = lookup_coord(postcode, country) school.town = town school.latitude = lat school.longitude = lng school.save() messages.success(request, 'You have updated the details for your school or club successfully.') return render(request, 'portal/teach/organisation_manage.html', { 'teacher': teacher, 'is_admin': is_admin, 'coworkers': coworkers, 'join_requests': join_requests, 'form': form, })
def process_update_school_form(request, school): update_school_form = OrganisationForm(request.POST, user=request.user, current_school=school) if update_school_form.is_valid(): data = update_school_form.cleaned_data name = data.get('name', '') postcode = data.get('postcode', '') country = data.get('country', '') school.name = name school.postcode = postcode school.country = country error, country, town, lat, lng = lookup_coord(postcode, country) school.town = town school.latitude = lat school.longitude = lng school.save() messages.success( request, 'You have updated the details for your school or club successfully.' )
def test_lookup_coord_connection_error(self): error, country, town, lat, lng = lookup_coord('AL109NE', 'GB') assert 'Connection error' in error self.assert_default_coord(town, lat, lng)
def test_lookup_coord_connection_error(self): error, country, town, lat, lng = lookup_coord("AL109NE", "GB") assert "Connection error" in error self.assert_default_coord(town, lat, lng)